具有后台刷新功能的 APN。刷新调用两次,何时传递 APN 以及何时点击通知?
APNs with background refresh. Refresh called twice, when the APN is delivered and when the notification is tapped on?
我正在尝试使用内容可用密钥在我的应用程序中实施 APN,以便触发后台刷新。这是我的代码:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
{
if([userInfo[@"aps"][@"content-available"] intValue]== 1){
//This stops a refresh happening is a push is delivered with the app in the foreground
if(application.applicationState!=UIApplicationStateActive){
NSLog(@“Background push refresh called");
[self backgroundRefreshWithPushUpdate:NO andHandler:^(BOOL successful, BOOL newMessages) {
if(successful){
if(newMessages) handler(UIBackgroundFetchResultNewData);
else handler(UIBackgroundFetchResultNoData);
}
else{
handler(UIBackgroundFetchResultFailed);
}
}];
}
else handler(UIBackgroundFetchResultNoData);
}
}
我有这个附加条件:if(application.applicationState!=UIApplicationStateActive)
用于在后台刷新,因为我不希望它在应用程序位于前台时被触发。但是,如果我收到推送,然后点击通知打开应用程序,- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
中的所有代码都会被再次调用。这意味着我的后台提取在通知第一次进入时被调用,然后在通知被点击时再次被调用。我不希望这发生。有什么想法可以解决这个问题吗?
接收推送通知时,application:didReceiveRemoteNotification:fetchCompletionHandler:方法中需要注意的事项:
1。当应用程序未启动时(即,当应用程序既不在后台也不在前台时),该方法被调用一次并且 applicationState 将是 UIApplicationStateInactive.
2.当应用程序在前台时,该方法被调用一次,applicationState 将是 UIApplicationStateActive.
3。当应用程序在后台时,该方法会被调用两次,一次是在您收到推送通知时,另一次是在您点击该通知时。当您收到推送通知时,applicationState 将为 UIApplicationStateBackground,当您点击该通知时,applicationState将是 UIApplicationStateInactive。
当 applicationState 将是 UIApplicationStateBackground[ 时我们可以忽略它=30=] 因此我们可以为所有三种情况只处理一次推送通知。
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
if (application.applicationState == UIApplicationStateBackground) {
completionHandler(UIBackgroundFetchResultNoData);
return;
}
// Do whatever you need here and call completionHandler with appropriate UIBackgroundFetchResult
}
我正在尝试使用内容可用密钥在我的应用程序中实施 APN,以便触发后台刷新。这是我的代码:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
{
if([userInfo[@"aps"][@"content-available"] intValue]== 1){
//This stops a refresh happening is a push is delivered with the app in the foreground
if(application.applicationState!=UIApplicationStateActive){
NSLog(@“Background push refresh called");
[self backgroundRefreshWithPushUpdate:NO andHandler:^(BOOL successful, BOOL newMessages) {
if(successful){
if(newMessages) handler(UIBackgroundFetchResultNewData);
else handler(UIBackgroundFetchResultNoData);
}
else{
handler(UIBackgroundFetchResultFailed);
}
}];
}
else handler(UIBackgroundFetchResultNoData);
}
}
我有这个附加条件:if(application.applicationState!=UIApplicationStateActive)
用于在后台刷新,因为我不希望它在应用程序位于前台时被触发。但是,如果我收到推送,然后点击通知打开应用程序,- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
中的所有代码都会被再次调用。这意味着我的后台提取在通知第一次进入时被调用,然后在通知被点击时再次被调用。我不希望这发生。有什么想法可以解决这个问题吗?
接收推送通知时,application:didReceiveRemoteNotification:fetchCompletionHandler:方法中需要注意的事项:
1。当应用程序未启动时(即,当应用程序既不在后台也不在前台时),该方法被调用一次并且 applicationState 将是 UIApplicationStateInactive.
2.当应用程序在前台时,该方法被调用一次,applicationState 将是 UIApplicationStateActive.
3。当应用程序在后台时,该方法会被调用两次,一次是在您收到推送通知时,另一次是在您点击该通知时。当您收到推送通知时,applicationState 将为 UIApplicationStateBackground,当您点击该通知时,applicationState将是 UIApplicationStateInactive。
当 applicationState 将是 UIApplicationStateBackground[ 时我们可以忽略它=30=] 因此我们可以为所有三种情况只处理一次推送通知。
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
if (application.applicationState == UIApplicationStateBackground) {
completionHandler(UIBackgroundFetchResultNoData);
return;
}
// Do whatever you need here and call completionHandler with appropriate UIBackgroundFetchResult
}