iOS 推送通知句柄
iOS Push Notifications handle
寻找有关推送通知句柄的良好做法。
目前在我的应用程序中,我正在处理 didFinishLaunchingWithOptions 和 didReceiveRemoteNotification 代表中的推送通知。
当我在应用程序 "Dead" 时收到推送通知时,我注意到两种处理逻辑都是 "firing"。
我在后台模式下的远程通知标志在我的应用程序中是 ON。
是否有处理这种情况的良好做法?为什么我在 didFinishLaunchingWithOptions(launchOptions) 中获取推送数据并且 didReceiveRemoteNotification 也被调用?据我所知,当应用程序 "Dead" 时,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 情况的原因是因为您可以将 background/download 代码放在那里,这样它就不会下载两次东西,即如果您收到通知然后点击它。
寻找有关推送通知句柄的良好做法。 目前在我的应用程序中,我正在处理 didFinishLaunchingWithOptions 和 didReceiveRemoteNotification 代表中的推送通知。 当我在应用程序 "Dead" 时收到推送通知时,我注意到两种处理逻辑都是 "firing"。 我在后台模式下的远程通知标志在我的应用程序中是 ON。 是否有处理这种情况的良好做法?为什么我在 didFinishLaunchingWithOptions(launchOptions) 中获取推送数据并且 didReceiveRemoteNotification 也被调用?据我所知,当应用程序 "Dead" 时,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 情况的原因是因为您可以将 background/download 代码放在那里,这样它就不会下载两次东西,即如果您收到通知然后点击它。