一、极光推送(j-PUSH)( https://www.jiguang.cn/dev/#/app/list#dev )
按照jpush-react-native 官方给的文档集成,但是不全面;结合[http://www.jianshu.com/p/a71512a8f921](http://www.jianshu.com/p/a71512a8f921) ;
在执行官方的脚本之后(appkey需要去官网注册应用);
ios的appdelegate.m,以及Android中的MainApplication.java,在react native link 之后也许路径或者初始化的位置不对,需要手动配置到对应位置;有时候会重复引用,记得删除Xcode中build Phases->Link Binary With Libraries或者android studio 中file->Project Structure中—掉即可;
在Android以及ios中遇到的问题有:
1、Android:
初始化,------》添加完成后Scyc Now一下,还有问题去查看MainApplication.java,以及对应的文件目录是否正确(不知道目录的情况下输入库的第一个字母它会提示下面的子目录,然后按照提示找到对应的子目录,检查无误后还报错的话,重启AndroidStudio)文件路径不对,(找对应的包去找它的路径)删除掉的第三方库没从Android Studio完全剔除;(file->Project Structure中—掉)有些interFace爆红 (可能是官网文档有问题,去搜索然后添加对应的API路径)
2 、IOS
link完之后编译报错;
1、文件没有引入对应的路径或者就是build Phases->Link Binary With Libraries中没有引入对应的framework或 者某个framework不是required是optional;
2、link的方法位置不对,需要手动来修改;(其中有些语法没link完整,需要查看并补全对应的语法结构));
[Expected ; after method prototype error] 参考:(https://stackoverflow.com/questions/26190254/expected-after-method-prototype-error)
Searchpaths 路径没有引入;(Build Setting 中的framework/Header Search Paths ->类似这样去添加路径:$(SRCROOT)/../node_modules/jpush-react-native/ios/RCTJPushModule);
[iOS下dyld: Library not loaded:](https://segmentfault.com/a/1190000000657902)
找到Link Binary With Libraries,在里面找到xx.framework,把status由Required 修改成Optional,然后再运行就OK了。其他的framework报错也适用,只需要修改对应的framework的status就行了。
(参考(https://segmentfault.com/a/1190000000657902) );
3、iOS “dyld`dyld_fatal_error: -> 0x12000d088 <+0>: brk #0x3”错误 (打包真机测试时的错误)
错误描述
- 硬件环境:Mac x.x + Xcode x.x
- 软件环境:RN项目
- 模拟器运行正常,IOS10系统以下真机一运行就崩溃
- 证书没有问题
借鉴 (http://blog.csdn.net/idoshi201109/article/details/50803419) (http://blog.csdn.net/chenyblog/article/details/50790635)
二、参考简书文章:
http://www.jianshu.com/p/6721a0360af9
http://www.jianshu.com/p/a71512a8f921
三、ReactNative中操作:
调用对应的api实现项目的需求;(注意依赖库之间的版本IOS和Android的API有些不是统一的,不同的环境API需要斟酌使用 https://github.com/jpush/jpush-react-native/blob/master/example/documents/APIs_zh.md )
三个库:
https://github.com/jpush/jpush-react-native
https://github.com/jpush/janalytics-react-native
https://github.com/jpush/jcore-react-native
四、推送消息绑定设备alias:
每次改完代码热更新不起作用需要重新编译才可以生效;
五、极光社区可以找到相关的问题解答:
极光社区:https://community.jiguang.cn ;
ios纯原生配置: https://community.jiguang.cn/t/topic/13924
下载demo看代码;
下载下来的文件: jpushfileimage.png
六、拓展:
从零开始搭建即时通讯项目: https://community.jiguang.cn/t/topic/20505 (转自极光社区)
七、附上React-native中实现代码:
其中实现了绑定alias设备来实现推送;
initJpush() {
const navigation = this.refs.app_root_nav._navigation;
console.log("navigation", navigation);
// 新版本必需写回调函数
if (!isIos) {
JPushModule.notifyJSDidLoad((resultCode) => {
if (resultCode === 0) {
console.log(resultCode)
}
});
}
// 接收自定义消息
JPushModule.addReceiveCustomMsgListener((message) => {
console.log("receive notification: " + message);
});
//应用没有启动情况 ;
JPushModule.addOpenNotificationLaunchAppListener(message => {
launchedApp = true;
console.log("addOpenNotificationLaunchAppListener=====>NO", message);
let params = Platform.OS === "android" ? JSON.parse(message.extras) : message;
openPage(navigation, params);
if (Platform.OS == "ios") {
JPushModule.getBadge((badge) => {
badges = badge;
console.log("jpush-", typeof badges, badges);
if (badges > 0) {
badges--;
console.log("jpush----", badges);
JPushModule.setBadge(badges, (success) => {
console.log("jpush", success);
});
}
});
}
});
// 接收推送通知&&iOS 10 以下的系统点击推送回调;
JPushModule.addReceiveNotificationListener(message => {
if (launchedApp) {
return launchedApp = false
}
console.log("接收到消息=====>", message,Platform.OS == "ios" && Platform.Version.split(".")[0] > 10,Platform.Version);
//处理iOS10以下APP打开状态接收推送消息的状态;
if (Platform.OS == "ios" && message.localNotice && AppState.currentState == "active") {
return false
}
//解决APP打开状态接收到消息不增加消息徽标
if (Platform.OS == "ios" && Platform.Version.split(".")[0] >= 10) {
//因为iOS10以下APP打开状态时接收到消息但是不显示在通知栏所以暂时先这样处理;
let sumbagde = message.aps.badge;
console.log("jpush----", message.aps.badge);
JPushModule.setBadge(sumbagde, (success) => {
console.log("jpush", success);
});
}
if (Platform.OS == "ios" && Platform.Version.split(".")[0] < 10 && AppState.currentState !== "active") {
console.log("addReceiveNotificationListener<10=====>", message)
let params = Platform.OS === "android" ? JSON.parse(message.extras) : message;
openPage(navigation, params);
JPushModule.getBadge((badge) => {
badges = badge;
console.log("jpush-", typeof badges, badges);
if (badges > 0) {
badges--;
console.log("jpush----", badges);
JPushModule.setBadge(badges, (success) => {
console.log("jpush", success);
});
}
});
}
if (Platform.OS == "ios" && Platform.Version.split(".")[0] < 10 && AppState.currentState == "active") {
let params = Platform.OS === "android" ? JSON.parse(message.extras) : message;
console.log("xxxx", params);
let cdate = new Date();
JPushModule.sendLocalNotification({
fireTime: cdate.getTime() + 500,
content: params.aps.alert,
badge: params.aps.badge,
extra: {localNotice: true,...params}
})
}
});
//点击推送消息打开应用回调
JPushModule.addReceiveOpenNotificationListener(message => {
if (launchedApp) {
return launchedApp = false
}
console.log("addReceiveOpenNotificationListener10=====>", message);
let params = Platform.OS === "android" ? JSON.parse(message.extras) : message;
openPage(navigation, params);
if (Platform.OS == "ios") {
JPushModule.getBadge((badge) => {
badges = badge;
console.log("jpush-", typeof badges, badges);
if (badges > 0) {
badges--;
console.log("jpush----", badges);
JPushModule.setBadge(badges, (success) => {
console.log("jpush", success);
});
}
});
}
});
}
//极光统计;
initJAnalytics() {
if (isIos) {
JAnalyticsModule.setup({appKey: "申请的APPKey"});
JAnalyticsModule.setDebug({enable: true});
JAnalyticsModule.crashLogON();
console.log("极光分析----");
}
}
//jpush绑定设备到alias
jpushSetAlias(user_id) {
console.log("jpush当前用户的登录信息", user_id, typeof user_id);
JPushModule.setAlias(user_id, (map) => {
console.log("jpush当前用户的登录信息", map);
if (map.errorCode === 0) {
console.log("jpush-android注册成功", map);
AsyncStorage.setItem("alias", map.alias)
} else {
console.log("jpushios注册成功", map);
AsyncStorage.setItem("alias", map.alias)
}
});
}
//删除掉alias
jpushDeteleAlias() {
JPushModule.deleteAlias((map) => {
if (map.errorCode === 0) {
console.log("jpush android删除成功", map);
AsyncStorage.removeItem("alias");
} else {
console.log("jpush ios删除成功", map);
AsyncStorage.removeItem("alias");
}
});
}
componentDidMount() {
this.initJpush();
this.initJAnalytics();
let that = this;
//防止在登录或者其他入口使用时没有绑定到alias的处理;
AsyncStorage.getItem("user_id", (err, res) => {
user_id = res;
AsyncStorage.getItem("alias", (err, res) => {
alias = res;
console.log("jpush-root外", err, user_id, alias, typeof user_id);
if (!_.isEmpty(alias)) {
if (_.isEmpty(user_id)) {
console.log("jpush-root内1", user_id, alias);
that.jpushDeteleAlias();
}
}
if (!_.isEmpty(user_id)) {
if (_.isEmpty(alias)) {
console.log("jpush-root内2", user_id, alias);
that.jpushSetAlias(user_id);
}
}
})
});
}
componentWillUnmount() {
//取消;
JPushModule.removeReceiveCustomMsgListener();
JPushModule.removeReceiveNotificationListener();
JPushModule.removeOpenNotificationLaunchAppEventListener();
}
以上几个方法添加到指定的业务需求即可使用,想要扩展业务需求还需再进行其他配置;
如有问题还需谅解;本人一点拙见。哈哈
「本文系极光征文 | 写写文章就能赢 Filco,岂不美滋滋的参赛文章」
网友评论