【问题标题】:react native ios willPresentNotification function doesn't handle push notification in the foregroundreact native ios willPresentNotification 函数不处理前台的推送通知
【发布时间】:2020-11-14 08:41:17
【问题描述】:

我想用 react native firebase messages 处理 react native FCM。 对于 IOS,我将所需的代码添加到我的 AppDelegate.m 我添加了 willPresentNotification 函数来处理应用程序在前台时的推送:

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

目前我没有在前台收到推送,我想在本地处理这个问题。 请问有什么建议吗?

【问题讨论】:

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


    【解决方案1】:

    默认情况下,应用在foreground时不会显示远程推送通知

    你必须添加UNUserNotificationCenterDelegate

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        // ...
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        center.delegate = self;
        // ...
    }
    
    - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
       [RNCPushNotificationIOS didRegisterUserNotificationSettings:notificationSettings];
    }
    
    
    - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
    {
     [RNCPushNotificationIOS didReceiveLocalNotification:notification];
    }
    

    AppDelegate.h 中添加UNUserNotificationCenterDelegate

    #import <React/RCTBridgeDelegate.h>
    #import <UIKit/UIKit.h>
    #import <UserNotifications/UserNotifications.h>
    
    @interface AppDelegate : UIResponder <UIApplicationDelegate, RCTBridgeDelegate, UNUserNotificationCenterDelegate>
    
    @property (nonatomic, strong) UIWindow *window;
    
    @end
    

    另一种方法是使用相同的远程通知并将其呈现为 localNotification like this

    【讨论】:

    • 感谢您的回答,我完全按照您在代码中显示的内容进行了操作,我在后台收到推送,但在 IOS 的前台没有收到
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多