ios – didReceiveRemoteNotification无法在后台运行

ios – didReceiveRemoteNotification无法在后台运行,第1张

概述我正在开发一个包含大量遗留代码的大型应用程序. 目前 – 有一个实现: - (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 问题是它仅在应用程序位于前台或用户在应用程序处于后台时点击通知时调用. 我试图实现: - (void) applicat 我正在开发一个包含大量遗留代码的大型应用程序.
目前 – 有一个实现:

- (voID) application:(UIApplication *)application dIDReceiveRemoteNotification:(NSDictionary *)userInfo

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

- (voID) application:(UIApplication *)application dIDReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(voID (^)(UIBackgroundFetchResult))completionHandler

但该应用程序的行为相同.
在任何情况下 – 当应用程序在后台时,不会调用此方法.
可能是什么问题呢?

解决方法 实现dIDReceiveRemoteNotification和dIDReceiveRemoteNotification:fetchCompletionHandler是正确的方法,但您还需要执行以下 *** 作:

确保注册远程通知,请参阅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 : ""    };}
总结

以上是内存溢出为你收集整理的ios – didReceiveRemoteNotification无法在后台运行全部内容,希望文章能够帮你解决ios – didReceiveRemoteNotification无法在后台运行所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/998958.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存