刘志礼 發表於 2019-7-2 21:30:00

React Native发送通知

<p>一、使用第三方库做本地/远程消息推送</p>
<p>推荐:https://github.com/zo0r/react-native-push-notification</p>
<p>demo解析:</p>
<p>AndroidManifest.xml:配置基本权限</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">manifest </span><span style="color: rgba(255, 0, 0, 1)">xmlns:android</span><span style="color: rgba(0, 0, 255, 1)">="http://schemas.android.com/apk/res/android"</span><span style="color: rgba(255, 0, 0, 1)">
    package</span><span style="color: rgba(0, 0, 255, 1)">="com.example"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">uses-permission </span><span style="color: rgba(255, 0, 0, 1)">android:name</span><span style="color: rgba(0, 0, 255, 1)">="android.permission.INTERNET"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">uses-permission </span><span style="color: rgba(255, 0, 0, 1)">android:name</span><span style="color: rgba(0, 0, 255, 1)">="android.permission.SYSTEM_ALERT_WINDOW"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>

    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">application
      </span><span style="color: rgba(255, 0, 0, 1)">android:name</span><span style="color: rgba(0, 0, 255, 1)">=".MainApplication"</span><span style="color: rgba(255, 0, 0, 1)">
      android:label</span><span style="color: rgba(0, 0, 255, 1)">="@string/app_name"</span><span style="color: rgba(255, 0, 0, 1)">
      android:icon</span><span style="color: rgba(0, 0, 255, 1)">="@mipmap/ic_launcher"</span><span style="color: rgba(255, 0, 0, 1)">
      android:allowBackup</span><span style="color: rgba(0, 0, 255, 1)">="false"</span><span style="color: rgba(255, 0, 0, 1)">
      android:theme</span><span style="color: rgba(0, 0, 255, 1)">="@style/AppTheme"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">activity
      </span><span style="color: rgba(255, 0, 0, 1)">android:name</span><span style="color: rgba(0, 0, 255, 1)">=".MainActivity"</span><span style="color: rgba(255, 0, 0, 1)">
      android:label</span><span style="color: rgba(0, 0, 255, 1)">="@string/app_name"</span><span style="color: rgba(255, 0, 0, 1)">
      android:configChanges</span><span style="color: rgba(0, 0, 255, 1)">="keyboard|keyboardHidden|orientation|screenSize"</span><span style="color: rgba(255, 0, 0, 1)">
      android:windowSoftInputMode</span><span style="color: rgba(0, 0, 255, 1)">="adjustResize"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">intent-filter</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">action </span><span style="color: rgba(255, 0, 0, 1)">android:name</span><span style="color: rgba(0, 0, 255, 1)">="android.intent.action.MAIN"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">category </span><span style="color: rgba(255, 0, 0, 1)">android:name</span><span style="color: rgba(0, 0, 255, 1)">="android.intent.category.LAUNCHER"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">intent-filter</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">activity</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">activity </span><span style="color: rgba(255, 0, 0, 1)">android:name</span><span style="color: rgba(0, 0, 255, 1)">="com.facebook.react.devsupport.DevSettingsActivity"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>

       <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">receiver
            </span><span style="color: rgba(255, 0, 0, 1)">android:name</span><span style="color: rgba(0, 0, 255, 1)">="com.google.android.gms.gcm.GcmReceiver"</span><span style="color: rgba(255, 0, 0, 1)">
            android:exported</span><span style="color: rgba(0, 0, 255, 1)">="true"</span><span style="color: rgba(255, 0, 0, 1)">
            android:permission</span><span style="color: rgba(0, 0, 255, 1)">="com.google.android.c2dm.permission.SEND"</span> <span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">intent-filter</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">action </span><span style="color: rgba(255, 0, 0, 1)">android:name</span><span style="color: rgba(0, 0, 255, 1)">="com.google.android.c2dm.intent.RECEIVE"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
                <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">category </span><span style="color: rgba(255, 0, 0, 1)">android:name</span><span style="color: rgba(0, 0, 255, 1)">="${applicationId}"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">intent-filter</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">receiver</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">receiver </span><span style="color: rgba(255, 0, 0, 1)">android:name</span><span style="color: rgba(0, 0, 255, 1)">="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">receiver </span><span style="color: rgba(255, 0, 0, 1)">android:name</span><span style="color: rgba(0, 0, 255, 1)">="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">intent-filter</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">action </span><span style="color: rgba(255, 0, 0, 1)">android:name</span><span style="color: rgba(0, 0, 255, 1)">="android.intent.action.BOOT_COMPLETED"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">intent-filter</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">receiver</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">service </span><span style="color: rgba(255, 0, 0, 1)">android:name</span><span style="color: rgba(0, 0, 255, 1)">="com.dieam.reactnativepushnotification.modules.RNPushNotificationRegistrationService"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">service
            </span><span style="color: rgba(255, 0, 0, 1)">android:name</span><span style="color: rgba(0, 0, 255, 1)">="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService"</span><span style="color: rgba(255, 0, 0, 1)">
            android:exported</span><span style="color: rgba(0, 0, 255, 1)">="false"</span> <span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">intent-filter</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">action </span><span style="color: rgba(255, 0, 0, 1)">android:name</span><span style="color: rgba(0, 0, 255, 1)">="com.google.android.c2dm.intent.RECEIVE"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">intent-filter</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">service</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>


      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">meta-data</span><span style="color: rgba(255, 0, 0, 1)">android:name</span><span style="color: rgba(0, 0, 255, 1)">="com.dieam.reactnativepushnotification.notification_channel_name"</span><span style="color: rgba(255, 0, 0, 1)">
                  android:value</span><span style="color: rgba(0, 0, 255, 1)">="Example-Channel"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">meta-data</span><span style="color: rgba(255, 0, 0, 1)">android:name</span><span style="color: rgba(0, 0, 255, 1)">="com.dieam.reactnativepushnotification.notification_channel_description"</span><span style="color: rgba(255, 0, 0, 1)">
                  android:value</span><span style="color: rgba(0, 0, 255, 1)">="Super channel description"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">meta-data</span><span style="color: rgba(255, 0, 0, 1)">android:name</span><span style="color: rgba(0, 0, 255, 1)">="com.dieam.reactnativepushnotification.notification_color"</span><span style="color: rgba(255, 0, 0, 1)">
                  android:resource</span><span style="color: rgba(0, 0, 255, 1)">="@android:color/white"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">application</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>


    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">uses-permission </span><span style="color: rgba(255, 0, 0, 1)">android:name</span><span style="color: rgba(0, 0, 255, 1)">="android.permission.WAKE_LOCK"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">permission
      </span><span style="color: rgba(255, 0, 0, 1)">android:name</span><span style="color: rgba(0, 0, 255, 1)">="${applicationId}.permission.C2D_MESSAGE"</span><span style="color: rgba(255, 0, 0, 1)">
      android:protectionLevel</span><span style="color: rgba(0, 0, 255, 1)">="signature"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">uses-permission </span><span style="color: rgba(255, 0, 0, 1)">android:name</span><span style="color: rgba(0, 0, 255, 1)">="${applicationId}.permission.C2D_MESSAGE"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>


    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">uses-permission </span><span style="color: rgba(255, 0, 0, 1)">android:name</span><span style="color: rgba(0, 0, 255, 1)">="android.permission.VIBRATE"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">uses-permission </span><span style="color: rgba(255, 0, 0, 1)">android:name</span><span style="color: rgba(0, 0, 255, 1)">="android.permission.RECEIVE_BOOT_COMPLETED"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">manifest</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<p>NotifService.js:配置各类推送的消息显示</p>
<div class="cnblogs_code">
<pre>import PushNotification from 'react-native-push-notification'<span style="color: rgba(0, 0, 0, 1)">;

export </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)"> class NotifService {

constructor(onRegister, onNotification) {
    </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.configure(onRegister, onNotification);

    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.lastId = 0<span style="color: rgba(0, 0, 0, 1)">;
}

configure(onRegister, onNotification, gcm </span>= ""<span style="color: rgba(0, 0, 0, 1)">) {
    PushNotification.configure({
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (optional) Called when Token is generated (iOS and Android)</span>
      onRegister: onRegister, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">this._onRegister.bind(this),</span>

      <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (required) Called when a remote or local notification is opened or received</span>
      onNotification: onNotification, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">this._onNotification,</span>

      <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> ANDROID ONLY: GCM Sender ID (optional - not required for local notifications, but is need to receive remote push notifications)</span>
<span style="color: rgba(0, 0, 0, 1)">      senderID: gcm,

      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> IOS ONLY (optional): default: all - Permissions to register.</span>
<span style="color: rgba(0, 0, 0, 1)">      permissions: {
      alert: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
      badge: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
      sound: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">
      },

      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Should the initial notification be popped automatically</span>
      <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> default: true</span>
      popInitialNotification: <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,

      </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
      * (optional) default: true
      * - Specified if permissions (ios) and token (android and ios) will requested or not,
      * - if not, you must call PushNotificationsHandler.requestPermissions() later
      </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
      requestPermissions: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
    });
}

localNotif() {
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.lastId++<span style="color: rgba(0, 0, 0, 1)">;
    PushNotification.localNotification({
      </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> Android Only Properties </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
      id: </span>''+<span style="color: rgba(0, 0, 255, 1)">this</span>.lastId, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID</span>
      ticker: "My Notification Ticker", <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (optional)</span>
      autoCancel: <span style="color: rgba(0, 0, 255, 1)">true</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (optional) default: true</span>
      largeIcon: "ic_launcher", <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (optional) default: "ic_launcher"</span>
      smallIcon: "ic_notification", <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (optional) default: "ic_notification" with fallback for "ic_launcher"</span>
      bigText: "My big text that will be shown when notification is expanded", <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (optional) default: "message" prop</span>
      subText: "This is a subText", <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (optional) default: none</span>
      color: "red", <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (optional) default: system default</span>
      vibrate: <span style="color: rgba(0, 0, 255, 1)">true</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (optional) default: true</span>
      vibration: 300, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> vibration length in milliseconds, ignored if vibrate=false, default: 1000</span>
      tag: 'some_tag', <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (optional) add tag to message</span>
      group: "group", <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (optional) add group to message</span>
      ongoing: <span style="color: rgba(0, 0, 255, 1)">false</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (optional) set whether this is an "ongoing" notification</span>

      <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> iOS only properties </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
      alertAction: </span>'view', <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (optional) default: view</span>
      category: <span style="color: rgba(0, 0, 255, 1)">null</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (optional) default: null</span>
      userInfo: <span style="color: rgba(0, 0, 255, 1)">null</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (optional) default: null (object containing additional notification data)</span>

      <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> iOS and Android properties </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
      title: </span>"Local Notification", <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (optional)</span>
      message: "My Notification Message", <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (required)</span>
      playSound: <span style="color: rgba(0, 0, 255, 1)">false</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (optional) default: true</span>
      soundName: 'default', <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (optional) Sound to play when the notification is shown. Value of 'default' plays the default sound. It can be set to a custom sound such as 'android.resource://com.xyz/raw/my_sound'. It will look for the 'my_sound' audio file in 'res/raw' directory and play it. default: 'default' (default sound is played)</span>
      number: '10', <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (optional) Valid 32 bit integer specified as string. default: none (Cannot be zero)</span>
      actions: '["Yes", "No"]',<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (Android only) See the doc for notification actions to know more</span>
<span style="color: rgba(0, 0, 0, 1)">    });
}

scheduleNotif() {
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.lastId++<span style="color: rgba(0, 0, 0, 1)">;
    PushNotification.localNotificationSchedule({
      date: </span><span style="color: rgba(0, 0, 255, 1)">new</span> Date(Date.now() + (30 * 1000)), <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> in 30 secs</span>

      <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> Android Only Properties </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
      id: </span>''+<span style="color: rgba(0, 0, 255, 1)">this</span>.lastId, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID</span>
      ticker: "My Notification Ticker", <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (optional)</span>
      autoCancel: <span style="color: rgba(0, 0, 255, 1)">true</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (optional) default: true</span>
      largeIcon: "ic_launcher", <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (optional) default: "ic_launcher"</span>
      smallIcon: "ic_notification", <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (optional) default: "ic_notification" with fallback for "ic_launcher"</span>
      bigText: "My big text that will be shown when notification is expanded", <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (optional) default: "message" prop</span>
      subText: "This is a subText", <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (optional) default: none</span>
      color: "blue", <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (optional) default: system default</span>
      vibrate: <span style="color: rgba(0, 0, 255, 1)">true</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (optional) default: true</span>
      vibration: 300, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> vibration length in milliseconds, ignored if vibrate=false, default: 1000</span>
      tag: 'some_tag', <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (optional) add tag to message</span>
      group: "group", <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (optional) add group to message</span>
      ongoing: <span style="color: rgba(0, 0, 255, 1)">false</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (optional) set whether this is an "ongoing" notification</span>

      <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> iOS only properties </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
      alertAction: </span>'view', <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (optional) default: view</span>
      category: <span style="color: rgba(0, 0, 255, 1)">null</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (optional) default: null</span>
      userInfo: <span style="color: rgba(0, 0, 255, 1)">null</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (optional) default: null (object containing additional notification data)</span>

      <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> iOS and Android properties </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
      title: </span>"Scheduled Notification", <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (optional)</span>
      message: "My Notification Message", <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (required)</span>
      playSound: <span style="color: rgba(0, 0, 255, 1)">true</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (optional) default: true</span>
      soundName: 'default', <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (optional) Sound to play when the notification is shown. Value of 'default' plays the default sound. It can be set to a custom sound such as 'android.resource://com.xyz/raw/my_sound'. It will look for the 'my_sound' audio file in 'res/raw' directory and play it. default: 'default' (default sound is played)</span>
<span style="color: rgba(0, 0, 0, 1)">    });
}

checkPermission(cbk) {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> PushNotification.checkPermissions(cbk);
}

cancelNotif() {
    PushNotification.cancelLocalNotifications({id: </span>''+<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.lastId});
}

cancelAll() {
    PushNotification.cancelAllLocalNotifications();
}
}</span></pre>
</div>
<p>App.js:消息显示</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow
</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">

import React, { Component } from </span>'react'<span style="color: rgba(0, 0, 0, 1)">;
import { TextInput, StyleSheet, Text, View, TouchableOpacity, Alert } from </span>'react-native'<span style="color: rgba(0, 0, 0, 1)">;
import NotifService from </span>'./NotifService'<span style="color: rgba(0, 0, 0, 1)">;
import appConfig from </span>'./app.json'<span style="color: rgba(0, 0, 0, 1)">;

type Props </span>=<span style="color: rgba(0, 0, 0, 1)"> {};
export </span><span style="color: rgba(0, 0, 255, 1)">default</span> class App extends Component&lt;Props&gt;<span style="color: rgba(0, 0, 0, 1)"> {

constructor(props) {
    super(props);
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.state =<span style="color: rgba(0, 0, 0, 1)"> {
      senderId: appConfig.senderID
    };

    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.notif = <span style="color: rgba(0, 0, 255, 1)">new</span> NotifService(<span style="color: rgba(0, 0, 255, 1)">this</span>.onRegister.bind(<span style="color: rgba(0, 0, 255, 1)">this</span>), <span style="color: rgba(0, 0, 255, 1)">this</span>.onNotif.bind(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">));
}

render() {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
      </span>&lt;View style={styles.container}&gt;
      &lt;Text style={styles.title}&gt;Example app react-native-push-notification&lt;/Text&gt;
      &lt;View style={styles.spacer}&gt;&lt;/View&gt;
      &lt;TextInput style={styles.textField} value={<span style="color: rgba(0, 0, 255, 1)">this</span>.state.registerToken} placeholder="Register token" /&gt;
      &lt;View style={styles.spacer}&gt;&lt;/View&gt;

      &lt;TouchableOpacity style={styles.button} onPress={() =&gt; { <span style="color: rgba(0, 0, 255, 1)">this</span>.notif.localNotif() }}&gt;&lt;Text&gt;Local Notification (now)&lt;/Text&gt;&lt;/TouchableOpacity&gt;
      &lt;TouchableOpacity style={styles.button} onPress={() =&gt; { <span style="color: rgba(0, 0, 255, 1)">this</span>.notif.scheduleNotif() }}&gt;&lt;Text&gt;Schedule Notification <span style="color: rgba(0, 0, 255, 1)">in</span> 30s&lt;/Text&gt;&lt;/TouchableOpacity&gt;
      &lt;TouchableOpacity style={styles.button} onPress={() =&gt; { <span style="color: rgba(0, 0, 255, 1)">this</span>.notif.cancelNotif() }}&gt;&lt;Text&gt;Cancel last notification (<span style="color: rgba(0, 0, 255, 1)">if</span> any)&lt;/Text&gt;&lt;/TouchableOpacity&gt;
      &lt;TouchableOpacity style={styles.button} onPress={() =&gt; { <span style="color: rgba(0, 0, 255, 1)">this</span>.notif.cancelAll() }}&gt;&lt;Text&gt;Cancel all notifications&lt;/Text&gt;&lt;/TouchableOpacity&gt;
      &lt;TouchableOpacity style={styles.button} onPress={() =&gt; { <span style="color: rgba(0, 0, 255, 1)">this</span>.notif.checkPermission(<span style="color: rgba(0, 0, 255, 1)">this</span>.handlePerm.bind(<span style="color: rgba(0, 0, 255, 1)">this</span>)) }}&gt;&lt;Text&gt;Check Permission&lt;/Text&gt;&lt;/TouchableOpacity&gt;

      &lt;View style={styles.spacer}&gt;&lt;/View&gt;
      &lt;TextInput style={styles.textField} value={<span style="color: rgba(0, 0, 255, 1)">this</span>.state.senderId} onChangeText={(e) =&gt; {<span style="color: rgba(0, 0, 255, 1)">this</span>.setState({ senderId: e })}} placeholder="GCM ID" /&gt;
      &lt;TouchableOpacity style={styles.button} onPress={() =&gt; { <span style="color: rgba(0, 0, 255, 1)">this</span>.notif.configure(<span style="color: rgba(0, 0, 255, 1)">this</span>.onRegister.bind(<span style="color: rgba(0, 0, 255, 1)">this</span>), <span style="color: rgba(0, 0, 255, 1)">this</span>.onNotif.bind(<span style="color: rgba(0, 0, 255, 1)">this</span>), <span style="color: rgba(0, 0, 255, 1)">this</span>.state.senderId) }}&gt;&lt;Text&gt;Configure Sender ID&lt;/Text&gt;&lt;/TouchableOpacity&gt;<span style="color: rgba(0, 0, 0, 1)">
      {</span><span style="color: rgba(0, 0, 255, 1)">this</span>.state.gcmRegistered &amp;&amp; &lt;Text&gt;GCM Configured !&lt;/Text&gt;}

      &lt;View style={styles.spacer}&gt;&lt;/View&gt;
      &lt;/View&gt;
<span style="color: rgba(0, 0, 0, 1)">    );
}

onRegister(token) {
    Alert.alert(</span>"Registered !"<span style="color: rgba(0, 0, 0, 1)">, JSON.stringify(token));
    console.log(token);
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.setState({ registerToken: token.token, gcmRegistered: <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)"> });
}

onNotif(notif) {
    console.log(notif);
    Alert.alert(notif.title, notif.message);
}

handlePerm(perms) {
    Alert.alert(</span>"Permissions"<span style="color: rgba(0, 0, 0, 1)">, JSON.stringify(perms));
}
}


const styles </span>=<span style="color: rgba(0, 0, 0, 1)"> StyleSheet.create({
container: {
    flex: </span>1<span style="color: rgba(0, 0, 0, 1)">,
    justifyContent: </span>'center'<span style="color: rgba(0, 0, 0, 1)">,
    alignItems: </span>'center'<span style="color: rgba(0, 0, 0, 1)">,
    backgroundColor: </span>'#F5FCFF'<span style="color: rgba(0, 0, 0, 1)">,
},
welcome: {
    fontSize: </span>20<span style="color: rgba(0, 0, 0, 1)">,
    textAlign: </span>'center'<span style="color: rgba(0, 0, 0, 1)">,
    margin: </span>10<span style="color: rgba(0, 0, 0, 1)">,
},
button: {
    borderWidth: </span>1<span style="color: rgba(0, 0, 0, 1)">,
    borderColor: </span>"#000000"<span style="color: rgba(0, 0, 0, 1)">,
    margin: </span>5<span style="color: rgba(0, 0, 0, 1)">,
    padding: </span>5<span style="color: rgba(0, 0, 0, 1)">,
    width: </span>"70%"<span style="color: rgba(0, 0, 0, 1)">,
    backgroundColor: </span>"#DDDDDD"<span style="color: rgba(0, 0, 0, 1)">,
    borderRadius: </span>5<span style="color: rgba(0, 0, 0, 1)">,
},
textField: {
    borderWidth: </span>1<span style="color: rgba(0, 0, 0, 1)">,
    borderColor: </span>"#AAAAAA"<span style="color: rgba(0, 0, 0, 1)">,
    margin: </span>5<span style="color: rgba(0, 0, 0, 1)">,
    padding: </span>5<span style="color: rgba(0, 0, 0, 1)">,
    width: </span>"70%"<span style="color: rgba(0, 0, 0, 1)">
},
spacer: {
    height: </span>10<span style="color: rgba(0, 0, 0, 1)">,
},
title: {
    fontWeight: </span>"bold"<span style="color: rgba(0, 0, 0, 1)">,
    fontSize: </span>20<span style="color: rgba(0, 0, 0, 1)">,
    textAlign: </span>"center"<span style="color: rgba(0, 0, 0, 1)">,
}
});</span></pre>
</div>
<p>最终效果:</p>
<p><img src="https://img2018.cnblogs.com/blog/1549437/201907/1549437-20190702210934792-2045505488.png"></p>
<p>&nbsp;</p>
<p>二、使用极光开发者服务做远程消息推送</p>
<p>1.官网注册极光开发者账号,并创建应用<img src="https://img2018.cnblogs.com/blog/1549437/201907/1549437-20190702211959927-1946720837.png"></p>
<p><img src="https://img2018.cnblogs.com/blog/1549437/201907/1549437-20190702212113386-1610480464.png"></p>
<p>2.安装</p>
<div class="cnblogs_code">
<pre>npm install jpush-react-native --<span style="color: rgba(0, 0, 0, 1)">save
npm install jcore</span>-react-native --save</pre>
</div>
<p>3.关联</p>
<div class="cnblogs_code">
<pre>react-native link jpush-react-<span style="color: rgba(0, 0, 0, 1)">native
react</span>-native link jcore-react-native</pre>
</div>
<p>4.打开 project/android/app/src/main/java/com/项目名/下的 MainApplication.java 文件,然后加入 JPushPackage</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">import cn.jpush.reactnativejpush.JPushPackage;

public class MainApplication extends Application implements ReactApplication {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设置为 true 将不会弹出 toast</span>
    private <span style="color: rgba(0, 0, 255, 1)">boolean</span> SHUTDOWN_TOAST = <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设置为 true 将不会打印 log</span>
    private <span style="color: rgba(0, 0, 255, 1)">boolean</span> SHUTDOWN_LOG = <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
private final ReactNativeHost mReactNativeHost </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> ReactNativeHost(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">) {
    @Override
    public </span><span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> getUseDeveloperSupport() {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> BuildConfig.DEBUG;
    }

    @Override
    protected List</span>&lt;ReactPackage&gt;<span style="color: rgba(0, 0, 0, 1)"> getPackages() {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> Arrays.&lt;ReactPackage&gt;<span style="color: rgba(0, 0, 0, 1)">asList(
            </span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> MainReactPackage(),
               </span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> JPushPackage(SHUTDOWN_TOAST, SHUTDOWN_LOG)
      );
    }</span></pre>
</div>
<p>5.打开&nbsp;project/android/app/src/main/java/com/项目名/下的MainActivity.java 文件,然后加入 如下代码:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">import android.os.Bundle;
import com.facebook.react.ReactActivity;
import cn.jpush.android.api.JPushInterface;

public class MainActivity extends ReactActivity {

    @Override
    protected </span><span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      JPushInterface.init(</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">);
    }

    @Override
    protected </span><span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onPause() {
      super.onPause();
      JPushInterface.onPause(</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">);
    }

    @Override
    protected </span><span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onResume() {
      super.onResume();
      JPushInterface.onResume(</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">);
    }

    @Override
    protected </span><span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onDestroy() {
      super.onDestroy();
    }
}</span></pre>
</div>
<p>&nbsp;6.使用</p>
<p>(1)RN工程代码:</p>
<div class="cnblogs_code">
<pre>import React, { Component } from "react"<span style="color: rgba(0, 0, 0, 1)">;
import { Dimensions, Text, Platform, Linking, Alert } from </span>"react-native"<span style="color: rgba(0, 0, 0, 1)">;
import JPushModule from </span>'jpush-react-native'<span style="color: rgba(0, 0, 0, 1)">

const deviceHeight </span>= Dimensions.get('window'<span style="color: rgba(0, 0, 0, 1)">).height;

export </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)"> class Login extends Component {
    constructor(props){
      super(props);
      </span><span style="color: rgba(0, 0, 255, 1)">this</span>.state =<span style="color: rgba(0, 0, 0, 1)"> {
            
      }
    }

    componentDidMount() {
    </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">***************************通知 start *************************************************</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
      <span style="color: rgba(0, 0, 255, 1)">if</span> (Platform.OS === 'android'<span style="color: rgba(0, 0, 0, 1)">) {
          JPushModule.initPush()
       </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 新版本必需写回调函数</span>
          JPushModule.notifyJSDidLoad(resultCode =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> (resultCode === 0<span style="color: rgba(0, 0, 0, 1)">) {
            }
          })
      } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
          JPushModule.setupPush()
      }
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 接收自定义消息</span>
      <span style="color: rgba(0, 0, 255, 1)">this</span>.receiveCustomMsgListener = map =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
          </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.setState({
            pushMsg: map.content
          })
          console.log(</span>'extras: ' +<span style="color: rgba(0, 0, 0, 1)"> map.extras)
      }

      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 接收自定义消息JPushModule.addReceiveCustomMsgListener(this.receiveCustomMsgListener)</span>
      <span style="color: rgba(0, 0, 255, 1)">this</span>.receiveNotificationListener = map =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
          console.log(</span>'alertContent: ' +<span style="color: rgba(0, 0, 0, 1)"> map.alertContent)
          console.log(</span>'extras: ' +<span style="color: rgba(0, 0, 0, 1)"> map.extras)
      }
      
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 接收推送通知</span>
      JPushModule.addReceiveNotificationListener(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.receiveNotificationListener)
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 打开通知</span>
      <span style="color: rgba(0, 0, 255, 1)">this</span>.openNotificationListener = map =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
          </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> console.log('Opening notification!')</span>
      <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">console.log('map.extra: ' + map.extras)</span>
          let webUrl=<span style="color: rgba(0, 0, 0, 1)"> JSON.parse(map.extras).webUrl
          let url </span>= webUrl.replace(<span style="color: rgba(0, 0, 255, 1)">new</span> RegExp("\/", 'g'), "/"<span style="color: rgba(0, 0, 0, 1)">)
          Linking.canOpenURL(url).then(supported </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">supported) {
            Alert.alert(</span>'您的系统不支持打开浏览器!'<span style="color: rgba(0, 0, 0, 1)">)
            } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> Linking.openURL(url);
            }
          }).</span><span style="color: rgba(0, 0, 255, 1)">catch</span>(err =&gt;<span style="color: rgba(0, 0, 0, 1)"> { });

      }

      JPushModule.addReceiveOpenNotificationListener(</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.openNotificationListener)

      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> this.getRegistrationIdListener = registrationId =&gt; {</span>
      <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">   console.log('Device register succeed, registrationId ' + registrationId)</span>
      <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> }</span>
      <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> JPushModule.addGetRegistrationIdListener(this.getRegistrationIdListener)</span>
      <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">***************************通知 end *************************************************</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
      }

    componentWillUnmount() {
      JPushModule.removeReceiveCustomMsgListener(</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.receiveCustomMsgListener)
      JPushModule.removeReceiveNotificationListener(</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.receiveNotificationListener)
      JPushModule.removeReceiveOpenNotificationListener(</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.openNotificationListener)
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> JPushModule.removeGetRegistrationIdListener(this.getRegistrationIdListener)</span>
      <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> console.log('Will clear all notifications')</span>
      <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> JPushModule.clearAllNotifications()</span>
<span style="color: rgba(0, 0, 0, 1)">    }
      
   

    render() {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
          </span>&lt;Text&gt;push notification test&lt;/Text&gt;
<span style="color: rgba(0, 0, 0, 1)">      );
    }
}</span></pre>
</div>
<p>(2)极光官网推送</p>
<p><img src="https://img2018.cnblogs.com/blog/1549437/201907/1549437-20190702212400113-1582534285.png"></p>
<p>&nbsp;</p>
<p>7.最终效果</p>
<p>&nbsp;<img src="https://img2018.cnblogs.com/blog/1549437/201907/1549437-20190702212603775-2044602527.png"></p>
<p>三、APP内消息通知</p>
<p>使用antd组件NoticeBar通告栏:https://rn.mobile.ant.design/components/notice-bar-cn/&nbsp;即可</p>
<p>最终效果:</p>
<p><img src="https://img2018.cnblogs.com/blog/1549437/201907/1549437-20190702212720490-1019567485.png"></p>
<p>&nbsp;</p>
<p>参考:React Native之通知栏消息提示(android)&nbsp; 作者:jackson影琪</p><br><br>
来源:https://www.cnblogs.com/bbcfive/p/11123274.html
頁: [1]
查看完整版本: React Native发送通知