gpt4 book ai didi

ios - didReceiveRemoteNotification 不在后台工作

转载 作者:IT王子 更新时间:2023-10-29 07:43:56 25 4
gpt4 key购买 nike

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

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

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

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

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

最佳答案

实现didReceiveRemoteNotificationdidReceiveRemoteNotification:fetchCompletionHandler是正确的方式,但是你还需要做以下事情:

确保注册远程通知,请参阅 documentation here :

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

return YES;
}

还要确保编辑 Info.plist 并选中“启用后台模式”和“远程通知”复选框:

enter image description here

此外,您需要将 "content-available":1 添加到您的推送通知负载中,否则应用程序在后台时不会被唤醒(参见 documentation here 已更新):

For a push notification to trigger a download operation, thenotification’s payload must include the content-available key with itsvalue set to 1. When that key is present, the system wakes the app inthe background (or launches it into the background) and calls the appdelegate’sapplication:didReceiveRemoteNotification:fetchCompletionHandler:method. Your implementation of that method should download therelevant content and integrate it into your app

所以有效载荷至少应该是这样的:

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

关于ios - didReceiveRemoteNotification 不在后台工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31450403/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com