之前有寫過利用Python自己寫一個推送服務器, 今天說下如果集成友盟的推送服務
在這之前我們需要做一些准備動作
#1. 注冊一個App ID
#2. Enable Push NotificationSerivice, 並創建和下載開發/發布推送證書
#3. 安裝推送證書, 然后把推送證書導出為p12文件
#4. 注冊友盟賬號
#5. 創建一個推送應用, 並上傳推送證書的p12文件和填寫密碼
#6. 下載SDK, 添加到項目中
在AppDelegatez
#import "UMessage.h"
添加一個屬性
@property (nonatomic, strong) NSDictionary *userInfo;
添加協議:
@interface AppDelegate ()<UNUserNotificationCenterDelegate>
設置友盟AppKey
static NSString *UMessageAppKey = @"112345678901234523";
創建一個配置友盟推送的方法
- (void)configureUMessageWithLaunchOptions:(NSDictionary *)launchOptions { //設置AppKey & LaunchOptions [UMessage startWithAppkey:UMessageAppKey launchOptions:launchOptions]; //初始化 [UMessage registerForRemoteNotifications]; //開啟log [UMessage setLogEnabled:YES]; //檢查是否為iOS 10以上版本 if ([[[UIDevice currentDevice] systemVersion] floatValue] < 10.0) { } else { //如果是iOS 10以上版本則必須執行以下操作 UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; center.delegate = self; UNAuthorizationOptions types10 = \ UNAuthorizationOptionBadge | UNAuthorizationOptionAlert |UNAuthorizationOptionSound; [center requestAuthorizationWithOptions:types10 completionHandler:^(BOOL granted, NSError * _Nullable error) { if (granted) { //點擊允許 //這里可以添加一些自己的邏輯 } else { //點擊不允許 //這里可以添加一些自己的邏輯 } }]; } }
協議方法:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { //關閉友盟自帶的彈出框 [UMessage setAutoAlert:NO]; [UMessage didReceiveRemoteNotification:userInfo]; self.userInfo = userInfo; //定制自定的的彈出框 if([UIApplication sharedApplication].applicationState == UIApplicationStateActive) { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"溫馨提示" message:self.userInfo[@"aps"][@"alert"] delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil]; [alertView show]; } } //iOS10新增:處理前台收到通知的代理方法 - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{ NSDictionary * userInfo = notification.request.content.userInfo; if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { //應用處於前台時的遠程推送接受 //關閉友盟自帶的彈出框 [UMessage setAutoAlert:NO]; //必須加這句代碼 [UMessage didReceiveRemoteNotification:userInfo]; }else{ //應用處於前台時的本地推送接受 } //當應用處於前台時提示設置,需要哪個可以設置哪一個 completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionAlert); } //iOS10新增:處理后台點擊通知的代理方法 -(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{ NSDictionary * userInfo = response.notification.request.content.userInfo; if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { //應用處於后台時的遠程推送接受 //必須加這句代碼 [UMessage didReceiveRemoteNotification:userInfo]; }else{ //應用處於后台時的本地推送接受 } } - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { [UMessage sendClickReportForRemoteNotification:self.userInfo]; }
最后是在ApplicationDidFinishLaunch中調用配置友盟推送的方法即可
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //配置友盟推送 [self configureUMessageWithLaunchOptions:launchOptions]; return YES; }