@opendt/react-native-ali-push
v1.0.0
Published
react-native wrapper of Ali Push SDK, support both iOS and Android.
Downloads
2
Readme
React Native Ali Push
react-native wrapper of Ali Push SDK, support both iOS and Android.
Note, this library only support react-native 0.60+
Installation
Install library via npm:
npm install --save @opendt/react-native-ali-push
Additional steps
- Android
- add aliyun maven repository to project's
build.gradle
file:allprojects { repositories { + maven { + // Aliyun repositories + url 'http://maven.aliyun.com/nexus/content/repositories/releases/' + } } }
- specify your
appKey
andappSecret
inAndroidManifest.xml
:<application android:name="*****"> + <meta-data android:name="com.alibaba.app.appkey" android:value="PUSH_APP_KEY"/> + <meta-data android:name="com.alibaba.app.appsecret" android:value="PUSH_APP_SECRET"/> </application>
- update your
MainApplication.java
file:+ import xin.dayukeji.rn.alipush.DTAliPushManager; public class MainApplication extends Application implements ReactApplication { @Override public void onCreate() { super.onCreate(); SoLoader.init(this, /* native exopackage */ false); + DTAliPushManager.initCloudChannel(this); } }
- update your
MainActivity.java
file(or wherever you create an instance of ReactActivityDelegate):+ import xin.dayukeji.rn.alipush.DTAliPushManager; public class MainActivity extends ReactActivity { + @Override + protected ReactActivityDelegate createReactActivityDelegate() { + return new ReactActivityDelegate(this, getMainComponentName()) { + @Override + protected Bundle getLaunchOptions() { + Bundle initialProps = new Bundle(); + initialProps.putBundle("notification", DTAliPushManager.extractPushData(getIntent())); + return initialProps; + } + }; + } + } }
- iOS
- add Aliyun's Pod repository to your
Podfile
:
or, you can add global repo, run:source 'https://github.com/aliyun/aliyun-specs.git'
then you need to complete the linking on iOS, run:pod repo add aliyum https://github.com/aliyun/aliyun-specs.git master
cd ios && pod install
- update your
AppDelegate.m
file:+ #import "DTAliPush.h" - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // ... + [[DTAliPush sharedInstance] initWithKey:@"PUSH_APP_KEY" appSecret:@"PUSH_APP_SECRET" callback:nil]; + [[DTAliPush sharedInstance] registerAPNS:application]; + [[DTAliPush sharedInstance] handleNotification:launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]]; return YES; } + - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { + [[DTAliPush sharedInstance] registerDeviceToken:deviceToken withCallback:nil]; + } + // for iOS < 10 + - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { + [[DTAliPush sharedInstance] handleNotification:userInfo]; + }
- enable
Push Notifications
capability in Xcode, and config push certifications in Aliyun Console, see help doc
Usage
import package in your js files
import AliPush from '@opendt/react-native-ali-push';
AliPush.subscribe(event: string, callback: Function)
subscribe to SDK event. available event are:AliPush.subscribe(AliPush.EVT_NOTIFICATION, notification => { console.log('new notification', notification); });
- AliPush.EVT_NOTIFICATION: event indicated that new notification received.
- AliPush.EVT_NOTIFICATION_OPENED: user click the notification to open it.
- AliPush.EVT_NOTIFICATION_REMOVE: user dismissed the notification.
AliPush.bindAccount(uid: string): Promise
bind account to Ali Push serviceAliPush.unbindAccount(uid: string): Promise
unbind account, if you want to bind to another account, you can directly callbindAccount(anotherUid)
.AliPush.binfTag(tags: Array<string>, target, alias: string): Promise
bind tags to specified target, wheretarget
must be one ofTARGET_ACCOUNT
,TARGET_DEVICE
,TARGET_ALIAS
. if target isTARGET_ALIAS
, third paramalias
must be specified.AliPush.unbindTag(tags: Array<string>, target, alias: string): Promise
unbind tags from specified target.