1

我的应用程序仅在应用程序打开并且处于后台时才会收到通知。但我需要通知应达到甚至应用程序已关闭。我如何在reactnative中使用react-native-fcm来做到这一点?React-native-fcm不推送已关闭应用程序的通知

我的代码低于请参考它

FCM.getInitialNotification().then(notif=>console.log(notif)); 
      FCM.requestPermissions(); // for iOS 
      FCM.getFCMToken().then(token => { 
       console.log(token) 
       // store fcm token in your server 
      }); 
      this.notificationListener = FCM.on(FCMEvent.Notification, async (notif) => { 
       // there are two parts of notif. notif.notification contains the notification payload, notif.data contains data payload 
       if(notif.local_notification){ 
        //this is a local notification 
        return; 
       } 
       if(notif.opened_from_tray){ 
        //app is open/resumed because user clicked banner 
        console.log('clicked'); 

        return; 
       } 
       this.showLocalNotification(notif); 

      }); 
      this.refreshTokenListener = FCM.on(FCMEvent.RefreshToken, (token) => { 
       console.log(token) 
       // fcm token may not be available on first load, catch it here 
      }); 

     showLocalNotification(notif) { 
      FCM.presentLocalNotification({ 
      title: notif.title, 
      body: notif.body, 
      priority: "high", 
      click_action: notif.click_action, 
      show_in_foreground: true, 
      local: true 
      }); 
     } 

<uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-permission android:name="android.permission.VIBRATE"/> 
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> 

    <uses-sdk 
     android:minSdkVersion="16" 
     android:targetSdkVersion="22" /> 

    <application 
     android:name=".MainApplication" 
     android:allowBackup="true" 
     android:label="@string/app_name" 
     android:icon="@mipmap/ic_launcher" 
     android:theme="@style/AppTheme"> 

     <receiver android:name="com.evollu.react.fcm.FIRLocalMessagingPublisher"/> 
     <receiver android:enabled="true" android:exported="true" android:name="com.evollu.react.fcm.FIRSystemBootEventReceiver"> 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED"/> 
      <action android:name="android.intent.action.QUICKBOOT_POWERON"/> 
      <action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
     </receiver> 

     <service android:name="com.evollu.react.fcm.MessagingService" android:enabled="true" android:exported="true"> 
     <intent-filter> 
      <action android:name="com.google.firebase.MESSAGING_EVENT"/> 
     </intent-filter> 
     </service> 

     <service android:name="com.evollu.react.fcm.InstanceIdService" android:exported="false"> 
     <intent-filter> 
      <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> 
     </intent-filter> 
     </service> 

     <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" 
     android:screenOrientation="portrait" 
     android:configChanges="keyboard|keyboardHidden|orientation|screenSize" 
     android:windowSoftInputMode="adjustResize"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
     <intent-filter> 
      <action android:name="fcm.ACTION.HELLO" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
     </activity> 
     <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" /> 
     <meta-data 
     android:name="com.google.android.geo.API_KEY" 
     android:value="XXXXXX"/> 
    </application> 
+0

你有没有考虑过使用'data'- * only * payload消息? –

+0

我正在发送数据和通知。我是新来的。我只是使用react-native-fcm来满足我的需求。 –

+0

对不起。我很困惑。我认为当应用处于前景/背景时,您更愿意自己处理这些消息。这是Android的行为,当通过'data'和'notification'发送一个有效载荷时,Notification Tray将处理有效载荷。你没有看到任何通知?你有没有在多个设备上测试过? –

回答

3

你的问题是你如何推动您的通知。 FCM将识别并弹出仅包含属性“通知”的通知。

如果您需要的命令

this.showLocalNotification(通知符);

要显示您的通知,这意味着您正在本地管理通知,因此,您关闭的应用程序将无法管理它。

因此,请检查您是如何将通知发送到云端的,并确保只有在您没有“通知”属性时才会触发事件“FCMEvent.Notification”。

例:

noteA = {notification: {title: "Notification Title", body:"Notification body"}, data: {optional: "content"}}; 
noteB = {title: "Notification Title", body:"Notification body"}, data: {optional: "content"}}; 

结果:

注意事项】:应该弹出一个通知,而不触发FCMEvent.Notification

noteB:应该触发FCMEvent.Notification并且如果this.showLocalNotification(通知符)是将显示调用本地通知。

相关问题