【发布时间】: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