标题解释了发生了什么。应用程序正在运行但最小化。通知将显示给用户。当用户点击通知时,应用程序将进入前台并输入方法didReceiveRemoteNotification。但是,如果用户单击应用程序图标而不是恢复应用程序,则不会执行didReceiveRemoteNotification方法。
这是我的代码:
//FOR ALLOWING NOTIFICATIONS
let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
UIApplication.sharedApplication().registerForRemoteNotifications()
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let deviceTokenStr = convertDeviceTokenToString(deviceToken)
GlobalVariables.UserDefaults.setValue(deviceTokenStr,forKey: "Push_Notification_Reg_ID");
RZLog.VIP("The PushNotification Device Token is: \(deviceTokenStr)")
}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
RZLog.Error(error.description)
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
RZLog.Debug("A NEW NOTIFICATION HAS BEEN RECEIVED")
//handle
completionHandler(UIBackgroundFetchResult.NoData)
}
}
答案 0 :(得分:1)
这是设计的。在应用程序中收到远程通知时(在后台或前台)以及用户从通知中启动应用程序时,将调用此方法。
如果用户点按应用图标,则仅
applicationWillEnterForeground:
和
applicationDidBecomeActive:
将被称为
答案 1 :(得分:0)
将不会调用didReceiveRemoteNotification。你应该使用
- (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);
}
}
基本上,didReceiveRemoteNotification仅在以下场景中调用:
点按推送通知横幅& app进入前台并调用didReceiveRemoteNotification。
当App在前台运行时,推送通知到达&调用didReceiveRemoteNotification。
来自Apple文档: 如果在推送通知到达时应用程序未运行,则该方法将启动应用程序并在启动选项字典中提供相应的信息。该应用程序不会调用此方法来处理该推送通知。相反,您的应用程序的实现:willFinishLaunchingWithOptions:或application:didFinishLaunchingWithOptions:方法需要获取推送通知有效负载数据并做出适当的响应。