React Native Firebase推送通知不适用于iOS

时间:2019-11-18 06:34:12

标签: react-native push-notification apple-push-notifications react-native-ios react-native-firebase

我最近将RN 0.57.1​​升级到RN 0.59.1,推送通知在更早的时候就可以正常工作,但是现在推送通知仅在应用程序首次安装时起作用。

package.json:

"react": "16.8.3",
"react-native": "^0.59.1",
"react-native-firebase": "^5.2.0"

我已经尝试过https://pushtry.com来测试实际问题,并且显示“ 未注册”。在我的旧版本中,它工作得很完美,但我不知道为什么会造成问题。

这是我的代码:

info.plist:

<key>FirebaseAppDelegateProxyEnabled</key>
<string>no</string>
<key>FirebaseDataCollectionDefaultEnabled</key>
<string>Yes</string>
<key>LSApplicationQueriesSchemes</key>

app.js:

async componentDidMount() {
  this.checkPermission();
  this.createNotificationListeners();
}
componentWillUnmount() {
  this.notificationListener();
  this.notificationOpenedListener();
}
async createNotificationListeners() {
  this.notificationListener = firebase.notifications().onNotification((notification) => {
         const { title, body} = notification.data;
         this.title = title;
         this.body = body;

         const localNotification = new firebase.notifications.Notification({
           sound: 'default',
           show_in_foreground: true,
         })
         .setNotificationId(notification.notificationId)
         .setTitle(notification.title)
         .setBody(notification.body)
         .setData(notification.data)
         .android.setAutoCancel(true)
         .android.setChannelId('mybeautysquad') // e.g. the id you chose above
         .android.setSmallIcon('ic_launcher') // create this icon in Android Studio
         .android.setColor('#000000') // you can set a color here
         .android.setPriority(firebase.notifications.Android.Priority.High);

         firebase.notifications()
         .displayNotification(localNotification)
         .catch(err => console.error(err));
     });

     /*
     * If your app is in background, you can listen for when a notification is clicked / tapped / opened as follows:
     * */
     this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen) => {
         const { title, body } = notificationOpen.notification.data;
         this.title = title;
         this.body = body;
     });

     /*
     * If your app is closed, you can check if it was opened by a notification being clicked / tapped / opened as follows:
     * */
     const notificationOpen = await firebase.notifications().getInitialNotification();
     if (notificationOpen) {
         const { title, body } = notificationOpen.notification.data;
         this.title = title;
         this.body = body;

     }
     /*
     * Triggered for data only payload in foreground
     * */
     this.messageListener = firebase.messaging().onMessage((message) => {
       //process data message
       console.log(JSON.stringify(message));
     });
}
 async checkPermission() {
     const enabled = await firebase.messaging().hasPermission();
     if (enabled) {
         this.getToken();
     } else {
         this.requestPermission();
     }
   }

   async getToken() {
     let fcmToken = await AsyncStorage.getItem('fcmToken');
     console.log(fcmToken);
     if (!fcmToken) {
         fcmToken = await firebase.messaging().getToken();
         if (fcmToken) {
             // user has a device token
             await AsyncStorage.setItem('fcmToken', fcmToken);
         }
     }
   }

   async requestPermission() {
     try {
         await firebase.messaging().requestPermission();
         // User has authorised
         this.getToken();
     } catch (error) {
         // User has rejected permissions
         console.log('permission rejected');
     }
   }

此外,我在登录时将健全的FCM令牌保存在服务器上,并检查用户FCM令牌是否在登录时未更新,然后在他们更新其个人资料时也更新了他们的FCM设备令牌。

请让我知道是否有人可以帮助我...谢谢!

0 个答案:

没有答案