我已经在我的应用程序中实现了推送通知。
当我的应用程序在前台运行时,它可以正常工作。
但是当应用程序在后台或被杀死时,didReceiveRemoteNotification
会被调用两次。
我创建了一个公共方法来处理推送通知,并从 didFinishLaunchingWithOptions
和 didReceiveRemoteNotification
中调用此方法。
这是我的实现:
- (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];
}
AND:
- (void) customPushHandler:(NSDictionary *)notification {
// Code to push ViewController
NSLog(@"Push Notification"); //Got it two times when Clicked in notification banner
}
当我的应用程序正在运行时,视图控制器会被推送一次。当我从通知横幅打开我的应用程序时,我的屏幕会被推送两次。
我在customPushHandler
中放置了一个NSLog,当应用程序在前台运行时,我得到了一次日志记录,当我从通知横幅启动它时,我得到了两次日志记录。
我的代码有什么问题?