推送设置
1.推送界面设置
在iOS8 之后,推送可以设置一些操作按钮,即使app在后台甚至是app被杀掉的情况下也能做一下简单的操作,就像微信的快捷回复一样。
- 设置action
UIMutableUserNotificationAction * action1 = [[UIMutableUserNotificationAction alloc] init];
action1.identifier = @"action1";
action1.title=@"忽略";
action1.behavior = UIUserNotificationActionBehaviorDefault; // 操作类型,默认按钮
action1.activationMode = UIUserNotificationActivationModeBackground; // 后台操作,不需要打开app
action1.authenticationRequired = YES; // 是否需要解锁
action1.destructive = YES; // 样式区别
UIMutableUserNotificationAction * action2 = [[UIMutableUserNotificationAction alloc] init];
action2.identifier = @"action2";
action2.title=@"回复";
action2.behavior = UIUserNotificationActionBehaviorTextInput; // 操作类型,输入框
action2.activationMode = UIUserNotificationActivationModeBackground; // 后台操作,不需要打开app
UIMutableUserNotificationAction * action3 = [[UIMutableUserNotificationAction alloc] init];
action3.identifier = @"action3";
action3.title=@"查看";
action3.behavior = UIUserNotificationActionBehaviorDefault; // 操作类型,默认按钮
action3.activationMode = UIUserNotificationActivationModeForeground; // 前台操作,打开app
//如果 activationMode = UIUserNotificationActivationModeForeground,authenticationRequired设置之后不起作用;
//因为如果需要前台操作就需要打开app,那肯定是需要解锁的
action1.authenticationRequired = NO;
- 组装category
// 可以设置多种样式,在推送的时候通过字段category控制样式
//category1
UIMutableUserNotificationCategory * category1 = [[UIMutableUserNotificationCategory alloc] init];
category1.identifier = @"Category1";
[category1 setActions:@[action1,action2,action3] forContext:(UIUserNotificationActionContextDefault)];
//category2
UIMutableUserNotificationCategory * category2 = [[UIMutableUserNotificationCategory alloc] init];
category2.identifier = @"Category2";
[category2 setActions:@[action2] forContext:(UIUserNotificationActionContextDefault)];
- 设置推送
UIUserNotificationSettings *uns = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:[NSSet setWithObjects: category1,category2, nil]];
[[UIApplication sharedApplication] registerUserNotificationSettings: uns];
2.推送红点数设置
对于BadgeNumber,只是一种提醒方式,在系统的通知设置页面可以对相应的app设置显示样式,其中就有一个选项是是否显示图标标记。
对于BadgeNumber的值,在推送消息中用badge
来设置,客户端可以用[UIApplication sharedApplication].applicationIconBadgeNumber
对红点数进行修改。并且每当有推送过来的时候,显示的BadgeNumber都会跟推送中设置的值一致,不会自动累加。
3.推送声音设置
推送声音的音频文件,试了aiff,wav,caf,mp3格式的音频貌似都可以。
将音频文件添加到项目中之后,就可以通过sound
字段来控制推送的声音。
4.推送内容设置
一般来说,推送显示的内容可以通过alert
字段进行直接的设置。同时对于一些固定格式的推送内容,可以配合Localizable.strings
来设置。在Localizable.strings
可以设置一条信息:
"weather" = "今天天气为%@,温度%@度";
推送内容设置为:
"alert":{"loc-args":["晴","15"],"loc-key":"weather"}
5.静默推送
需要设置"content-available" : 1
,既然是静默推送就是不让用户感知,所以alert和sound字段一般都不设置。如果设置了,就会有提示,但本质上讲还是静默推送,因为它让然会默默的调用下面的方法
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler ;
推送处理
一般的推送处理,就是点击推送的消息打开app,如果要增加体验的话可以在点击推送之后做一下操作,比如跳转页面或者刷新数据。
app在不同的状态下,点击推送会触发不同的方法
1、app前台运行,这条推送不会出现在消息栏,但是会触发下面的方法。
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;
2、后台运行,点击推送消息,包括通知栏和顶部提示,启动app
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo ;
4、进程已经被kill,点击推送启动app,launchOptions 有值,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
5、推送有操作按钮,微信的锁屏回复就是通过这个方法处理的
- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void(^)())completionHandler;
6、静默推送,收到静默推送时,会主动调用下面的方法,不需要操作
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler ;
推送内容格式
- 一般形式
{
"aps":{
"alert":"message",
"badge":1,
"category":"Category2",
"sound":"Glass.aiff"
}
}
- 本地拼接推送内容
{
"aps":{
"alert":{
"loc-args":["晴","15"],
"loc-key":"weather"
},
"badge":1,
"category":"Category2",
"sound":"Glass.aiff"
}
}
- 静默推送
{
"aps":{
"alert":{
"loc-args":["晴","15"],
"loc-key":"weather"
},
"badge":1,
"category":"Category2",
"sound":"Glass.aiff",
"content-available" : 1
}
}
搞了个推送工具方便测试
可以通过scheme跳转的方式添加token
添加证书方式为p12文件的base64字符串
网友评论