【问题标题】:Handling Apple Push Notification Service处理 Apple 推送通知服务
【发布时间】:2014-01-04 00:14:37
【问题描述】:

我在我的项目中使用苹果推送通知服务。

请按照以下两种方式打开应用并处理此推送通知。在第二种情况下,我不知道如何处理它。你知道怎么做吗?

推送通知已到达我的设备,

场景 1:

  1. 我点击了推送通知。

  2. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 函数 AppDelegate.m 文件捕获了这个函数。

场景 2:

  1. 我正常打开设备(通过点击应用)

  2. 如何处理推送通知?

【问题讨论】:

  • 看起来场景 2 没有问题。通常,当用户通过触摸应用程序图标启动应用程序时,这意味着他不希望触发推送通知中的操作。顺便说一句,如果你想轻松实现推送通知,可以查看 PushApps 提供的以下 SDK pushapps.mobi

标签: ios iphone objective-c push-notification apple-push-notifications


【解决方案1】:

其他答案显示了当用户点击通知时如何获取通知数据。

显示的两个网络之间的区别在于,一个在应用程序已经运行时调用,无论是在前台还是在后台,而另一个在应用程序根本没有运行时调用。

在第二种情况下,当用户没有点击通知时,当您使用启动图标打开应用时,通知数据不会传递给应用。

【讨论】:

  • 您是否建议我使用一种模式来处理此案例的新通知(应用程序以启动图标打开)?
【解决方案2】:

第一种情况:

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
    NSLog (@"APNS: notification received: %@", userInfo);

    NSString *message = nil;
    id alert = [userInfo objectForKey:@"aps"];

    if ([alert isKindOfClass:[NSString class]])
    {
        message = alert;
    }
    else if ([alert isKindOfClass:[NSDictionary class]])
    {
        message = [alert objectForKey:@"alert"];
    }

    if (message)
    {
        if (![message isEqualToString:@""])
        {
            UIAlertView *alertView = [[UIAlertView alloc]
                                      initWithTitle: @"notification"
                                      message: message
                                      delegate: nil
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:nil];
            [alertView show];
        }
    }
}

第二种情况:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSLog (@"LAUNCH OPTIONS: %@",launchOptions);

    id remoteNotificationValue = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if (remoteNotificationValue)
    {
        NSString *message = nil;
        id alert = [remoteNotificationValue objectForKey:@"aps"];

        if ([alert isKindOfClass:[NSString class]])
        {
            message = alert;
        }
        else if ([alert isKindOfClass:[NSDictionary class]])
        {
            message = [alert objectForKey:@"alert"];
        }

        if (message)
        {         
            if (![message isEqualToString:@""])
            {
                UIAlertView *alertView = [[UIAlertView alloc]
                                          initWithTitle: @"notification"
                                          message: message
                                          delegate: nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
                [alertView show];
            }
        }
    }
    ....

当然,您可能想要创建一个特殊的方法来处理通知并在两种情况下调用(使用NSDictionary * 参数),这样您的代码将更具可读性。有时 APNS 通知在应用程序运行时也很有用 - 可能会使用空通知(没有负载)来触发与服务器的数据同步以避免轮询。

【讨论】:

    【解决方案3】:

    您可以在应用启动时获取到达通知,使用以下代码(例如:在 application:didFinishLaunchingWithOptions):

    NSDictionary *remoteNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    

    这里有更详尽的解释:How to manage notification when users click on badge

    【讨论】:

      【解决方案4】:

      你可以这样处理

      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
      
          // Checking if app was launched from the notification
          if (launchOptions != nil) {
      
              NSDictionary *dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
      
              if (dictionary != nil){
                  // Read dictionary and do something since the app
                  // was launched from the notification.
              }
      
          }
      

      这里是字典对象包含的示例

      NSString *message = @"";
      NSString *badge = @"";
      NSString *sound = @"";
      
      if([dictionary objectForKey:@"alert"]) {
          message = [dictionary objectForKey:@"alert"]; 
      }
      
      if([dictionary objectForKey:@"badge"]) {
          badge = [dictionary objectForKey:@"badge"]; 
      }
      
      
      if([dictionary objectForKey:@"sound"]) {
          sound = [dictionary objectForKey:@"sound"]; 
      }
      

      希望对你有帮助!

      【讨论】:

        猜你喜欢
        • 2017-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-24
        相关资源
        最近更新 更多