1

我可以将推送通知发送到我的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 

} 

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

请帮忙。

谢谢。

+0

,你必须把你刚才notification.The代码是不相关的代码显示警报查看应用何时处于活动状态。你想做什么 ? –

+0

我在我的IOS设备上收到推送通知。我只是想当我点击推送消息,而不是它应该打开我的应用程序,并显示该消息给用户多数民众赞成它。如果您使用正确的代码详细指导,会很高兴。 @Teja Nandamuri –

回答

-2

处理这种事情的最好方法是在APN中使用深度链接。这将让您嵌入数据,然后可以在您的应用程序中处理数据,并将用户引导至特定事件。

否则,您仅限于使用应用程序委托中的ApplicationDidEnterForeground方法。只要把你的alertView代码放在那里,任何时候你的应用程序都会被放到前台运行。

+0

能否请您提供一个解决方案详细的代码@TheValyreanGroup –

+0

这里是一个很好的写在如何做到这一点。您需要3件事... 1-Custom URL Scheme APN内的2-Deep链接 3-Code用于处理来自APN的深层链接 https://blog.mixpanel.com/2015/04/21/ guide-setting-up-deep-linking-in-ios-and-android/ – TheValyreanGroup

+0

感谢您的帮助,但该教程提供了如何使用深层链接打开特定的推送通知屏幕的知识。但我只想在主屏幕上显示我的推送通知。 @TheValyreanGroup –

1

从苹果文档

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

“当你的应用程序必须启动以 收到通知,UIKit中包括推出 UIApplicationLaunchOptionsLocalNotificationKey或 UIApplicationLaunchOptionsRemoteNotificationKey关键 选项字典传递给你的应用程序代理的 应用程序:willFinishLaunchingWithOptions:和 应用程序:didFinishLaunchingWithOptio ns:方法。 这些密钥的存在让你知道,有通知数据等待 处理,并让你有机会适当地配置你的应用程序的界面 。不过,您不需要在这些 方法中处理通知。在您的应用运行后,UIKit会调用您的应用委托的其他方法 ,例如 应用:didReceiveLocalNotification:方法,以便为您提供处理通知数据的 机会。这方法被称为 取决于哪些方法您实现与用户 是否与该消息的系统UI交互。”

所以,请检查您的应用程序已经启动,由于通知,如果是这样显示的对话框

0

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

  1. 可以存储一些标志的应用程序,一旦当您收到 通知,您可以检查该怎么办呢here
  2. 后,关于你当你从后台 也许像here
  3. 检测

如果你想,当你从通知打开应用程序,只显示警报,那么也许你这是你solution

+0

50%我有一个解决方案。使用这个代码:else if(application。应用程序状态== UIApplicationStateInactive){ //应用程序正在从后台切换到前台(用户点击通知),当用户点击此处时执行所需操作 }但是,如果我用于UiapplicationStateBackground,它对我不起作用。 @ m1sh0 –

1

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

我发布此解决方案,因为它为我工作。

转到您的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]; 

    } 


} 

这整个代码将工作,无论是应用程序有效,无效或完全死亡。它会为您提供推送消息的AlertView。

1

当应用程序被完全杀死GET通知代码

- (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 

    } 

} 
} 

更多通过这个链接:Handling Push Notifications when App is Terminated

相关问题