一贴浙、 本地推送
1-1 本地推送邏輯
UNNotificationContent//推送內(nèi)容
推送內(nèi)容
標(biāo)題/副標(biāo)題/提醒/聲音...
UNNotificationTrigger //推送觸發(fā)者
UNPushNotificationTrigger
UNTimeIntervalNotificationTrigger //時(shí)間間隔觸發(fā)器
UNCalendarNotificationTrigger //日歷觸發(fā)器
UNLocationNOtificationTrigger //位置觸發(fā)器
UNNotificationRequest
封裝Content 和 Trigger為統(tǒng)一格式
交給NotificationCenter處理
UNUserNotificationCenter
處理推送權(quán)限
接收和移除NotificationRequest
UNUserNotificationCenterDelegate
即將展示推送的處理
用戶進(jìn)行交互行為的處理
1-2 實(shí)現(xiàn)本地推送
#import "GTNotifation.h"
#import <UserNotifications/UserNotifications.h>
@interface GTNotifation()<UNUserNotificationCenterDelegate>
@end
@implementation GTNotifation
+ (GTNotifation *)notificationManager{ //設(shè)置單例
static GTNotifation *manager;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [[GTNotifation alloc] init];
});
return manager;
}
/// 檢查申請(qǐng)用戶通知權(quán)限
- (void)checkNotificationAuthorization{
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:UNAuthorizationOptionBadge|UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError * _Nullable error) {
if(granted){
[self _pushLocalNotification];//觸發(fā)一次推送
}
}];
}
/// 發(fā)送推送消息
-(void)_pushLocalNotification{
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.badge = @(1);
content.title = @"標(biāo)題";
content.body = @"內(nèi)容";
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval: 30.f repeats:NO];//30秒后;不重復(fù)觸發(fā)
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"_pushLocalNotification" content: content trigger: trigger];
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError *_Nullable error){
}];
}
#pragma mark - UNUserNotificationCenterDelegate
/// 將要展示通知
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
completionHandler(UNNotificationPresentationOptionAlert); //調(diào)用本地需要把Alert展示出來(lái)
}
/// 收到了通知--用戶點(diǎn)擊通知來(lái)到這里
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler{
//處理業(yè)務(wù)邏輯: 比如跳轉(zhuǎn)頁(yè)面
completionHandler();//調(diào)用系統(tǒng)的完成方法
}
@end
二杆逗、遠(yuǎn)程推送
1-1漏隐、APNs
APNs : Apple Push Notification services
APNs的作用:
防止每個(gè)App都要維持連接
保證連接帶來(lái)安全性和性能的挑戰(zhàn)
推送的流程
App 發(fā)送 UDID和BundleID 給APNs, 生成deviceToken
App 發(fā)送deviceToken 給服務(wù)器
服務(wù)器將信息和設(shè)備 deviceToken 發(fā)送給APNs
APNs 根據(jù) deviceToken 進(jìn)行推送
與本地推送不同:
推送數(shù)據(jù)不是由本地代碼生成
代碼中只需要接收和處理通知--UNUserNotificationCenterDelegate
需要服務(wù)器與APNs配合進(jìn)行推送
證書的配置/Capabilities配置--后續(xù)的證書課程詳細(xì)講解
注冊(cè)DeviceToken
注冊(cè)獲得DeviceToken
registerForRemoteNotifications
UIApplicationDelete 回調(diào)注冊(cè)結(jié)果
推送服務(wù)基本流程:
本地: 權(quán)限申請(qǐng)--> 生成Content、選擇Trigger食店、生成Request-->接收處理-->業(yè)務(wù)層回調(diào)
遠(yuǎn)程: 權(quán)限申請(qǐng)--> 遠(yuǎn)程推送注冊(cè)-->APNs + 系統(tǒng)-->接收處理-->業(yè)務(wù)層回調(diào)
第三方的推送平臺(tái)
極光推送/信鴿推送
無(wú)須自己維護(hù)后臺(tái)/全平臺(tái)(Android)
1-2 實(shí)現(xiàn)遠(yuǎn)程推送
?在自定義的GTNotifation.m中:
#import "GTNotifation.h"
#import <UserNotifications/UserNotifications.h>
@interface GTNotifation()<UNUserNotificationCenterDelegate>
@end
@implementation GTNotifation
+ (GTNotifation *)notificationManager{ //設(shè)置單例
static GTNotifation *manager;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [[GTNotifation alloc] init];
});
return manager;
}
/// 檢查申請(qǐng)用戶通知權(quán)限
- (void)checkNotificationAuthorization{
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:UNAuthorizationOptionBadge|UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError * _Nullable error) {
if(granted){
//必須要在主線程執(zhí)行, 獲取deviceToken
dispatch_async(dispatch_get_main_queue(), ^{
//(向遠(yuǎn)程推送注冊(cè)deviceToken)向蘋果的遠(yuǎn)程服務(wù)器APNs進(jìn)行請(qǐng)求deviceToken
[[UIApplication sharedApplication] registerForRemoteNotifications];
});
//我把deviceToken傳給我們服務(wù)器, 我們服務(wù)器需要用這個(gè)deviceToken來(lái)告訴 蘋果的APNs具體有哪些設(shè)備需要接收這條遠(yuǎn)程推送.
}
}];
}
/*可以用開源軟件"Pusher"來(lái)模擬遠(yuǎn)程push:
在項(xiàng)目"Capabilities"-->開啟"Push Notification"
注意:遠(yuǎn)程推送的內(nèi)容需要Json的格式
選擇推送證書
deviceToken
{"aps":{"alert:"推送標(biāo)題","badge":1,"sound":"default"}}
*/
#pragma mark - UNUserNotificationCenterDelegate
/// 將要展示通知
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
completionHandler(UNNotificationPresentationOptionAlert); //調(diào)用本地需要把Alert展示出來(lái)
}
/// 收到了通知--用戶點(diǎn)擊通知來(lái)到這里
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler{
//處理業(yè)務(wù)邏輯: 比如跳轉(zhuǎn)頁(yè)面
completionHandler();//調(diào)用系統(tǒng)的完成方法
}
?在AppDelegate.m中:
#pragma mark - UIApplicationDelegate
/// 遠(yuǎn)程推送服務(wù)器注冊(cè)成功的回調(diào), 并返回deviceToken
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
//GTNotifation中實(shí)現(xiàn)
}
/// 遠(yuǎn)程推送服務(wù)器注冊(cè)失敗的回調(diào)
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
}
/*總結(jié):
遠(yuǎn)程推送證書&代碼實(shí)現(xiàn):
注冊(cè)Token/自定義發(fā)送
接收推送消息進(jìn)行業(yè)務(wù)處理
遠(yuǎn)程推送內(nèi)容:
Json數(shù)據(jù)格式
https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingNotificationPayload.html
遠(yuǎn)程推送調(diào)試工具:
Pusher
搭建自己的服務(wù)器平臺(tái)
*/
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者