靜默推送是干嘛的?有什么作用道宅,蘋果為何提供靜默推送食听?如何去觸發(fā)靜默通知?
一污茵、靜默通知調(diào)用方法的研讀
通過觀看 WWDC 樱报,方知有個(gè)靜默通知東東,于是想著如何實(shí)現(xiàn)它泞当,它有什么注意點(diǎn)迹蛤?通過后面查詢資料才知道,靜默遠(yuǎn)程通知會(huì)調(diào)用如下的方法。
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler NS_AVAILABLE_IOS(7_0);
上面的方法作用是告訴應(yīng)用盗飒,遠(yuǎn)程通知已經(jīng)來了穷缤,遠(yuǎn)程通知捎點(diǎn)東西,讓應(yīng)用自己去拿一下箩兽。
參數(shù)說明
application application 單例對象津肛。
userInfo 這個(gè)參數(shù)是遠(yuǎn)程通知帶來的東西,userInfo 里面可能包含 badge 和 通知聲音汗贫,和通知展示給用戶的信息身坐,通知的標(biāo)識和自定義的數(shù)據(jù)哦。遠(yuǎn)程服務(wù)器推送的內(nèi)容是 Json 格式的字典落包,而 iOS 會(huì)將其推送的內(nèi)容轉(zhuǎn)化成 NSDictonary 對象部蛇。
handler 當(dāng)下載操作完成時(shí),handler 將會(huì)被執(zhí)行咐蝇。當(dāng)調(diào)用 handler 方法時(shí)涯鲁,請傳入 UIBackgroundFetchResult 枚舉值最能描述下載操作結(jié)果。你一定要盡快的調(diào)用 handler 方法
UIBackgroundFetchResult 枚舉的意思
UIBackgroundFetchResultNewData
新的數(shù)據(jù)下載成功UIBackgroundFetchResultNoData
沒有新的數(shù)據(jù)下載UIBackgroundFetchResultFailed
下載新的數(shù)據(jù)失敗
二有序、討論
- 1抹腿、 靜默通知能下載多久?
有30秒的下載時(shí)間
官方說明:Your app has up to 30 seconds of wall-clock time to process the notification and call the specified completion handler block. In practice, you should call the handler block as soon as you are done processing the notification旭寿。
2警绩、靜默通知能在前臺下載嗎?
已經(jīng)驗(yàn)證盅称,是前臺和后臺都能下載肩祥。3、靜默通知后臺下載有限制嗎缩膝?
有限制混狠,蘋果會(huì)監(jiān)聽下載的時(shí)間、電量消耗疾层。如果應(yīng)用處理靜默通知下載去花費(fèi)太多的電量将饺,將來的靜默通知可能不能及時(shí)喚醒應(yīng)用的。
官方說明: The system tracks the elapsed time, power usage, and data costs for your app’s background downloads. Apps that use significant amounts of power when processing remote notifications may not always be woken up early to process future notifications.
三云芦、配置和代碼實(shí)現(xiàn)
- 1俯逾、配置
如果你不明如何配置遠(yuǎn)程推送證書,請參考從零開始創(chuàng)建iOS遠(yuǎn)程推送證書里面有舅逸,教你如何配置推送證書桌肴。
另外要在 Xcode 中操作兩個(gè)步驟,一個(gè)是允許后臺下載琉历,另一個(gè)是允許接受遠(yuǎn)程通知坠七。
允許后臺下載
- 2水醋、代碼
#import "AppDelegate.h"
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
@interface AppDelegate ()<UNUserNotificationCenterDelegate>
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//注冊通知
if ([[UIDevice currentDevice].systemVersion doubleValue]>= 10.0) {
//iOS 10 特有
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:UNAuthorizationOptionBadge | UNAuthorizationOptionAlert | UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
NSLog(@"打印成功");
}
}];
center.delegate = self;
}else if([[UIDevice currentDevice].systemVersion doubleValue]>8){
//分類
UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc] init];
//分類標(biāo)識
category.identifier = @"iOS 8 Category id";
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:[NSSet setWithObject:category]];
[application registerUserNotificationSettings:settings];
}
[[UIApplication sharedApplication] registerForRemoteNotifications];
return YES;
}
#pragma mark - iOS 10 通知
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
NSLog(@"helloworld");
completionHandler(UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);
}
// The method will be called on the delegate when the user responded to the notification by opening the application, dismissing the notification or choosing a UNNotificationAction. The delegate must be set before the application returns from applicationDidFinishLaunching:.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
completionHandler();
}
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
NSLog(@"%@",[NSString stringWithFormat:@"device Token %@",deviceToken]);
}
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
if (error) {
NSLog(@"%@",error);
}
}
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
NSLog(@"iOS 10 以下收到通知");
}
#pragma mark - 靜默通知
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
NSDictionary *dict = userInfo[@"aps"];
NSLog(@"%@",dict[@"name"]);
completionHandler(UIBackgroundFetchResultNewData);
}
@end
四、推送遠(yuǎn)程內(nèi)容
提供一個(gè)強(qiáng)大的基于 MAC 的推送服務(wù)器彪置,SmartPush拄踪。運(yùn)行 Xcode 會(huì)出現(xiàn)如下圖的彈框。
注意:請使用最新的 notification 格式
- 不能加alert拳魁,如果加入了alert就不是靜默推送了惶桐,本人試過。
- 務(wù)必加"content-available" : 1
- sound感覺也不能加潘懊,加入的話就不是靜默推送姚糊,但是我在測試中有加入,也是可以的授舟,建議不要加救恨。
服務(wù)器推送的內(nèi)容如下。
{
"aps": { "content-available" : 1,"name":"oliver"
}
}
如果服務(wù)器推送的內(nèi)容包含 alert 就不是靜默推送了释树,就是 一般的遠(yuǎn)程推送啦肠槽。
{
"aps": {
"alert": "This is some fancy message.",
"badge": 1,
"sound": "default",
"mutable-content": "1",
"imageAbsoluteString": "http://www.gaoxiaogif.com/d/file/201707/small1dd8ecd6f646f3785778c92ae68bcfda.gif"
,
"title" :"noticefyTitle",
"subtitle":"subtittle","fileType":"gif"
}
}
五、結(jié)語
靜默通知是讓應(yīng)用在后臺悄悄的下載東東奢啥,這樣用戶啟動(dòng)應(yīng)用時(shí)秸仙,會(huì)給用戶一種驚喜,蘋果還是為了提高用戶體驗(yàn)出發(fā)的扫尺。
如果不正確的地方筋栋,請告知,感恩Uぁ!