iOS推送通知问题

我正在做一个推送通知function是其中一个关键function的项目。
当我在应用程序中时它工作正常,我收到通知并处理该通知。

但问题是,当我在后台并收到通知时,我会看到我的应用程序图标上的徽章,当我点击图标时,我的应用程序正在启动,但未调用didReceiveRemoteNotification方法,因此我无法处理该通知。

另一个问题是有时它会在device notification list显示通知消息,有时却没有。

当我通过点击通知列表项进入我的应用程序时, didReceiveRemoteNotification调用并且我成功地能够处理通知。 我在didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method编写以下代码

 NSDictionary* remoteNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; if (remoteNotif != nil) { NSLog(@"didFinishLaunchingWithOptions\nNotification recieved:\n%@",remoteNotif); notificationData=[[NSDictionary alloc]initWithDictionary:remoteNotif]; [notif saveNotification:remoteNotif]; } 

帮我解决这个问题。 提前致谢。

你在didfinishlaunch方法中所做的事情也在didReceiveRemoteNotification中进行。 当你来自背景时,不会召唤didfinishlaunch

 - (void)application:(UIApplication*)application didReceiveRemoteNotification: (NSDictionary*)userInfo { UIApplicationState state = [application applicationState]; if (state == UIApplicationStateActive) { //What you want to do when your app was active and it got push notification } else if (state == UIApplicationStateInactive) { //What you want to do when your app was in background and it got push notification } } 

来自Apple的Local and Push Notification编程指南

处理本地和远程通知

让我们回顾一下系统为应用程序发送本地通知或远程通知时的可能情况。

  1. 当应用程序未在前台运行时,将传递通知。

    在这种情况下,系统会显示通知,显示警报,标记图标,或者播放声音。

    作为所呈现的通知的结果,用户点击警报的动作按钮或点击(或点击)应用程序图标。

    如果点击操作按钮(在运行iOS的设备上),系统将启动应用程序,应用程序将调用其委托的application:didFinishLaunchingWithOptions: method(如果已实现); 它传递通知有效负载(用于远程通知)或本地通知对象(用于本地通知)。

    如果在运行iOS的设备上轻触应用程序图标,则应用程序会调用相同的方法,但不会提供有关通知的信息 。 如果在运行OS X的计算机上单击应用程序图标,则应用程序将调用委托的applicationDidFinishLaunching:方法,其中委托可以获取远程通知负载。

  2. 当应用程序在前台运行时,将传递通知。

    应用程序调用其委托的application:didReceiveRemoteNotification: method(用于远程通知)或application:didReceiveLocalNotification:方法(用于本地通知)并传入通知有效内容或本地通知对象。

因此,在您的情况下,当应用程序在后台运行时,以及当您单击通知/警报时,操作系统会将您的应用程序置于前台。 所以它属于第二点。

如果点击操作按钮,您可以实现application:didReceiveRemoteNotification:方法来获取通知有效负载。 但是,当按下应用程序图标而不是操作消息时,不会使用该方法转发通知有效内容。 您唯一的选择是联系您的服务器并同步数据。 毕竟按照Apple的政策,推送通知只会告诉服务器上有数据,并且您需要连接到服务器并获取数据。

我在我的一个应用程序中做了同样的事情。 单击应用程序图标时无法处理通知。 因此,您可以做的是进行服务器调用以获取latestPushNotificationIdSync。 您必须将数据存储在服务器上的某个位置,因此,您需要在服务器上检查最新的latestPushNotificationId。

  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions notificationContentDict = launchOptions; if([[launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"]count]>0){ application.applicationIconBadgeNumber = 0; NSString *key = [[launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"] valueForKey:@"id"]; contentId = key; ////// you can use this content id ///// write your code here as per the requirement ..//// //// display your content on UI /// either get from server or local .../// [self displayContent:[launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"] application:application]; } else [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound]; return YES; } - (void)applicationDidBecomeActive:(UIApplication *)application { NSLog(@"badge count ..%d", application.applicationIconBadgeNumber); if( application.applicationIconBadgeNumber >0){ if(!notificationContentDict){ make a server call to get "latestPushNotificationIdSync" application.applicationIconBadgeNumber = 0; NSLog(@"badge count applicationDidBecomeActive.%d", application.applicationIconBadgeNumber); } } }