IOS推送
推送
●iOS不允許App常駐后臺
●提供服務器主動與用戶相互的功能
●新聞類(熱點新聞的推送)
●本地推送
●由本地應用觸發(鬧鈴/待辦事項)
●無需網路資料,提供和遠程推送統一的互動
●遠程推送
●普通遠程推送
●靜默推送
UserNotifications
UserNotifications
●iOS10之后統一UserNotificatiohsframework
●需要兼容低版本機型
●UNUserNotificationCenter單例管理全部推送
●統一的權限的申請
●在每次App啟動時呼叫requestAuthorizationWithOptions
●系統在首次會出現彈窗,之后保存用戶選擇
●遠程/本地Options相同
大體流程
- 開發者生成
- UNNotificationContent && UNNotificationTrigger
- 封裝成UNNotificationRequest
- 然后給UNUserNotificationCenter
- 最后呼叫UNUserNotificationCenterDelegate
每個的作用
本地推送
UNNotificationContent
●推送內容
●標題/副標題/提醒/聲音…
UNNotificationTrigger
●UNPushNotificationTrigger
●UNTimeIntervalNotificationTrigger
●UNCalendarNotificationTrigger
●UNL ocationNotificationTrigger
UNNotificationRequest
●封裝Content 和Trigger為統一格式
●交給NotificationCenter處理
UNUserNotificationCenter
處理推送權限
●接收和移除止NotificaitonRequest
UNUserNotificationCenterDelegate
●即將展示推送的處理
●用戶進行互動行為的處理
本地推送代碼
- (void)checkNotificationAuthorization{
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:UNAuthorizationOptionBadge | UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError * _Nullable error) {
//UNAuthorizationOptionBadge 右上角圖示
//UNAuthorizationOptionSound 聲音
//如果允許推送 granted == yes
if (granted) {
//本地推送
[self _pushLocalNotification];
});
}
}];
}
- (void)_pushLocalNotification{
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.badge = @(1);
content.title = @"我是推送標題";
content.body = @"我是需要推送的內容";
content.sound = [UNNotificationSound defaultSound];
//30秒推送一次 且不重復
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:30.f repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"_pushLocalNotification" content:content trigger:trigger];
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
//
}];
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
completionHandler(UNNotificationPresentationOptionAlert);
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler {
//處理badge展示邏輯
//點擊之后根據業務邏輯處理
//[UIApplication sharedApplication].applicationIconBadgeNumber = 100;
//處理業務邏輯
completionHandler();
}
遠程推送
推送資料不是由本地代碼生成
●只需要接收和處理通知
●UNUserNotification CenterDelegate
需要服務器與APNs配合進行推送
●證書的配置/ Capabilities配置
●注冊Device Token
●注冊獲得Device Token
●registerForRemoteNotifications
●UlApplicationDelegate回呼注冊結果
步驟
遠程推送證書&代碼實作
●注冊Token/自定義發送
●接收推送訊息進行業務處理
遠程推送基本流程
權限申請,遠程推送注冊,本地推送的生成content選擇Trigger生成request都變成了遠程推送中用APNS+系統進行處理,最后接收處理,業務層回呼
使用第三方推送平臺:極光推送,信鴿推送,無需自己維護后臺,全平臺
遠程推送內容
●Json 資料格式
●https://developer.apple.com/ibrary/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/
CreatingtheNotificationPayload.html
遠程推送除錯工具
●Pusher
●搭建自己的服務器平臺
APNs是啥
Apple Push Notification services
●APNs的作用
●防止每個App都要維持連接
●保證連接帶來安全性和性能的挑戰
●推送的流程
●App發送UDID和BundlelD給APNs,生成device Token
●App發送deviceToken給服務器
●服務器將資訊和設備device Token發送給APNs
●APNs根據device Token進行推送
遠程推送代碼
因為在application獲取register需要在主執行緒執行
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] registerForRemoteNotifications];
});
在appdelegate中代理這兩個方法
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
//盡量收斂到GTNotification中實作
//注冊成功
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
//注冊失敗
}
剩下的將要彈出跟已經彈出推送的方法,跟本地推送一樣
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/297616.html
標籤:其他
