【问题标题】:How to Implement iOS8 Interactive Notification如何实现 iOS8 交互通知
【发布时间】:2014-11-13 19:08:19
【问题描述】:

我正在开发一个支持交互式通知的 iOS8 应用程序。但我不清楚交互式通知支持的功能以及如何发送/处理交互式通知。如果有人能举个例子,那对我很有帮助。

在此先感谢 :)

【问题讨论】:

    标签: objective-c iphone push-notification ios8


    【解决方案1】:

    我想稍微扩展一下 Sourav Gupta 的回答。一旦你完成了 Sourav 解释的内容,你必须实现代表来接收推送通知操作。代表们是,

    - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler {
    
    // Handle actions of local notifications here. You can identify the action by using "identifier" and perform appropriate operations
    
         if(completionHandler != nil)    //Finally call completion handler if its not nil
             completionHandler();
    }
    
    - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler {
    
    // Handle actions of remote notifications here. You can identify the action by using "identifier" and perform appropriate operations
    
         if(completionHandler != nil)    //Finally call completion handler if its not nil
             completionHandler();
    }
    

    您可以在此处参考远程通知负载示例iOS8 Push notification Payload

    【讨论】:

      【解决方案2】:
      1. 首先您需要创建通知操作。
      2. 其次,您需要创建通知类别并设置其操作。您可以设置两个上下文。 UIUserNotificationActionContextDefault 或 UIUserNotificationActionContextMinimal
      3. 第三个你需要创建通知设置并分配上述类别
      4. 第四步是创建本地通知并为其分配类别标识符。

      UIMutableUserNotificationAction *notificationAction1 = [[UIMutableUserNotificationAction alloc] init];
      notificationAction1.identifier = @"Accept";
      notificationAction1.title = @"Accept";
      notificationAction1.activationMode = UIUserNotificationActivationModeBackground;
      notificationAction1.destructive = NO;
      notificationAction1.authenticationRequired = NO;
      
      UIMutableUserNotificationAction *notificationAction2 = [[UIMutableUserNotificationAction alloc] init];
      notificationAction2.identifier = @"Reject";
      notificationAction2.title = @"Reject";
      notificationAction2.activationMode = UIUserNotificationActivationModeBackground;
      notificationAction2.destructive = YES;
      notificationAction2.authenticationRequired = YES;
      
      UIMutableUserNotificationAction *notificationAction3 = [[UIMutableUserNotificationAction alloc] init];
      notificationAction3.identifier = @"Reply";
      notificationAction3.title = @"Reply";
      notificationAction3.activationMode = UIUserNotificationActivationModeForeground;
      notificationAction3.destructive = NO;
      notificationAction3.authenticationRequired = YES;
      
      UIMutableUserNotificationCategory *notificationCategory = [[UIMutableUserNotificationCategory alloc] init];
      notificationCategory.identifier = @"Email";
      [notificationCategory setActions:@[notificationAction1,notificationAction2,notificationAction3] forContext:UIUserNotificationActionContextDefault];
      [notificationCategory setActions:@[notificationAction1,notificationAction2] forContext:UIUserNotificationActionContextMinimal];
      
      NSSet *categories = [NSSet setWithObjects:notificationCategory, nil];
      
      UIUserNotificationType notificationType = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
      UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:notificationType categories:categories];
      
      [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
      
      UILocalNotification* localNotification = [[UILocalNotification alloc] init];
      localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
      localNotification.alertBody = @"Testing";
      localNotification.category = @"Email"; //  Same as category identifier
      [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
      

      【讨论】:

      • 非常感谢@Sourav Gupta。但是远程通知呢?
      • 同样的代码也适用于远程通知。您只需要在推送有效负载中添加“类别”键。 “类别”键应具有您在代码中定义的相同标识符值。
      • 抱歉 Sourav 我不明白这个概念。您能否详细说明您的答案?当我发送远程通知时,有效负载应该包含所有详细信息?
      • 您的代码中可以有 n 个不同的类别,这些类别具有与之关联的不同操作。例如。在上面的代码中,我们有“notificationCategory”具有动作“notificationAction1”、“notificationAction2”、“notificationAction3”。假设您的代码中有 2 个不同的类别。那么你将如何区分一个与另一个。你只需要像这样添加类别键。根据您发送的类别标识符,它将显示相应的操作。 {“警报”:“你好”,“类别”:“电子邮件”}
      • 应该和之前的差不多。唯一的变化是根据您想要的类别在现有有效负载中发送诸如“类别”:“电子邮件”之类的类别键
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多