如何实现iOS8交互式通知

Aug*_*P A 19 iphone objective-c push-notification ios8

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

提前致谢 :)

Sou*_*pta 35

  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];
Run Code Online (Sandbox Code Playgroud)

  • 相同的代码也适用于远程通知.您只需要在推送有效负载中添加"类别"键."category"键应具有您在代码中定义的相同标识符值. (2认同)
  • 它应该与之前类似.只更改将发送类别键,如"类别":现有有效负载中的"电子邮件",具体取决于您想要的类别 (2认同)

Aug*_*P A 11

我想延长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();
}
Run Code Online (Sandbox Code Playgroud)

您可以在此处引用远程通知有效负载示例iOS8推送通知有效负载