React-native-firebase:推送通知并不总是适用于 iOS

React-native-firebase: Push notifications not always working on iOS

我在 react-native 中实现了一个应用程序,它通过 Firebase 发送推送通知。大多数时候,它运行良好,但有时,设备(主要是 iOS 13 台设备)没有收到推送通知。

对于正确接收我的推送通知的设备,onNotification 每次都会触发(前台和后台)。

对于没有收到我的推送通知的设备,onMessage 被触发(仅在前台)。

package.json

"react-native-firebase": "^5.6.0"

播客文件

pod 'Firebase/Core', '~> 6.19.0'
pod 'Firebase/Functions', '~> 6.19.0'
pod 'Firebase/Messaging', '~> 6.19.0'
pod 'Firebase/Auth', '~> 6.19.0'

为了测试我的推送通知,我通过 POSTMAN 发送它,使用 firebase API,当前有效负载:

{
"to" : "my_FCM_token",
"priority" : "high",
"notification" : {
    "body" : "Body TEST",
    "title": "TEST Notification",
    "vibrate": 1,
    "sound": 1
},
"data" : {
    "key" : "value"
}
}

请注意,它总是 returns 我成功了

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // FIREBASE CONFIG
    [FIRApp configure];

    // SETTING ROOT VIEW CONTROLLER
    RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
    RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
                                               moduleName:@"MyModule"
                                        initialProperties:nil];
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    UIViewController *rootViewController = [UIViewController new];
    rootViewController.view = rootView;
    self.window.rootViewController = rootViewController;
    [self.window makeKeyAndVisible];
    [RNSplashScreen show];
    return YES;
 }   

App.js

async componentDidMount() {

  firebase.messaging().hasPermission().then(enabled => {
    if (enabled) {
      firebase.messaging().getToken().then(token => {
        global.token = token;
      })
    } else {
      firebase.messaging().requestPermission()
        .then(() => {
          alert("Permission Accepted", error)
        })
        .catch(error => {
          alert("Permission Denied", error)
        });
     }
  });

  this.initialNotificationListener = firebase.notifications().getInitialNotification().then((notificationOpen: NotificationOpen) => {
    alert("Getting initial Notification")
  });

  this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen: NotificationOpen) => {
    alert("onNotificationOpened triggered")
  });

  this.notificationListener = firebase.notifications().onNotification((notification: Notification) => {
    alert("onNotification triggered")
  });

  this.onMessageListener = firebase.messaging().onMessage(async (remoteMessage) => {
    alert("onMessage triggered")
  });
}

componentWillUnmount() {
  this.notificationOpenedListener();
  this.notificationDisplayedListener();
  this.notificationListener();
  this.initialNotificationListener();
  this.onMessageListener();
}

任何帮助将不胜感激,谢谢 :)

我终于让它可以使用 react-native-firebase 发布的新修复程序。

以下是要遵循的步骤:

1) 您必须通过本教程升级到 react-native-firebase v6:https://rnfirebase.io/migrating-to-v6

2) 在您的 package.json 中添加:

"@react-native-firebase/app": "6.4.0-rc4",
"@react-native-firebase/messaging": "6.4.0-rc4"

3) 在您的 App.js 中,在此处添加这些侦听器:

  // When a user tap on a push notification and the app is in background
  this.backgroundNotificationListener = messaging().onNotificationOpenedApp(async (remoteMessage) => {
     alert("Background Push Notification opened")
  });

  // When a user tap on a push notification and the app is CLOSED
  this.closedAppNotificationListener = messaging().getInitialNotification().then((remoteMessage) => {
    if (remoteMessage) {
      alert("App Closed Push Notification opened")
    }
  });

  // When a user receives a push notification and the app is in foreground
  this.onMessageListener = messaging().onMessage(() => {
     alert("Foreground Push Notification opened")
  });

您可以在此处找到有关听众的更多信息:https://rnfb-docs.netlify.com/messaging/notifications#handling-interaction

这里是解决我的问题的讨论:https://github.com/invertase/react-native-firebase/pull/3339