Ugo*_*lli 3 push-notification ios firebase react-native react-native-firebase
我在 react-native 中实现了一个通过 Firebase 发送推送通知的应用程序。大多数情况下,它运行良好,但有时,设备(主要是 iOS 13 设备)未收到推送通知。
对于正确接收我的推送通知的设备,每次(前台和后台)都会触发onNotification。
对于未收到我的推送通知的设备,会触发onMessage(仅在前台)。
包.json
"react-native-firebase": "^5.6.0"
Run Code Online (Sandbox Code Playgroud)
播客文件
pod 'Firebase/Core', '~> 6.19.0'
pod 'Firebase/Functions', '~> 6.19.0'
pod 'Firebase/Messaging', '~> 6.19.0'
pod 'Firebase/Auth', '~> 6.19.0'
Run Code Online (Sandbox Code Playgroud)
为了测试我的推送通知,我通过 POSTMAN 发送它,使用 firebase API 和当前有效负载:
{
"to" : "my_FCM_token",
"priority" : "high",
"notification" : {
"body" : "Body TEST",
"title": "TEST Notification",
"vibrate": 1,
"sound": 1
},
"data" : {
"key" : "value"
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,它总是让我成功
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;
}
Run Code Online (Sandbox Code Playgroud)
应用程序.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();
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激,谢谢:)
我终于使用 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"
Run Code Online (Sandbox Code Playgroud)
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")
});
Run Code Online (Sandbox Code Playgroud)
您可以在此处找到有关侦听器的更多信息:https : //rnfb-docs.netlify.com/messaging/notifications#handling-interaction
这是解决我的问题的讨论:https : //github.com/invertase/react-native-firebase/pull/3339
归档时间: |
|
查看次数: |
9019 次 |
最近记录: |