【问题标题】:React-native-firebase: Push notifications not always working on iOSReact-native-firebase:推送通知并不总是适用于 iOS
【发布时间】:2020-06-24 08:33:29
【问题描述】:

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

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

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

package.json

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

Podfile

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"
}
}

请注意,它总是让我成功

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();
}

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

【问题讨论】:

  • 你找到什么了吗?
  • 是的,检查我的答案 :),祝你好运!
  • 我听到了通知的声音,但没有出现通知。请帮助我

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


【解决方案1】:

我终于让它使用了 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

【讨论】:

  • 我添加了 await messages().requestPermission();等待消息()。registerDeviceForRemoteMessages();和 const fcmToken = await messages().getToken();在上面的代码之前,但仍然没有运气。我无法在后台收到通知?是否必须在 AppDelegate 中添加任何内容才能使推送通知在后台工作?
猜你喜欢
  • 1970-01-01
  • 2020-05-22
  • 2018-05-25
  • 1970-01-01
  • 1970-01-01
  • 2020-12-13
  • 2023-03-17
  • 1970-01-01
  • 2021-03-31
相关资源
最近更新 更多