iOS 8.0 之后,在推送中新增加了categories,这是个什么意思呢?它只针对iphne锁屏的状态下。看图就明白了。
在ios8.0 以前(7.0~8.0):在锁屏的情况下,只会显示这样的推送界面:
但是 ios8.0 的系统可以这样:(向右滑动的,出现选择两个选择项)
代码中体现为:
使用了umeng的推送sdk,(所有的第三方推送都差不多,个人推荐友盟的推送,因为友盟的集成简单,比JPUSH还简单。其次是是友盟的数据统计与分析很强大)。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//程序入口
self.window=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[UMessage startWithAppkey:@"5xxxxx3fd98c55858000e8d" launchOptions:launchOptions];
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0)
{
// ios 8.0 之后,可以添加自定义categories
//1.创建消息上面要添加的动作(按钮的形式显示出来)
UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc] init];
action1.identifier = @"action1_identifier";//按钮的标示
action1.title=@"Accept";//按钮的标题
action1.activationMode = UIUserNotificationActivationModeForeground;//当点击的时候启动程序
UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init]; //第二按钮
action2.identifier = @"action2_identifier";
action2.title=@"Reject";
action2.activationMode = UIUserNotificationActivationModeBackground;//当点击的时候不启动程序,在后台处理
action2.authenticationRequired = YES;//需要解锁才能处理,如果action.activationMode = UIUserNotificationActivationModeForeground;则这个属性被忽略;
action2.destructive = YES;
UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];
categorys.identifier = @"category1";//这组动作的唯一标示,推送通知的时候也是根据这个来区分
[categorys setActions:@[action1,action2] forContext:(UIUserNotificationActionContextDefault)];
UIUserNotificationSettings *userSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert
categories:[NSSet setWithObject:categorys]];
[UMessage registerRemoteNotificationAndUserNotificationSettings:userSettings];
} else {
//categories 必须为nil
[UMessage registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge
|UIRemoteNotificationTypeSound
|UIRemoteNotificationTypeAlert];
}
//打开调试日志
[UMessage setLogEnabled:YES];
[self.window makeKeyAndVisible];
return YES;
}
注意这里的:
categorys.identifier = @"category1";//这组动作的唯一标示,推送通知的时候也是根据这个来区分
这也就是,当我门在推送后台推送消息的时候,需要指明的标示,如果没有指明,那么也就不会注册通知的按键行为。