【问题标题】:React-native firebase IOS push notificationReact-native firebase IOS 推送通知
【发布时间】:2019-11-01 14:32:07
【问题描述】:

我正在尝试使用 FB 进行推送通知。在 android bud 上一切正常 IOS 有一些问题。 主要问题是我无法收到通知。 -> Bud 有时通知会正确,并且当我重新安装应用程序通知时一切正常,iPhone 上不会收到通知。它随机工作。

我用

"react-native": "0.59.4"
"react-native-firebase": "^5.5.6"

我的 pod 文件

  pod 'Firebase/Core', '~> 6.3.0'
  pod 'Firebase/Messaging', '~> 6.3.0'

AppDelegate.m

#import <Firebase.h>
#import <UserNotifications/UserNotifications.h>
#import "RNFirebaseNotifications.h"
#import "RNFirebaseMessaging.h"
...

if ([FIRApp defaultApp] == nil) {
    [FIRApp configure];
}
[RNFirebaseNotifications configure];
[[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];
UIUserNotificationSettings *settings =
[UIUserNotificationSettings
 settingsForTypes: (UIUserNotificationTypeBadge |
                    UIUserNotificationTypeSound |
                    UIUserNotificationTypeAlert)
 categories:nil];

[[UIApplication sharedApplication] registerUserNotificationSettings:settings];

// Register the supported interaction types.

UIUserNotificationType types = UIUserNotificationTypeBadge |

UIUserNotificationTypeSound | UIUserNotificationTypeAlert;

UIUserNotificationSettings *mySettings =

[UIUserNotificationSettings settingsForTypes:types categories:nil];

[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];

...

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

这是我在代码中使用它的方式:

import firebase from 'react-native-firebase';

const getToken = () => {
  firebase.messaging().getToken().then((token) => {
    console.log('XXXXX getToken: ', token);
    firebase.messaging().ios.registerForRemoteNotifications();
  });
};

const requestPermission = () => {
  try {
    firebase.messaging().requestPermission().then((xx) => {
      console.log('XXXX requestPermission', xx);
    });
  } catch (error) {
    console.log('XXXX requestPermission User has rejected permissions');
  }
};

export const checkPermission = () => {
  // firebase.messaging().ios.registerForRemoteNotifications();

  firebase.messaging().hasPermission().then((permission) => {
    console.log('XXXXX hasPermission check: ', permission);
    if (permission) {
      console.log('XXXX hasPermission TRUE');
      getToken();
    } else {
      console.log('XXXX hasPermission FALSE');

      requestPermission();
    }
  }).catch((e) => {
    console.log('XXXXX hasPermission ERROR: ', e);
  });
};


export const listeners = () => {
  console.log("XXXXX Listeners setup OK!!");

  firebase.notifications().onNotificationDisplayed((notification) => {
    console.log("XXXXX onNotificationDisplayed", notification);
  });

  firebase.notifications().onNotification((notification) => {
    console.log("XXXXX LISTENER onNotification", notification);

  });
  firebase.notifications().onNotificationOpened((notificationOpen) => {
    console.log("XXXXX LISTENER onNotificationOpened");
  });
  firebase.notifications().getInitialNotification().then((notificationOpen)=> {
    console.log("XXXXX LISTENER getInitialNotification OK", notificationOpen);
  }).catch((error)=> {
    console.log("XXXXX LISTENER getInitialNotification ERROR");
  });
  firebase.messaging().onMessage((message) => {
    console.log("XXXXX LISTENER onMessage");
  });
}

我正在将此文件导入到主组件中

 componentDidMount() {
    checkPermission();
    listeners();
  }

在 android 上一切正常我能够接收通知并提取所有数据。 但是对于 ios,我已经做好了一切准备,它应该可以通过日志工作: 一切看起来都很好... 现在我尝试向我的手机发送通知,但没有任何反应。但是,如果我尝试将其直接发送到该令牌,它会起作用,我会收到通知。

有时它会开始工作,当我重新安装应用程序时会收到通知,但它不再工作了。

有什么解决办法吗?

【问题讨论】:

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


    【解决方案1】:

    我的解决方案: 更新 Cocopod。所有问题都消失了

      pod 'Firebase/Core', '~> 6.5.0'
      pod 'Firebase/Messaging', '~> 6.5.0'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-24
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 2018-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多