当Firebase与Push框架一起使用时,didReceiveRemoteNotification未被调用

8
我正在使用Firebase进行分析,使用一个推送通知库。当我使用其中任何一个时,didReceiveRemoteNotification被调用。但是,当我同时使用它们时,didReceiveRemoteNotification没有被调用。
代码:

didFinishLaunchingWithOptions

NSString *google_app_id = @"----";
    NSString *gcm_sender_id = @"----";
    FIROptions *o = [[FIROptions alloc] initWithGoogleAppID:google_app_id GCMSenderID:gcm_sender_id];
    o.APIKey = @"----";
    o.bundleID = @"----";
    o.clientID = @"----";
    [FIRApp configureWithOptions:o];  

// Initilization of push framework 

didReceiveRemoteNotification :

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    // This method is not called when Firebase and push framework are enabled at the same time
    // It works fine when only Firebase or only push framework is enabled
    NSLog(@"Push: %@", userInfo);
}

你是如何注册接收推送通知的? - dudeman
@dudeman:推送框架可以实现这一点。它们有自己的后端。当框架初始化时,应用程序会注册推送通知。 - Nitish
在注册推送通知后,有回调方法可以让您知道注册是否成功,并为您提供用于发送通知的推送令牌。您尝试过查看注册是否成功吗?另外,您拿到的令牌用来做什么? - dudeman
@dudeman:注册肯定成功了。我一直都能收到推送通知。只是在使用推送框架和Firebase时,didReceiveRemoteNotification方法没有被调用。 - Nitish
你有没有研究过 UNUserNotificationCenter https://developer.apple.com/documentation/usernotifications/unusernotificationcenter? - dudeman
@dudeman:我做了。 - Nitish
2个回答

2
这是因为Firebase使用 swizzling来拦截AppDelegate的方法。
您应该在您的应用程序的info.plist中禁用它:
<key>FirebaseAppDelegateProxyEnabled</key>
<false/>

实际上,关于在 iOS 中使用 Firebase 进行推送通知的信息 在 GitHub 上有很多。你可能会在那里找到你进一步问题的答案。

但这也会停止 Firebase Analytics。有没有办法只禁用 Firebase 推送通知? - Nitish
它是禁用整个分析还是只有某些事件?也许您可以尝试手动调用它们。 - kelin
我要求所有的分析工作都由Firebase处理,而不是手动处理。 - Nitish
@Nitish,您需要我提供吗,先生?: D 不好意思,我想我无法帮忙。如果我是您,我会与Firebase支持联系或手动处理这些事件。 - kelin
1
那不是我想表达的意思。但我理解你的观点。谢谢。 - Nitish
我明白这不是你的本意,只是一个玩笑。 - kelin

-1
在 Firebase Cloud Messaging 的方法文档中提到:
如果您的应用程序在后台接收到通知消息,则在用户点击通知启动应用程序之前,此回调将不会被触发。
也许只有在您点击启动应用程序的通知时,才会调用此方法?
根据我的经验,我使用以下方法处理推送:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
         withCompletionHandler:(void (^)())completionHandler {

如果应用程序处于活动状态,则获取有关即将推送的通知的信息。在块中,您必须放置常量以显示您的通知:

completionHandler(UNNotificationPresentationOptionAlert);

还有一个:

(void)userNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {

这种方法适用于点击推送通知。也许你正在处理跳转到某个特定屏幕的情况。

希望我的经验能够帮到你!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接