Ios DidReceivereMotenofication打了两次电话
我已经在我的应用程序中实现了推送通知 当我的应用程序位于前台时,我的应用程序工作正常 但当应用程序处于后台或被禁用时,myIos DidReceivereMotenofication打了两次电话,ios,objective-c,iphone,push-notification,xcode6,Ios,Objective C,Iphone,Push Notification,Xcode6,我已经在我的应用程序中实现了推送通知 当我的应用程序位于前台时,我的应用程序工作正常 但当应用程序处于后台或被禁用时,myDidReceiveEmotentification会调用两次 我已经制定了一个处理推送通知的常用方法,并从didfinishlaunchwithoptions和didreceiveremotentification 以下是我的实现: - (BOOL)application:(UIApplication *)application didFinishLaunchingWith
DidReceiveEmotentification
会调用两次
我已经制定了一个处理推送通知的常用方法,并从didfinishlaunchwithoptions
和didreceiveremotentification
以下是我的实现:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSDictionary *pushDictionary = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (pushDictionary) {
[self customPushHandler:pushDictionary];
}
return YES;
}
和:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler {
[self customPushHandler:userInfo];
}
- (void) customPushHandler:(NSDictionary *)notification {
// Code to push ViewController
NSLog(@"Push Notification"); //Got it two times when Clicked in notification banner
}
和:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler {
[self customPushHandler:userInfo];
}
- (void) customPushHandler:(NSDictionary *)notification {
// Code to push ViewController
NSLog(@"Push Notification"); //Got it two times when Clicked in notification banner
}
当我的应用程序运行时,ViewController会被按下一次。当我从通知横幅打开我的应用程序时,我的屏幕会被推两次
我在customPushHandler
中放置了一个NSLog,当应用程序位于前台时,我得到了一次,当我从通知横幅启动它时,我得到了两次
我的代码中出现了什么问题?请检查DidReceiveMemotentification中的这一条件,例如当应用程序处于后台时点击通知。这样,你就可以避免被打两次电话
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler {
UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if (state == UIApplicationStateBackground || state == UIApplicationStateInactive || state == UIApplicationStateForeground || state == UIApplicationStateActive)
{
//Do checking here.
[self customPushHandler:userInfo];
}
}
检查我是否编辑了答案。检查当前导航堆栈中的俯视图控制器,如:
if(![self.navigationController.topViewController isKindOfClass:[MyClass class]]) {
//code for pushing new controller
}
无论何时使用backgroundFetch进行远程通知,都要为此添加一个完成处理程序
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler {
[self customPushHandler:userInfo];
completionHandler(UIBackgroundFetchResultNewData)
}
当应用程序处于后台时,该方法将被调用两次,一次是在您收到推送通知时,另一次是在您点击该通知时
如果应用程序位于后台,则可以验证应用程序状态并忽略收到的通知
解决方案:
Swift 4代码:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if (application.applicationState == .background) {
completionHandler(.noData)
return
}
// logic
// call completionHandler with appropriate UIBackgroundFetchResult
}
没有内部条件,只要你打电话。因为如果应用程序是从后台打开的,那么应该调用此方法。如果应用程序被终止,那么从didfinishlauch方法,你的处理程序应该被调用。你没有明白我的意思。我正在调用
customPushHandler
方法,该方法也来自didfishlaunchwithoptions
。。如果我使用你的代码,我的customPushHandler
将始终被调用两次,当我的应用程序位于前台时,我的customPushHandler
将永远不会被调用。应用程序不会一次调用这两个方法。它们具有文档中描述的不同功能。与此相反,if(pushDictionary)检查如下if(pushDictionary!=nil)。或者用这两种方法记录pushDictionary。如果得到的值相同或为零,请参阅