在iOS 開發(fā)中項(xiàng)目會集成推送功能, 推送的三方很多,如友盟,極光推送.他們的推送原理大致差不多,文檔也寫的很詳細(xì).因公司項(xiàng)目中用到了友盟推送,以實(shí)際項(xiàng)目為例進(jìn)行總結(jié),僅供參考.
1.推送原理:#####
*1.拿到device token - *** App注冊推送 -> 通過設(shè)備向(APNS)蘋果推送服務(wù)器請求device token (圖中上半部分) -> APP 拿到token
2.通過token 發(fā)送推送 - 拿到 token 后,將token 發(fā)送到服務(wù)器 -> 要發(fā)送通知時,服務(wù)器將token 和 message 發(fā)送到APNS -> APNS 推送消息到APP(圖中下半部分)
2.配置證書:#####
證書這一塊的話友盟推送文檔中介紹的很詳細(xì),可參考 iOS證書配置指南.
與正常配置證書差不多,不同之處的話有一下三點(diǎn):
1.創(chuàng)建APPid時,要點(diǎn)選Push Notification選項(xiàng)
2.創(chuàng)建推送證書,推送證書分為開發(fā)環(huán)境和生產(chǎn)環(huán)境
3.導(dǎo)出p12文件,在友盟上傳p12文件#####
4.項(xiàng)目集成#####
1.導(dǎo)入SDK
2.引入庫文件
3.配置(可選項(xiàng))
SDK采用ARC管理內(nèi)存,非ARC項(xiàng)目也是默認(rèn)支持
用了-all_load箭窜,可能需要添加libz的庫:
TARGETS -> Build Phases ->Link Binary With Libraries -> + ->libz.dylib
3.打開推送開關(guān)
5.添加代碼#####
初始化 iOS 10.0以下
//初始化
[UMessage startWithAppkey:@"your appkey" launchOptions:launchOptions];
//注冊通知
[UMessage registerForRemoteNotifications];
初始化 iOS 10.0
//初始化
[UMessage startWithAppkey:@"your appkey" launchOptions:launchOptions];
//注冊通知
[UMessage registerForRemoteNotifications];
//iOS10必須加下面這段代碼毯焕。
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
//設(shè)置代理
center.delegate=self;
//授權(quán)
UNAuthorizationOptions types10=UNAuthorizationOptionBadge|UNAuthorizationOptionAlert|UNAuthorizationOptionSound;
[center requestAuthorizationWithOptions:types10 completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
//點(diǎn)擊允許
} else {
//點(diǎn)擊不允許
}
}];
為通知添加按鈕 iOS 10.0以下
UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc] init];
action1.identifier = @"action1_identifier";
action1.title=@"打開應(yīng)用";
action1.activationMode = UIUserNotificationActivationModeForeground;//當(dāng)點(diǎn)擊的時候啟動程序
UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init]; //第二按鈕
action2.identifier = @"action2_identifier";
action2.title=@"忽略";
action2.activationMode = UIUserNotificationActivationModeBackground;//當(dāng)點(diǎn)擊的時候不啟動程序,在后臺處理
action2.authenticationRequired = YES;//需要解鎖才能處理
action2.destructive = YES;
UIMutableUserNotificationCategory *actionCategory1 = [[UIMutableUserNotificationCategory alloc] init];
actionCategory1.identifier = @"category1";//這組動作的唯一標(biāo)示
[actionCategory1 setActions:@[action1,action2] forContext:(UIUserNotificationActionContextDefault)];
NSSet *categories = [NSSet setWithObjects:actionCategory1, nil];
[UMessage registerForRemoteNotifications:categories];
為通知添加按鈕 iOS 10.0
UNNotificationAction *action1_ios10 = [UNNotificationAction actionWithIdentifier:@"action1_ios10_identifier" title:@"打開應(yīng)用" options:UNNotificationActionOptionForeground];
UNNotificationAction *action2_ios10 = [UNNotificationAction actionWithIdentifier:@"action2_ios10_identifier" title:@"忽略" options:UNNotificationActionOptionForeground];
//UNNotificationCategoryOptionNone
//UNNotificationCategoryOptionCustomDismissAction 清除通知被觸發(fā)會走通知的代理方法
//UNNotificationCategoryOptionAllowInCarPlay 適用于行車模式
UNNotificationCategory *category1_ios10 = [UNNotificationCategory categoryWithIdentifier:@"category101" actions:@[action1_ios10,action2_ios10] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
NSSet *categories_ios10 = [NSSet setWithObjects:category1_ios10, nil];
[center setNotificationCategories:categories_ios10];
接收通知 iOS 10.0以下
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
//關(guān)閉友盟自帶的彈出框
[UMessage setAutoAlert:NO];
[UMessage didReceiveRemoteNotification:userInfo];
//userInfo 中存有接受通知的信息
self.userInfo = userInfo;
//判斷APP的狀態(tài)
if([UIApplication sharedApplication].applicationState = UIApplicationStateActive) {
}
}
接收通知 iOS 10.0
//處理前臺收到通知的代理方法
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
NSDictionary * userInfo = notification.request.content.userInfo;
//判斷是否為遠(yuǎn)程推送的通知類型
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
//應(yīng)用處于前臺時的遠(yuǎn)程推送接受
//關(guān)閉友盟自帶的彈出框
[UMessage setAutoAlert:NO];
//必須加這句代碼
[UMessage didReceiveRemoteNotification:userInfo];
}else{
//應(yīng)用處于前臺時的本地推送接受
}
//當(dāng)應(yīng)用處于前臺時提示設(shè)置,需要哪個可以設(shè)置哪一個
completionHandler(UNNotificationPresentationOptionSound|UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionAlert);
}
//iOS10新增:處理后臺點(diǎn)擊通知的代理方法
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
NSDictionary * userInfo = response.notification.request.content.userInfo;
//判斷是否為遠(yuǎn)程推送的通知類型
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
//應(yīng)用處于后臺時的遠(yuǎn)程推送接受
//必須加這句代碼
[UMessage didReceiveRemoteNotification:userInfo];
}else{
//應(yīng)用處于后臺時的本地推送接受
}
}
6.調(diào)試#####
獲取devicetoken,這個devicetoken 會在測試模式中添加設(shè)備使用
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
//用戶可以在這個方法里面獲取devicetoken
// NSLog(@"%@",[NSString stringWithFormat:@"%@",[NSData dataWithData:deviceToken]]);
NSLog(@"%@",[[[[deviceToken description] stringByReplacingOccurrencesOfString: @"<" withString: @""]
stringByReplacingOccurrencesOfString: @">" withString: @""]
stringByReplacingOccurrencesOfString: @" " withString: @""]);
NSString *deviceStr = [NSString stringWithFormat:@"%@",[[[[deviceToken description] stringByReplacingOccurrencesOfString: @"<" withString: @""]
stringByReplacingOccurrencesOfString: @">" withString: @""]
stringByReplacingOccurrencesOfString: @" " withString: @""]];
NSLog(@"%@", deviceStr);
打印出這個devicetoken后,就可以進(jìn)入友盟推送
1.進(jìn)入友盟推送點(diǎn)擊以添加好的設(shè)備
2.點(diǎn)擊測試模式菜單,點(diǎn)擊添加設(shè)備按鈕添加設(shè)備
3.填寫測試推送的內(nèi)容
測試內(nèi)容中的參數(shù)和設(shè)備推送按鈕標(biāo)識符,根據(jù)自己的項(xiàng)目填寫就好
點(diǎn)擊立即發(fā)送后的測試結(jié)果如下