目前 – 有一个实现:
- (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 : "" }; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。