在App终止时处理推送通知

当我的应用程序没有运行,并收到推送通知,如果我点击该通知,应用程序启动 – 但它不会提示用户与我设置的警报视图,询问他们是否要查看通知的内容与否。 它刚刚启动,并坐在那里。

当应用程序运行时,推送通知可以完美地工作 – 既可以作为活动应用程序,也可以在后台运行 – 但是当应用程序没有运行时没有任何工作正常。

我尝试在应用程序中注销launchOptions NSDictionary:didFinishLaunchingWithOptions:查看加载它的是什么 – 但是它出现为“(null)”。 所以它基本上不包含任何内容 – 这是不合理的,不应该包含通知的负载?

任何人有任何想法如何使推送通知工作,当他们到达而应用程序不运行?

我的意思是当应用程序处于非运行状态时如何处理推送通知。 如果如果您收到很多通知,并且您没有打开应用程序,您也不会点击系统的通知面板。 你如何保留这些推后检索。

根据你的问题,当你打开应用程序时,没有办法保留所有的通知,最好你调用一个API来获取所有的通知,从你的后端/服务器的时间戳,这是Facebook的做法。

1) 当应用程序在后台运行时当应用程序在前台运行 application:didReceiveRemoteNotification:方法将调用如下。

 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { if (application.applicationState == UIApplicationStateInactive) { // opened from a push notification when the app was on background NSLog(@"userInfo->%@", [userInfo objectForKey:@"aps"]); } else if(application.applicationState == UIApplicationStateActive) { // a push notification when the app is running. So that you can display an alert and push in any view NSLog(@"userInfo->%@", [userInfo objectForKey:@"aps"]); } } 

2) 当应用程序没有启动(closures),然后application:didFinishedLaunchingWithOptions方法将被调用。

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { if (launchOptions != nil) { // opened from a push notification when the app is closed NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; if (userInfo != nil) { NSLog(@"userInfo->%@", [userInfo objectForKey:@"aps"]); } } else { // opened app without a push notification. } } 

3)无法删除特定的通知。 当用户从其中一个应用程序中打开应用程序时,从应用程序中删除所有通知的方式,使其不会显示在通知中心,而是将应用程序标记设置为0。

该应用程序不会在后台处理推送通知,一旦您按下通知,操作系统真正在唤醒应用程序。 你可以通过以下方式来捕捉这个瞬间:

  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { if ([launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]) { // Your app has been awoken by a notification... } } 

在应用程序结束时没有办法处理这个问题。 您需要维护服务器上的未读徽章数。 当应用程序被杀害时,徽章值从服务器更新。

因此,当您随时打开应用程序时,您需要调用Web服务来获取所需的通知并更新tabbar的徽章(如果使用的话)。

您可以使用getDeliveredNotifications(completionHandler:)方法检索传递到您的应用程序的通知。 请注意,这只会返回通知中心当前显示的通知,而不是用户手动清除的通知。

 UNUserNotificationCenter.current().getDeliveredNotifications { notifications in // notifications: An array of UNNotification objects representing the local // and remote notifications of your app that have been delivered and are still // visible in Notification Center. If none of your app's notifications are // visible in Notification Center, the array is empty. // As said in the documentation, this closure may be executed in a background // thread, so if you want to update your UI you'll need to do the following: DispatchQueue.main.sync { /* or .async {} */ // update UI } }