我正在尝试实现后台推送通知处理,但在确定用户是否从发送的推送通知打开应用程序而不是从图标打开应用程序时遇到问题。
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
//************************************************************
// I only want this called if the user opened from swiping the push notification.
// Otherwise I just want to update the local model
//************************************************************
if(applicationState != UIApplicationStateActive) {
MPOOpenViewController *openVc = [[MPOOpenViewController alloc] init];
[self.navigationController pushViewController:openVc animated:NO];
} else {
///Update local model
}
completionHandler(UIBackgroundFetchResultNewData);
}
使用此代码,无论用户如何打开应用程序,应用程序都会向 MPOOpenViewController 打开。如何才能使视图控制器仅在通过滑动通知打开应用程序时才被推送?
使用相同的代码,这可以在 iOS 6 上运行,但使用新的 iOS 7 方法时,它的行为并不像我想要的那样。
Edit:我现在尝试在 iOS 7 上运行该应用程序,并且我们不支持 iOS 7 之前的任何版本。我在该方法的 iOS 6 版本中使用了相同的代码(没有完成处理程序),它的行为方式如下我希望如此。您只需滑动通知即可调用该通知。如果您从图标打开,则永远不会调用该方法。
好吧,我想通了。该方法实际上被调用两次(一次是在收到推送时,一次是在用户与图标或通知交互时)。
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
if(application.applicationState == UIApplicationStateInactive) {
NSLog(@"Inactive");
//Show the view with the content of the push
completionHandler(UIBackgroundFetchResultNewData);
} else if (application.applicationState == UIApplicationStateBackground) {
NSLog(@"Background");
//Refresh the local model
completionHandler(UIBackgroundFetchResultNewData);
} else {
NSLog(@"Active");
//Show an in-app banner
completionHandler(UIBackgroundFetchResultNewData);
}
}
感谢 Tim Castelijns 的以下补充:
注意:它被调用两次的原因是由于有效负载有content_available : 1
。如果你remove键及其值,那么它只会在点击时运行。这并不能解决每个人的问题,因为有些人需要该密钥为真
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)