iOS交互推送

iOS 8提供的3个新类:UIUserNotificationSettings,UIUserNotificationCategory,UIUserNotificationAction以及它们的变体。

和 以前简单地注册通知类型(sounds、banners、alerts)相比,现在你可以注册自定义的通知类别(categories)和动作 (actions)。类别描述了应用自定义的通知类型,并且包含用户能够执行的响应动作。比如,你收到一个通知说某人在社交网上了关注了你,作为回应你可 能会想要关注他或者忽略。

#define NotificationCategoryIdent  @"ACTIONABLE"

#define NotificationActionOneIdent  @"ACTION_ONE"

#define NotificationActionTwoIdent  @"ACTION_TWO"

注册通知

UIMutableUserNotificationAction*action1;

action1=[[UIMutableUserNotificationActionalloc]init];

[action1setActivationMode:UIUserNotificationActivationModeBackground];

[action1setTitle:@"Action1"];

[action1setIdentifier:NotificationActionOneIdent];

[action1setDestructive:NO];

[action1setAuthenticationRequired:NO];

UIMutableUserNotificationAction*action2;

action2=[[UIMutableUserNotificationActionalloc]init];

[action2setActivationMode:UIUserNotificationActivationModeBackground];

[action2setTitle:@"Action2"];

[action2setIdentifier:NotificationActionTwoIdent];

[action2setDestructive:NO];

[action2setAuthenticationRequired:NO];

UIMutableUserNotificationCategory*actionCategory;

actionCategory=[[UIMutableUserNotificationCategoryalloc]init];

[actionCategorysetIdentifier:NotificationCategoryIdent];

[actionCategorysetActions:@[action1,action2]

forContext:UIUserNotificationActionContextDefault];

NSSet*categories=[NSSetsetWithObject:actionCategory];

UIUserNotificationTypetypes=(UIUserNotificationTypeAlert|

UIUserNotificationTypeSound|

UIUserNotificationTypeBadge);

UIUserNotificationSettings*settings;

settings=[UIUserNotificationSettingssettingsForTypes:types

categories:categories];

[[UIApplicationsharedApplication]registerUserNotificationSettings:settings];

}

要发送这个通知类型,只需简单的将category添加到声明里。

{"aps":{"alert":"测试","category":"ACTIONABLE","badge":1,"sound":"default","custom":{"t":"jump","p":"second"}}}

现在为了响应用户选择的操作,你需要在UIApplicationDelegate协议添加两个新方法:

application:handleActionWithIdentifier:forLocalNotification:completionHandler:

application:handleActionWithIdentifier:forRemoteNotification:completionHandler:

用户从你的推送通知中选择一个动作后,该方法将会在后台被调用。

-(void)application:(UIApplication*)applicationhandleActionWithIdentifier:(NSString*)identifierforRemoteNotification:(NSDictionary*)userInfocompletionHandler:(void(^)())completionHandler{

if([identifierisEqualToString:NotificationActionOneIdent]){

NSLog(@"Youchoseaction1.");

}

elseif([identifierisEqualToString:NotificationActionTwoIdent]){

NSLog(@"Youchoseaction2.");

}

if(completionHandler){

completionHandler();

}

}

你可能感兴趣的:(iOS交互推送)