我正在开发一个包含大量遗留代码的大型应用程序. 目前 – 有一个实现: - (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 问题是它仅在应用程序位于前台
目前 – 有一个实现:
- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
问题是它仅在应用程序位于前台或用户在应用程序处于后台时点击通知时调用.
我试图实现:
- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
但该应用程序的行为相同.
在任何情况下 – 当应用程序在后台时,不会调用此方法.
可能是什么问题呢?
确保注册远程通知,请参阅documentation here:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)]; return YES; }
还要确保编辑Info.plist并选中“启用后台模式”和“远程通知”复选框:
此外,您需要在推送通知有效负载中添加“content-available”:1,否则如果应用程序在后台,则不会被唤醒(请参阅documentation here):
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
因此有效载荷至少应如下所示:
{ aps = { "content-available" : 1, sound : "" }; }