fetchCompletionHandler

fetchCompletionHandler

本文介绍了didReceiveRemoteNotification 在后台不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个包含大量遗留代码的大型应用程序.目前 - 有一个实现:

- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

问题是它仅在应用程序处于前台或用户在应用程序处于后台时点击通知时调用.我尝试实现:

- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

但该应用程序的行为相同.无论如何 - 当应用程序在后台时不会调用此方法.可能是什么问题?

解决方案

Implementing didReceiveRemoteNotificationdidReceiveRemoteNotification:fetchCompletionHandler 是正确的方法,但是你还需要做下面的事情:

确保注册远程通知,请参阅 更新):

对于触发下载操作的推送通知,通知的有效负载必须包含内容可用密钥及其值设置为 1.当该键存在时,系统将应用程序唤醒后台(或将其启动到后台)并调用应用程序代表的应用程序:didReceiveRemoteNotification:fetchCompletionHandler:方法.您对该方法的实现应该下载相关内容并将其集成到您的应用中

所以payload至少应该是这样的:

{aps = {内容可用": 1,声音:"};}

I'm working on a big app with a huge chunk of legacy code.Currently - there's an implementation for:

- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

The problem is that it is only called when the app is in the foreground OR when the user taps the the notification while the app is in the background.I tried to implement:

- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

But the app behaves the same.In any case - this method is not called when the app is in the background.What could be the problem?

解决方案

Implementing didReceiveRemoteNotification and didReceiveRemoteNotification:fetchCompletionHandler is the correct way, but you also need to do the following:

Make sure to register for remote notifications, see documentation here:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];

    return YES;
}

Also make sure to edit Info.plist and check the "Enable Background Modes" and "Remote notifications" check boxes:

Additionally, you need to add "content-available":1 to your push notification payload, otherwise the app won't be woken if it's in the background (see documentation here updated):

So payload should at least look like this:

{
    aps = {
        "content-available" : 1,
        sound : ""
    };
}

这篇关于didReceiveRemoteNotification 在后台不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 07:20