【问题标题】:Handling Push Notifications when App is NOT running (i.e Totally Killed)在应用程序未运行时处理推送通知(即完全被杀死)
【发布时间】:2016-11-25 12:45:33
【问题描述】:

我可以向我的 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

}

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

请帮忙。

谢谢。

【问题讨论】:

  • 您拥有的代码与推送通知无关。您拥有的代码只是在应用处于活动状态时显示警报视图。你想做什么?
  • 我在我的 IOS 设备上收到推送通知。我只想当我单击该推送消息时,它应该打开我的应用程序并向用户显示该消息。如果您使用适当的代码进行详细指导,将会很高兴。 @Teja Nandamuri

标签: ios push-notification apple-push-notifications uialertview uilocalnotification


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    来自苹果文档

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

    "当你的应用程序必须启动到 收到通知,UIKit 包括 UIApplicationLaunchOptionsLocalNotificationKey 或 UIApplicationLaunchOptionsRemoteNotificationKey 键在启动 传递给您的应用程序委托的选项字典 应用程序:willFinishLaunchingWithOptions:和 应用程序:didFinishLaunchingWithOptions:方法。的存在 这些键让您知道有通知数据等待 处理并让您有机会配置应用程序的界面 适当地。您不需要处理这些通知 方法,虽然。应用运行后,UIKit 会调用其他方法 您的应用程序委托的,例如 application:didReceiveLocalNotification: 方法,给你一个 处理通知数据的机会。调用了哪些方法 取决于您实现了哪些方法以及用户是否 与消息的系统 UI 交互。”

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

    【讨论】:

      【解决方案3】:

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

      1. 您可以在收到后将一些标志存储在应用程序中一次 通知您可以查看操作方法here
      2. 之后,您必须检测您何时来自后台 也许像here

      如果您只想在从通知中打开应用程序时显示警报,那么也许您就是您的solution

      【讨论】:

      • 50% 我找到了解决方案。使用此代码: else if(application.applicationState == UIApplicationStateInactive){ //app 正在从后台转换到前台(用户点击通知),当用户点击此处时执行您需要的操作} 但如果我使用的是 UiapplicationStateBackground 它不会对我有用。 @m1sh0
      【解决方案4】:

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

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

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

      【讨论】:

        【解决方案5】:

        处理此类问题的最佳方法是在 APN 中使用深度链接。这将允许您嵌入可以在您的应用程序中处理的数据,并将用户引导到特定事件。

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

        【讨论】:

        • 能否用代码@TheValyreanGroup 提供详细的解决方案
        • 这里有一篇关于如何做的很好的文章。您需要 3 样东西... 1-自定义 URL 方案 2-APN 中的深层链接 3-代码来处理来自 APN blog.mixpanel.com/2015/04/21/… 的深层链接
        • 感谢您的帮助,但该教程提供了如何使用深度链接打开特定屏幕以进行推送通知的知识。但我只想在主屏幕上显示我的推送通知。 @TheValyreanGroup
        • 1) 完全没有必要进行深度链接 2) 无论如何,您甚至无法通过推送进行深度链接。 3) APN 中的有效负载在应用程序启动时直接可用,那么尝试深度链接的意义何在? 4) 他仅限于 ApplicationDidEnterForeground 这不是真的,这与推送通知和问题有什么关系?
        猜你喜欢
        • 1970-01-01
        • 2018-12-20
        • 2017-06-07
        • 2015-12-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多