推送設(shè)置
1.推送界面設(shè)置
在iOS8 之后圣勒,推送可以設(shè)置一些操作按鈕,即使app在后臺(tái)甚至是app被殺掉的情況下也能做一下簡(jiǎn)單的操作摧扇,就像微信的快捷回復(fù)一樣圣贸。
- 設(shè)置action
UIMutableUserNotificationAction * action1 = [[UIMutableUserNotificationAction alloc] init];
action1.identifier = @"action1";
action1.title=@"忽略";
action1.behavior = UIUserNotificationActionBehaviorDefault; // 操作類(lèi)型,默認(rèn)按鈕
action1.activationMode = UIUserNotificationActivationModeBackground; // 后臺(tái)操作扛稽,不需要打開(kāi)app
action1.authenticationRequired = YES; // 是否需要解鎖
action1.destructive = YES; // 樣式區(qū)別
UIMutableUserNotificationAction * action2 = [[UIMutableUserNotificationAction alloc] init];
action2.identifier = @"action2";
action2.title=@"回復(fù)";
action2.behavior = UIUserNotificationActionBehaviorTextInput; // 操作類(lèi)型吁峻,輸入框
action2.activationMode = UIUserNotificationActivationModeBackground; // 后臺(tái)操作,不需要打開(kāi)app
UIMutableUserNotificationAction * action3 = [[UIMutableUserNotificationAction alloc] init];
action3.identifier = @"action3";
action3.title=@"查看";
action3.behavior = UIUserNotificationActionBehaviorDefault; // 操作類(lèi)型庇绽,默認(rèn)按鈕
action3.activationMode = UIUserNotificationActivationModeForeground; // 前臺(tái)操作锡搜,打開(kāi)app
//如果 activationMode = UIUserNotificationActivationModeForeground橙困,authenticationRequired設(shè)置之后不起作用瞧掺;
//因?yàn)槿绻枰芭_(tái)操作就需要打開(kāi)app,那肯定是需要解鎖的
action1.authenticationRequired = NO;
- 組裝category
// 可以設(shè)置多種樣式凡傅,在推送的時(shí)候通過(guò)字段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)];
- 設(shè)置推送
UIUserNotificationSettings *uns = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:[NSSet setWithObjects: category1,category2, nil]];
[[UIApplication sharedApplication] registerUserNotificationSettings: uns];
2.推送紅點(diǎn)數(shù)設(shè)置
對(duì)于BadgeNumber辟狈,只是一種提醒方式,在系統(tǒng)的通知設(shè)置頁(yè)面可以對(duì)相應(yīng)的app設(shè)置顯示樣式夏跷,其中就有一個(gè)選項(xiàng)是是否顯示圖標(biāo)標(biāo)記哼转。
對(duì)于BadgeNumber的值,在推送消息中用badge
來(lái)設(shè)置槽华,客戶(hù)端可以用[UIApplication sharedApplication].applicationIconBadgeNumber
對(duì)紅點(diǎn)數(shù)進(jìn)行修改壹蔓。并且每當(dāng)有推送過(guò)來(lái)的時(shí)候,顯示的BadgeNumber都會(huì)跟推送中設(shè)置的值一致猫态,不會(huì)自動(dòng)累加佣蓉。
3.推送聲音設(shè)置
推送聲音的音頻文件披摄,試了aiff,wav勇凭,caf疚膊,mp3格式的音頻貌似都可以。
將音頻文件添加到項(xiàng)目中之后虾标,就可以通過(guò)sound
字段來(lái)控制推送的聲音寓盗。
4.推送內(nèi)容設(shè)置
一般來(lái)說(shuō),推送顯示的內(nèi)容可以通過(guò)alert
字段進(jìn)行直接的設(shè)置璧函。同時(shí)對(duì)于一些固定格式的推送內(nèi)容傀蚌,可以配合Localizable.strings
來(lái)設(shè)置。在Localizable.strings
可以設(shè)置一條信息:
"weather" = "今天天氣為%@柳譬,溫度%@度";
推送內(nèi)容設(shè)置為:
"alert":{"loc-args":["晴","15"],"loc-key":"weather"}
5.靜默推送
需要設(shè)置"content-available" : 1
喳张,既然是靜默推送就是不讓用戶(hù)感知,所以alert和sound字段一般都不設(shè)置美澳。如果設(shè)置了销部,就會(huì)有提示,但本質(zhì)上講還是靜默推送制跟,因?yàn)樗屓粫?huì)默默的調(diào)用下面的方法
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler ;
推送處理
一般的推送處理舅桩,就是點(diǎn)擊推送的消息打開(kāi)app,如果要增加體驗(yàn)的話(huà)可以在點(diǎn)擊推送之后做一下操作雨膨,比如跳轉(zhuǎn)頁(yè)面或者刷新數(shù)據(jù)擂涛。
app在不同的狀態(tài)下,點(diǎn)擊推送會(huì)觸發(fā)不同的方法
1聊记、app前臺(tái)運(yùn)行撒妈,這條推送不會(huì)出現(xiàn)在消息欄,但是會(huì)觸發(fā)下面的方法排监。
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;
2狰右、后臺(tái)運(yùn)行,點(diǎn)擊推送消息舆床,包括通知欄和頂部提示棋蚌,啟動(dòng)app
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo ;
4、進(jìn)程已經(jīng)被kill挨队,點(diǎn)擊推送啟動(dòng)app谷暮,launchOptions 有值,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
5盛垦、推送有操作按鈕湿弦,微信的鎖屏回復(fù)就是通過(guò)這個(gè)方法處理的
- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void(^)())completionHandler;
6、靜默推送腾夯,收到靜默推送時(shí)颊埃,會(huì)主動(dòng)調(diào)用下面的方法赌蔑,不需要操作
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler ;
推送內(nèi)容格式
- 一般形式
{
"aps":{
"alert":"message",
"badge":1,
"category":"Category2",
"sound":"Glass.aiff"
}
}
- 本地拼接推送內(nèi)容
{
"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
}
}
搞了個(gè)推送工具方便測(cè)試
可以通過(guò)scheme跳轉(zhuǎn)的方式添加token
添加證書(shū)方式為p12文件的base64字符串