didReceiveRemoteNotification没有在后台运行 [英] didReceiveRemoteNotification not working in the background
问题描述
我工作的一个大的应用程序与传统的code的一大块。
目前 - 有一个实现:
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?
推荐答案
实施 didReceiveRemoteNotification
和 didReceiveRemoteNotification:fetchCompletionHandler
是正确的方法,但你还需要做到以下几点:
Implementing didReceiveRemoteNotification
and didReceiveRemoteNotification:fetchCompletionHandler
is the correct way, but you also need to do the following:
确认为远程通知注册,请参阅documentation这里:
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;
}
另外,还要确保修改的Info.plist
并选中启用后台模式和远程通知复选框:
Also make sure to edit Info.plist
and check the "Enable Background Modes" and "Remote notifications" check boxes:
此外,你需要添加内容提供1
您推送通知的有效载荷,否则,如果它在后台应用程序将不会被唤醒(见<一href=\"https://developer.apple.com/library/$p$prelease/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html\">documentation这里):
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):
有关推送通知来触发下载操作时,
通知的有效载荷必须包括与内容提供密钥其
值设置为1。当这关键是present,系统唤醒的应用中
背景(或启动它进入后台),并调用应用程序
委托的
应用:didReceiveRemoteNotification:fetchCompletionHandler:
方法。你的实现该方法的应该下载
相关内容并将其集成到你的应用程序
For a push notification to trigger a download operation, the notification’s payload must include the content-available key with its value set to 1. When that key is present, the system wakes the app in the background (or launches it into the background) and calls the app delegate’s application:didReceiveRemoteNotification:fetchCompletionHandler: method. Your implementation of that method should download the relevant content and integrate it into your app
所以,有效载荷至少应该是这样的:
So payload should at least look like this:
{
aps = {
"content-available" : 1,
sound : ""
};
}
这篇关于didReceiveRemoteNotification没有在后台运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!