gpt4 book ai didi

ios - didReceiveLocalNotification、didReceiveRemoteNotification 和 willPresentNotification 从未在通知出现时触发,React Native App

转载 作者:行者123 更新时间:2023-11-29 13:53:48 27 4
gpt4 key购买 nike

我正在尝试在 iOS 上的 react native 应用程序中实现通知,但我遇到了一个奇怪的问题。该应用程序能够显示本地通知,并显示来自 firebase 的远程通知,但它不会按预期触发 react-native-push-notification 的事件“onNotification”。我浏览了很多论坛寻找解决方案,但我仍然没有找到,这就是为什么我请求你的帮助。

经过调查,我意识到 Objective-C 函数“didReceiveLocalNotification”和“didReceiveRemoteNotification”从未被触发,所以我认为这就是“onNotification”也不触发的原因。不幸的是,我完全是这种语言的初学者,所以我无法理解问题的原因。

查看“didReceiveLocalNotification”文档,我了解到此函数已被弃用,我应该改用“willPresentNotification”,但与其他函数一样,它从未被触发。

郑重声明,我已按照本指南处理 iOS 中的通知: https://firebase.google.com/docs/cloud-messaging/ios/client .我正在 iOS 13、react-native 0.60.5、push-notifiaction-ios ^1.0.2、react-native-push-notification ^3.1.3 和 react-native-firebase ^5.5.6 上进行测试。

AppDelegate.m


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if ([FIRApp defaultApp] == nil) {
[FIRApp configure];
[FIRMessaging messaging].delegate = self;
}
if ([UNUserNotificationCenter class] != nil) {
// iOS 10 or later
// For iOS 10 display notification (sent via APNS)
[UNUserNotificationCenter currentNotificationCenter].delegate = self;
UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert |
UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
[[UNUserNotificationCenter currentNotificationCenter]
requestAuthorizationWithOptions:authOptions
completionHandler:^(BOOL granted, NSError * _Nullable error) {
// ...
}];
} else {
// iOS 10 notifications aren't available; fall back to iOS 8-9 notifications.
UIUserNotificationType allNotificationTypes =
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
[application registerUserNotificationSettings:settings];
}

[application registerForRemoteNotifications];

// Load RootView ...

return YES;
}
// sourceURLForBridge function ...

// Required to register for notifications
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
[RNCPushNotificationIOS didRegisterUserNotificationSettings:notificationSettings];
}
// Required for the register event.
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
[RNCPushNotificationIOS didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
// Required for the notification event. You must call the completion handler after handling the remote notification.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
// Never called
[RNCPushNotificationIOS didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}
// Required for the registrationError event.
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
[RNCPushNotificationIOS didFailToRegisterForRemoteNotificationsWithError:error];
}
// Required for the localNotification event.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
// Never called
[RNCPushNotificationIOS didReceiveLocalNotification:notification];
}

//Called when a notification is delivered to a foreground app.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
// Never called
completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
//completionHandler(UNNotificationPresentationOptionAlert);
}

PushNotification 和 firebase 配置


function configurePushNotification(){
PushNotification.configure({
onRegister: function(token) {
console.warn("TOKEN:", token);
},
onNotification: function(notification) {
console.warn("NOTIFICATION:", notification);

// Never called

notification.finish(PushNotificationIOS.FetchResult.NoData);
},
permissions: {
alert: true,
badge: true,
sound: true
},
popInitialNotification: true,
requestPermissions: true
});
}
async function checkPermission(onReady) {
const enabled = await firebase.messaging().hasPermission();
if (enabled) {
console.warn('checkPermission enabled');
getToken(onReady);
} else {
console.warn('checkPermission disabled');
requestPermission(onReady);
}
}

async function requestPermission(onReady) {
try {
await firebase.messaging().requestPermission();
// User has authorised
console.warn('requestPermission try');
getToken(onReady);
} catch (error) {
// User has rejected permissions
console.error(error)
console.warn('requestPermission rejected');
}
}

async function getToken(onReady) {
fcmToken = await AsyncStorage.getItem('fcmToken');
if (!fcmToken) {
fcmToken = await firebase.messaging().getToken();
if (fcmToken) {
// user has a device token
await AsyncStorage.setItem('fcmToken', fcmToken);
}
}
console.warn(fcmToken)
if(onReady) onReady()
}

class App extends React.Component {

async componentDidMount() {
checkPermission()
configurePushNotification()
}

render(){
return (
// View ...
)
}
};

感谢您的帮助!

编辑:经过调查,我意识到在我将 Firebase/Messaging 添加到我的 podfile 后这些事件停止工作。但根据 firebase 文档,这不是正常行为。知道这是什么原因吗?

最佳答案

我有办法做到这一点。

我没有完全弄清楚为什么 AppDelegate.m 中没有调用这些事件,但我注意到 Firebase Messaging 实现了相同的事件(转到 Pods/Development Pods/RNFirebase/messaging and notifications 中的代码) 在通知出现时被调用。所以我假设这段代码覆盖了我在 AppDelegate.m 中的代码。

事实上,我并没有在听好的 Javascript 事件。此处指定的事件 https://rnfirebase.io/docs/v5.x.x/messaging/receiving-messages在这里 https://rnfirebase.io/docs/v5.x.x/notifications/receiving-notifications在 iOS 中为我​​工作。但是这些事件不会在android上调用...

所以我结束了在 Android 和 iOS 上以不同方式处理通知,使用:

  • Android react-native-push-notification 的“onNotification”

  • iOS 版 react-native-firebase 的“onNotification”和“onNotificationOpened”。

关于ios - didReceiveLocalNotification、didReceiveRemoteNotification 和 willPresentNotification 从未在通知出现时触发,React Native App,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58050537/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com