为什么Firebase远程推送通知在ios中不起作用,但在android中起作用

问题描述 投票:1回答:1

我将配置设置为通过react-native-firebase获取远程推送通知。在android中,所有设置都是正确的,但是在ios中,我可以获得FCMToken,但无法接收弹出的推送通知。podFile是:

 pod 'Firebase/Core'
 pod 'Firebase/Auth'
 pod 'Firebase/Messaging'
 pod 'GoogleIDFASupport'

我的AppDelegate.m是:

    #import <Firebase.h>
    #import "RNFirebaseNotifications.h"
    #import "RNFirebaseMessaging.h"
    ...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  [FIRApp configure];
  [RNFirebaseNotifications configure];
...
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
  [[RNFirebaseNotifications instance] didReceiveLocalNotification:notification];
}

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

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
  [[RNFirebaseMessaging instance] didRegisterUserNotificationSettings:notificationSettings];
}

和我获取FCm令牌并显示的代码是:

componentDidMount() {
    const channel = new firebase.notifications.Android.Channel(
      'insider',
      'insider channel',
      firebase.notifications.Android.Importance.Max,
    );
    firebase.notifications().android.createChannel(channel);
    this.checkPermission();
    this.createNotificationListeners();
  }

  async getToken() {
    let tokenFCM = await AsyncStorage.getItem('tokenFCM');
    console.log(tokenFCM);

    if (!tokenFCM) {
      // console.log('token not find');
      try {
        tokenFCM = await firebase.messaging().getToken();
        if (tokenFCM) {
          await AsyncStorage.setItem('tokenFCM', tokenFCM);
          // do sth here
        }
      } catch {
         console.log('token not find2');

      }
    } else {
      // console.log('token is find');
    }
  }

  async checkPermission() {
    const enabled = await firebase.messaging().hasPermission();
    if (enabled) {
      this.getToken();
    } else {
      this.requestPermission();
    }
  }

  async requestPermission() {
    try {
      await firebase.messaging().requestPermission();
      this.getToken();
    } catch (error) {
      await AsyncStorage.setItem('tokenFCM', '');
      console.log('permission rejected');
    }
  }

  createNotificationListeners() {
    firebase.notifications().onNotification(notification => {
      notification.android.setChannelId('insider').setSound('default');
      notification.android.setSmallIcon('@mipmap/...');
      firebase.notifications().displayNotification(notification);
    });
  }

为什么我可以在Android中看到推送通知但不能在ios中显示?

xcode react-native push-notification react-native-firebase
1个回答
0
投票

我通过添加react-native-permissions解决了这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.