一、極光推送(j-PUSH)( https://www.jiguang.cn/dev/#/app/list#dev )
按照jpush-react-native 官方給的文檔集成,但是不全面;結(jié)合[http://www.itdecent.cn/p/a71512a8f921](http://www.itdecent.cn/p/a71512a8f921) ;
在執(zhí)行官方的腳本之后(appkey需要去官網(wǎng)注冊(cè)應(yīng)用);
ios的appdelegate.m,以及Android中的MainApplication.java,在react native link 之后也許路徑或者初始化的位置不對(duì),需要手動(dòng)配置到對(duì)應(yīng)位置;有時(shí)候會(huì)重復(fù)引用,記得刪除Xcode中build Phases->Link Binary With Libraries或者android studio 中file->Project Structure中—掉即可;
在Android以及ios中遇到的問(wèn)題有:
1、Android:
初始化,------》添加完成后Scyc Now一下,還有問(wèn)題去查看MainApplication.java,以及對(duì)應(yīng)的文件目錄是否正確(不知道目錄的情況下輸入庫(kù)的第一個(gè)字母它會(huì)提示下面的子目錄,然后按照提示找到對(duì)應(yīng)的子目錄,檢查無(wú)誤后還報(bào)錯(cuò)的話,重啟AndroidStudio)文件路徑不對(duì),(找對(duì)應(yīng)的包去找它的路徑)刪除掉的第三方庫(kù)沒(méi)從Android Studio完全剔除;(file->Project Structure中—掉)有些interFace爆紅 (可能是官網(wǎng)文檔有問(wèn)題,去搜索然后添加對(duì)應(yīng)的API路徑)
2 、IOS
link完之后編譯報(bào)錯(cuò);
????1、文件沒(méi)有引入對(duì)應(yīng)的路徑或者就是build Phases->Link Binary With Libraries中沒(méi)有引入對(duì)應(yīng)的framework或 者某個(gè)framework不是required是optional;
????2、link的方法位置不對(duì),需要手動(dòng)來(lái)修改;(其中有些語(yǔ)法沒(méi)link完整,需要查看并補(bǔ)全對(duì)應(yīng)的語(yǔ)法結(jié)構(gòu)));
[Expected ; after method prototype error] 參考:(https://stackoverflow.com/questions/26190254/expected-after-method-prototype-error)
Searchpaths 路徑?jīng)]有引入;(Build Setting 中的framework/Header Search Paths ->類(lèi)似這樣去添加路徑:$(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,然后再運(yùn)行就OK了。其他的framework報(bào)錯(cuò)也適用,只需要修改對(duì)應(yīng)的framework的status就行了。
(參考(https://segmentfault.com/a/1190000000657902) );
????3、iOS “dyld`dyld_fatal_error: -> 0x12000d088 <+0>: brk #0x3”錯(cuò)誤 (打包真機(jī)測(cè)試時(shí)的錯(cuò)誤)
錯(cuò)誤描述
- 硬件環(huán)境:Mac x.x + Xcode x.x
- 軟件環(huán)境:RN項(xiàng)目
- 模擬器運(yùn)行正常,IOS10系統(tǒng)以下真機(jī)一運(yùn)行就崩潰
- 證書(shū)沒(méi)有問(wèn)題
借鑒 (http://blog.csdn.net/idoshi201109/article/details/50803419) (http://blog.csdn.net/chenyblog/article/details/50790635)
二、參考簡(jiǎn)書(shū)文章:
http://www.itdecent.cn/p/6721a0360af9
http://www.itdecent.cn/p/a71512a8f921
三、ReactNative中操作:
調(diào)用對(duì)應(yīng)的api實(shí)現(xiàn)項(xiàng)目的需求;(注意依賴(lài)庫(kù)之間的版本IOS和Android的API有些不是統(tǒng)一的,不同的環(huán)境API需要斟酌使用 https://github.com/jpush/jpush-react-native/blob/master/example/documents/APIs_zh.md )
三個(gè)庫(kù):
https://github.com/jpush/jpush-react-native
https://github.com/jpush/janalytics-react-native
https://github.com/jpush/jcore-react-native
四、推送消息綁定設(shè)備alias:
每次改完代碼熱更新不起作用需要重新編譯才可以生效;
五、極光社區(qū)可以找到相關(guān)的問(wèn)題解答:
極光社區(qū):https://community.jiguang.cn ;
ios純?cè)渲茫?https://community.jiguang.cn/t/topic/13924
下載demo看代碼;

下載下來(lái)的文件:

六、拓展:
從零開(kāi)始搭建即時(shí)通訊項(xiàng)目: https://community.jiguang.cn/t/topic/20505 (轉(zhuǎn)自極光社區(qū))
七、附上React-native中實(shí)現(xiàn)代碼:
其中實(shí)現(xiàn)了綁定alias設(shè)備來(lái)實(shí)現(xiàn)推送;
initJpush() {
const navigation = this.refs.app_root_nav._navigation;
console.log("navigation", navigation);
// 新版本必需寫(xiě)回調(diào)函數(shù)
if (!isIos) {
JPushModule.notifyJSDidLoad((resultCode) => {
if (resultCode === 0) {
console.log(resultCode)
}
});
}
// 接收自定義消息
JPushModule.addReceiveCustomMsgListener((message) => {
console.log("receive notification: " + message);
});
//應(yīng)用沒(méi)有啟動(dòng)情況 ;
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 以下的系統(tǒng)點(diǎn)擊推送回調(diào);
JPushModule.addReceiveNotificationListener(message => {
if (launchedApp) {
return launchedApp = false
}
console.log("接收到消息=====>", message,Platform.OS == "ios" && Platform.Version.split(".")[0] > 10,Platform.Version);
//處理iOS10以下APP打開(kāi)狀態(tài)接收推送消息的狀態(tài);
if (Platform.OS == "ios" && message.localNotice && AppState.currentState == "active") {
return false
}
//解決APP打開(kāi)狀態(tài)接收到消息不增加消息徽標(biāo)
if (Platform.OS == "ios" && Platform.Version.split(".")[0] >= 10) {
//因?yàn)閕OS10以下APP打開(kāi)狀態(tài)時(shí)接收到消息但是不顯示在通知欄所以暫時(shí)先這樣處理;
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}
})
}
});
//點(diǎn)擊推送消息打開(kāi)應(yīng)用回調(diào)
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);
});
}
});
}
});
}
//極光統(tǒng)計(jì);
initJAnalytics() {
if (isIos) {
JAnalyticsModule.setup({appKey: "申請(qǐng)的APPKey"});
JAnalyticsModule.setDebug({enable: true});
JAnalyticsModule.crashLogON();
console.log("極光分析----");
}
}
//jpush綁定設(shè)備到alias
jpushSetAlias(user_id) {
console.log("jpush當(dāng)前用戶的登錄信息", user_id, typeof user_id);
JPushModule.setAlias(user_id, (map) => {
console.log("jpush當(dāng)前用戶的登錄信息", map);
if (map.errorCode === 0) {
console.log("jpush-android注冊(cè)成功", map);
AsyncStorage.setItem("alias", map.alias)
} else {
console.log("jpushios注冊(cè)成功", 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;
//防止在登錄或者其他入口使用時(shí)沒(méi)有綁定到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內(nèi)1", user_id, alias);
that.jpushDeteleAlias();
}
}
if (!_.isEmpty(user_id)) {
if (_.isEmpty(alias)) {
console.log("jpush-root內(nèi)2", user_id, alias);
that.jpushSetAlias(user_id);
}
}
})
});
}
componentWillUnmount() {
//取消;
JPushModule.removeReceiveCustomMsgListener();
JPushModule.removeReceiveNotificationListener();
JPushModule.removeOpenNotificationLaunchAppEventListener();
}
以上幾個(gè)方法添加到指定的業(yè)務(wù)需求即可使用,想要擴(kuò)展業(yè)務(wù)需求還需再進(jìn)行其他配置;
如有問(wèn)題還需諒解;本人一點(diǎn)拙見(jiàn)。哈哈
「本文系極光征文 | 寫(xiě)寫(xiě)文章就能贏 Filco,豈不美滋滋的參賽文章」