应用程序未运行时处理推送通知(即完全被杀死)

我能够将推送通知发送到我的IOS设备。 但是,当我点击该通知时,它只会打开应用程序。 应用内没有显示任何消息。

我使用的代码:

if (application.applicationState == UIApplicationStateActive) { NSString *cancelTitle = @"Close"; NSString *showTitle = @"Show"; NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"]; UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Some title" message:message delegate:self cancelButtonTitle:cancelTitle otherButtonTitles:showTitle, nil]; [alertView show]; [alertView release]; } else { //Do stuff that you would do if the application was not active } 

但无法在上述代码的帮助下显示我的消息。 以上代码仅在我的应用程序处于打开状态且处于前台状态时才起作用,而不是仅显示此警报。

请帮忙。

谢谢。

当应用程序被完全杀死时获取通知代码

 - (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"]); //write you push handle code here } } } 

有关更多信息,请访问此链接: 在应用程序终止时处理推送通知

来自苹果文档

https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/Introduction.html

“当你的应用程序必须启动以接收通知时,UIKit在传递给你的app委托应用程序的启动选项字典中包含UIApplicationLaunchOptionsLocalNotificationKey或UIApplicationLaunchOptionsRemoteNotificationKey键:willFinishLaunchingWithOptions:和application:didFinishLaunchingWithOptions:methods。这些键的存在让你知道那里是等待处理的通知数据,让您有机会正确配置应用程序的界面。但是,您不需要在这些方法中处理通知。在您的应用程序运行后,UIKit会调用您的应用程序委托的其他方法,例如应用程序:didReceiveLocalNotification:方法,为您提供处理通知数据的机会。调用哪些方法取决于您实现的方法以及用户是否与消息的系统UI交互。

因此,请检查您的应用是否因为通知而启动,如果是,则显示对话框。

应用程序未运行(或完全被杀死)时处理推送通知

我发布这个解决方案,因为它对我有用。

转到AppDelegate.m文件。

第1步:在此函数中编写此代码:

 -(BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions { UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; if (localNotif) { NSString *cancelTitle = @"Close"; NSString *showTitle = @"OK"; NSString *message = [[localNotif valueForKey:@"aps"] valueForKey:@"alert"]; UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message Received" message:message delegate:self cancelButtonTitle:cancelTitle otherButtonTitles:showTitle, nil]; [alertView show]; } } 

第2步:

插入此完整代码:

 -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { NSLog(@"%s..userInfo=%@",__FUNCTION__,userInfo); /** * Dump your code here according to your requirement after receiving push */ if (application.applicationState == UIApplicationStateActive) { NSString *cancelTitle = @"Close"; NSString *showTitle = @"OK"; NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"]; UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message Received" message:message delegate:self cancelButtonTitle:cancelTitle otherButtonTitles:showTitle, nil]; [alertView show]; } else if(application.applicationState == UIApplicationStateBackground){ //app is in background, if content-available key of your notification is set to 1, poll to your backend to retrieve data and update your interface here NSString *cancelTitle = @"Close"; NSString *showTitle = @"OK"; NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"]; UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message Received" message:message delegate:self cancelButtonTitle:cancelTitle otherButtonTitles:showTitle, nil]; [alertView show]; } else if(application.applicationState == UIApplicationStateInactive){ //app is in background, if content-available key of your notification is set to 1, poll to your backend to retrieve data and update your interface here NSString *cancelTitle = @"Close"; NSString *showTitle = @"OK"; NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"]; UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message Received" message:message delegate:self cancelButtonTitle:cancelTitle otherButtonTitles:showTitle, nil]; [alertView show]; } } 

无论app是Active,InActive还是Totally Killed,这整个代码都能正常运行。 它将为您提供推送消息的AlertView。

我认为有一个简单的解决方案。

  1. 当您收到通知时,您可以在应用程序中存储一些标记,您可以在此处查看如何执行此操作
  2. 在那之后,你必须检测你何时来自背景可能就像这里

如果您只想在通知中打开应用程序时显示警报,那么您可能就是这个解决方案

处理这类事情的最佳方法是在APN中使用深层链接。 这样您就可以嵌入数据,然后可以在您的应用中处理这些数据并将用户定向到特定事件。

否则,您只能使用ApplicationDidEnterForeground程序委托中的ApplicationDidEnterForeground方法。 只需将您的alertView代码放在那里,任何时候您的应用程序进入将运行的前台。