gpt4 book ai didi

iOS 推送通知句柄

转载 作者:行者123 更新时间:2023-11-29 12:19:18 25 4
gpt4 key购买 nike

寻找有关推送通知句柄的良好做法。目前在我的应用程序中,我正在处理 didFinishLaunchingWithOptionsdidReceiveRemoteNotification 代表中的推送通知。我注意到当我在应用程序“死”时收到推送通知时,两个处理逻辑都在“触发”。我的后台模式中的远程通知标志在我的应用程序中为 ON。是否有处理这种情况的良好做法?为什么我在 didFinishLaunchingWithOptions(launchOptions) 中获取推送数据并且 didReceiveRemoteNotification 也被调用?据我所知, didReceiveRemoteNotification 不会在应用程序“死”时被调用。

最佳答案

didReceiveRemoteNotification 在收到通知以及您从通知中打开应用程序时调用。如果您的应用已死并且根本无法打开,则不会触发后台下载。您遇到的是 didReceiveRemoteNotification 因为您点击了通知而被调用。您需要做的是向该方法添加一些逻辑,以查看应用程序处于什么状态并在那里处理您的通知。

即:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
{


if ([UIApplication sharedApplication].applicationState == UIApplicationStateInactive) {

[[NSNotificationCenter defaultCenter] postNotificationName:@“inactivePush" object:nil];


}

else if([UIApplication sharedApplication].applicationState==UIApplicationStateActive){



[[NSNotificationCenter defaultCenter] postNotificationName:@"appOpenPush" object:nil];


}

//When the app is in the background
else {



}//End background

}

}

第一种情况是应用程序处于非事件状态(通过推送打开),第二种情况是应用程序实际打开的情况,第三种情况是应用程序在后台运行的情况。我使用 NSNotifcation 只是作为示例,但您可以使用任何您想要的处理代码。我添加最终 if 情况的原因是因为您可以将后台/下载代码放在那里,这样它就不会下载两次东西,即如果您收到通知然后点击它。

关于iOS 推送通知句柄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31100510/

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