react-native-pure-umeng-push

react native umeng push


Keywords
react, native, umeng push
License
MIT
Install
npm install react-native-pure-umeng-push@0.3.5

Documentation

react-native-pure-umeng-push

友盟推送

Installation

npm i react-native-pure-umeng-push
// link below 0.60
react-native link react-native-pure-umeng-push

Setup

iOS

打开推送开关。

打开后台推送权限设置

修改 AppDelegate.m,如下

#import <RNTUmengPush.h>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  ...
  [RNTUmengPush init:@"appKey" launchOptions:launchOptions debug:false];
  return YES;
}

- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  [RNTUmengPush didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  [RNTUmengPush didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}

Android

android/build.gradle add the umeng maven repo.

allprojects {
    repositories {
        // add this line
        maven { url 'https://dl.bintray.com/umsdk/release' }
    }
}

MainApplicationonCreate 方法进行初始化,如下:

override fun onCreate() {
    // 点击推送跳转到的 activity
    // 通常 react native 只有一个 main activity
    UmengPushActivity.mainActivityClass = MainActivity::class.java
    // 初始化
    RNTUmengPushModule.init(this, "appKey", "appSecret", "渠道名,如xiaomi/huawei", false)
    // 如果需要厂商通道,按需调用
    RNTUmengPushModule.huawei(this)
    RNTUmengPushModule.xiaomi(this, "appId", "appKey")
    RNTUmengPushModule.oppo(this, "appId", "appSecret")
    RNTUmengPushModule.vivo(this)
    RNTUmengPushModule.meizu(this, "appId", "appKey")
}

配置厂商通道请先阅读官方文档,主要是获取各个通道的 appIdappKeyappSecret 等数据,并保存到友盟后台的应用信息里,此外还需要进行一些额外配置,如下。

配置华为

AndroidManifest.xmlapplication 标签下配置华为的 appId,如下:

<meta-data
    android:name="com.huawei.hms.client.appid"
    android:value="你的华为应用的 app id"
/>

配置 vivo

AndroidManifest.xmlapplication 标签下配置 vivo 的 appIdappKey,如下:

<meta-data
    android:name="com.vivo.push.app_id"
    android:value="你的 vivo 应用的 app id"
/>
<meta-data
    android:name="com.vivo.push.api_key"
    android:value="你的 vivo 应用的 app key"
/>

配置魅族

配置系统通知图标

请在 drawable 目录下添加一个图标,命名为 stat_sys_third_app_notify.png,建议尺寸 64px * 64px,图标四周留有透明。若不添加此图标,可能在部分魅族手机上无法弹出通知。

Usage

import umengPush from 'react-native-pure-umeng-push'

umengPush.addListener(
  'register',
  function (data) {
    data.deviceToken
    // 如果 app 未启动状态下,点击推送打开 app,会有两个新字段
    // 点击的推送
    data.notification
    // 推送的自定义参数
    data.custom
  }
)

umengPush.addListener(
  'remoteNotification',
  function (data) {
    // 如果点击了推送,data.clicked 是 true
    data.clicked
    // 如果推送送达并展示了,data.presented 是 true
    data.presented

    // 推送详情,如标题、内容
    data.notification
    // 推送的自定义参数
    data.custom
  }
)

// 启动
umengPush.start()