首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何实现iOS8交互式通知

如何实现iOS8交互式通知
EN

Stack Overflow用户
提问于 2014-09-19 16:36:13
回答 2查看 10K关注 0票数 19

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

提前感谢:)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-09-19 16:56:40

  1. 首先,您需要创建通知操作。
  2. 第二步,您需要创建通知类别并设置它的actions.You,可以为两个上下文设置。UIUserNotificationActionContextDefault或UIUserNotificationActionContextMinimal
  3. 第三步,您需要创建通知设置并分配上述类别
  4. 第四步是创建本地通知并为其分配类别的标识符。

代码语言:javascript
复制
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];
票数 35
EN

Stack Overflow用户

发布于 2014-09-29 18:44:54

我想扩展一下Sourav Gupta的答案。一旦你完成了Sourav所解释的,你就必须实现接收推送通知动作的委托。代表们是,

代码语言:javascript
复制
- (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

票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25929665

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档