個(gè)人感覺阿里云推送比極光推送要好配置些(有免費(fèi)5萬(wàn)條的推送哦,感覺像是打廣告哈哈~),需要的可以看下??
react-native-aliyun-push需要注冊(cè)(請(qǐng)參考阿里云移動(dòng)推送文檔)
安裝
npm install react-native-aliyun-push --save
react-native link react-native-aliyun-push
android配置
- 在android根目錄下build.gradle配置以下代碼:
allprojects {
repositories {
mavenLocal()
jcenter()
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url "$rootDir/../node_modules/react-native/android"
}
// 下面是添加的代碼
maven {
url "http://maven.aliyun.com/nexus/content/repositories/releases/"
}
flatDir {
dirs project(':react-native-aliyun-push').file('libs')
}
// 添加結(jié)束
}
}
- 在settings.gradle中添加以下代碼:
include ':react-native-aliyun-push'
project(':react-native-aliyun-push').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-aliyun-push/android')
- 在app/build.gradle中添加以下代碼:
dependencies {
//下面是被添加的代碼
compile project(':react-native-aliyun-push')
//添加結(jié)束
}
4.在AndroidManifest.xml文件中的 application 標(biāo)簽下添加以下代碼:
<meta-data android:name="com.alibaba.app.appkey" android:value="********"/> <!-- 你自己的阿里云- appKey -->
<meta-data android:name="com.alibaba.app.appsecret" android:value="********"/> <!-- 你自己的阿里云-appSecret -->
- 在MainApplication.java中被添加以下代碼
// 下面是被添加的代碼
import org.wonday.aliyun.push.AliyunPushPackage;(link 以后一般是已經(jīng)有的了)
import com.alibaba.sdk.android.push.CloudPushService;
import com.alibaba.sdk.android.push.CommonCallback;
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory;
import com.alibaba.sdk.android.push.register.HuaWeiRegister;
import com.alibaba.sdk.android.push.register.MiPushRegister;
// 添加結(jié)束
...
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
//下面是被添加的代碼
new AliyunPushPackage() (link 以后一般是已經(jīng)有的了)
//添加結(jié)束
);
}
};
@Override
public void onCreate() {
super.onCreate();
SoLoader.init(this, /* native exopackage */ false);
//下面是添加的代碼
this.initCloudChannel();
//添加結(jié)束
}
// 下面是添加的代碼
/**
* 初始化阿里云推送通道
* @param applicationContext
*/
private void initCloudChannel() {
PushServiceFactory.init(this.getApplicationContext());
CloudPushService pushService = PushServiceFactory.getCloudPushService();
pushService.setNotificationSmallIcon(R.mipmap.ic_launcher_s);//設(shè)置通知欄小圖標(biāo)妄讯, 需要自行添加
pushService.register(this.getApplicationContext(), "你自己的阿里云appKey", "你自己的阿里云appSecret", new CommonCallback() {
@Override
public void onSuccess(String responnse) {
// success
}
@Override
public void onFailed(String code, String message) {
// failed
}
});
// 注冊(cè)方法會(huì)自動(dòng)判斷是否支持小米系統(tǒng)推送,如不支持會(huì)跳過(guò)注冊(cè)伏社。
MiPushRegister.register(this.getApplicationContext(), "小米AppID", "小米AppKey");
// 注冊(cè)方法會(huì)自動(dòng)判斷是否支持華為系統(tǒng)推送每辟,如不支持會(huì)跳過(guò)注冊(cè)。
HuaWeiRegister.register(this.getApplicationContext());
}
// 添加結(jié)束
ios配置
添加node_modules/react-native-aliyun-push/ios/RCTAliyunPush.xcodeproj到xcode項(xiàng)目工程
添加阿里云移動(dòng)推送SDK
拖拽node_modules/react-native-aliyun-push/ios/libs下列目錄到xcode工程的frameworks
目錄下包斑,將copy items if needed
打勾流礁。 注意:從阿里云下載的SDK中UTDID.framework有問(wèn)題,編譯會(huì)報(bào)錯(cuò)罗丰,請(qǐng)使用react-native-aliyun-push中內(nèi)置的版本神帅。
請(qǐng)自行下載:https://gitee.com/cuic1/react-native-aliyun-push-master
- AlicloudUtils.framework
- CloudPushSDK.framework
- UTDID.framework
- UTMini.framework
- 點(diǎn)擊項(xiàng)目根節(jié)點(diǎn),在targets app的BuildPhase的Link Binary With Libraries中添加公共包依賴
- libz.tbd
- libresolv.tbd
- libsqlite3.tbd
- CoreTelephony.framework
- SystemConfiguration.framework
- UserNotifications.framework
同時(shí)確保targets app的BuildPhase的Link Binary With Libraries包含
- AlicloudUtils.framework
- CloudPushSDK.framework
- UTDID.framework
- UTMini.framework
- 修改AppDelegate.m添加如下代碼
#import "AliyunPushManager.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
// 下面是添加的代碼
[[AliyunPushManager sharedInstance] setParams:@"阿里云appKey"
appSecret:@"阿里云appSecret"
lauchOptions:launchOptions
createNotificationCategoryHandler:^{
//create customize notification category here
}];
// 添加結(jié)束
return YES;
}
// 下面是添加的代碼
// APNs注冊(cè)成功回調(diào)萌抵,將返回的deviceToken上傳到CloudPush服務(wù)器
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
[[AliyunPushManager sharedInstance] application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
// APNs注冊(cè)失敗回調(diào)
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
[[AliyunPushManager sharedInstance] application:application didFailToRegisterForRemoteNotificationsWithError:error];
}
// 打開/刪除通知回調(diào)
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
{
[[AliyunPushManager sharedInstance] application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}
// 請(qǐng)求注冊(cè)設(shè)定后找御,回調(diào)
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
[[AliyunPushManager sharedInstance] application:application didRegisterUserNotificationSettings:notificationSettings];
}
// 添加結(jié)束
@end
使用示例
引入模塊
import AliyunPush from 'react-native-aliyun-push';
監(jiān)聽推送事件
componentDidMount() {
//監(jiān)聽推送事件
AliyunPush.addListener(this.handleAliyunPushMessage);
}
componentWillUnmount() {
//
AliyunPush.removeListener(this.handleAliyunPushMessage);
}
handleAliyunPushMessage = (e) => {
console.log("Message Received. " + JSON.stringify(e));
//e結(jié)構(gòu)說(shuō)明:
//e.type: "notification":通知 或者 "message":消息
//e.title: 推送通知/消息標(biāo)題
//e.body: 推送通知/消息具體內(nèi)容
//e.actionIdentifier: "opened":用戶點(diǎn)擊了通知, "removed"用戶刪除了通知, 其他非空值:用戶點(diǎn)擊了自定義action(僅限ios)
//e.extras: 用戶附加的{key:value}的對(duì)象
};
寫完以后親測(cè)是沒(méi)有問(wèn)題的,所以小伙伴們出錯(cuò)過(guò)的绍填,多配置幾次??
覺得有用的小伙伴點(diǎn)個(gè)關(guān)注和小紅心就行??霎桅,么么噠。