1.創(chuàng)建應(yīng)用
在友盟有賬號的情況下(沒有的請自行創(chuàng)建),創(chuàng)建應(yīng)用之前需要去蘋果開發(fā)者網(wǎng)站申請一個App ID和配置一個用于推送的APNs證書,下載并安裝APNs推送證書后,打開鑰匙串從這個證書導(dǎo)出一個.P12的證書文件并保存下來用于創(chuàng)建應(yīng)用。對APNs證書不了解的可以參考宏創(chuàng)學(xué)院提供的證書設(shè)置指南:
首先在友盟消息推送功能中創(chuàng)建一個應(yīng)用,上傳我們的證書:

創(chuàng)建好應(yīng)用后進入應(yīng)用詳情頁面,點擊應(yīng)用信息,可以看到我們的AppKey和App Master Secret,證書也可以在這里進行修改。


選擇合適版本的SDK,下載下來之后解壓壓縮包,找到UMessage_Sdk_1.2.3(1.2.3為版本號,本文以1.2.3示例) 文件夾,里面就是我們需要的文件了(一個.a的庫文件,一個.h頭文件)。

將上述的UMessage_Sdk_1.2.3文件夾拖入工程,若工程的Other Linker Flag中設(shè)置了-all_load,則需要添加libz.dylib(iOS 9為lib.tbd)庫
在AppDelegate類中引入頭文件Message.h,在AppDelegate.m的application:didFinishLaunchingWithOptions方法中初始化并注冊友盟推送
//初始化友盟推送
[UMessagestartWithAppkey:@"568cd65be0f55ac5610017ea"launchOptions:launchOptions];
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0
//iOS 8之后使用此種方法注冊
if([[[UIDevicecurrentDevice]systemVersion]floatValue]>=8.0){
//遠程消息注冊類型
UIMutableUserNotificationAction*action1=[[UIMutableUserNotificationActionalloc]init];
action1.identifier=@"action1_identifier";
action1.title=@"Accept";
action1.activationMode=UIUserNotificationActivationModeForeground;// 當(dāng)點擊的時候啟動程序
// 第二按鈕
UIMutableUserNotificationAction*action2=[[UIMutableUserNotificationActionalloc]init];
action2.identifier=@"action2_identifier";
action2.title=@"Reject";
// 當(dāng)點擊的時候不啟動程序,在后臺處理
action2.activationMode=UIUserNotificationActivationModeBackground;
// 需要解鎖才能處理,如果action.activationMode = UIUserNotificationActivationModeForeground;則下面這個屬性被忽略
action2.authenticationRequired=YES;
action2.destructive=YES;
UIMutableUserNotificationCategory*categorys=[[UIMutableUserNotificationCategoryalloc]init];
// 這組動作的唯一標(biāo)示
categorys.identifier=@"category1";
[categorys setActions:@[action1,action2]forContext:(UIUserNotificationActionContextDefault)];
UIUserNotificationTypetypes=UIUserNotificationTypeBadge
|UIUserNotificationTypeSound
|UIUserNotificationTypeAlert;
UIUserNotificationSettings*userSettings=[UIUserNotificationSettingssettingsForTypes:types
categories:[NSSetsetWithObject:categorys]];
[UMessageregisterRemoteNotificationAndUserNotificationSettings:userSettings];
}else{
// 遠程消息注冊類型
UIRemoteNotificationTypetypes=UIRemoteNotificationTypeBadge
|UIRemoteNotificationTypeSound
|UIRemoteNotificationTypeAlert;
[UMessageregisterForRemoteNotificationTypes:types];
}
#else
// iOS8.0之前使用此注冊
// 遠程消息注冊類型
UIRemoteNotificationTypetypes=UIRemoteNotificationTypeBadge
|UIRemoteNotificationTypeSound
|UIRemoteNotificationTypeAlert;
[UMessageregisterForRemoteNotificationTypes:types];
#endif
//打開調(diào)試日志
[UMessagesetLogEnabled:YES];
//不自動清空角標(biāo)
[UMessagesetBadgeClear:NO];
//app發(fā)送渠道,默認為APP Store
[UMessagesetChannel:nil];
消息處理方法
//向友盟注冊deviceToken
-(void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken{
//打印deviceToken
NSLog(@"%@",[[[[deviceToken description]stringByReplacingOccurrencesOfString:@"<"withString:@""]stringByReplacingOccurrencesOfString:@">"withString:@""]
stringByReplacingOccurrencesOfString:@" "withString:@""]);
//注冊deviceToken
[UMessageregisterDeviceToken:deviceToken];
}
//注冊遠程通知失敗
-(void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error{
NSLog(@"注冊推送失敗,具體錯誤:%@",error);
}
//收到推送消息時調(diào)用
-(void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo{
[UMessagedidReceiveRemoteNotification:userInfo];
}
如果需要關(guān)閉推送,使用[UMessage unregisterForRemoteNotifications]
由于使用的是開發(fā)環(huán)境,所以推送的時候,需要在友盟注冊的應(yīng)用中添加一個測試設(shè)備用來測試。將前面的代碼集成到項目里以后,使用真機運行項目,提示注冊成功后可以在debug信息中看到獲取的DeviceToken,將DeviceToken拷貝下來備用。

得到DeviceToken后進入友盟網(wǎng)站,依次執(zhí)行下面的操作:
消息推送->立即使用->(你的應(yīng)用)->開發(fā)環(huán)境->測試設(shè)備->添加測試設(shè)備

設(shè)備添加完成后就可以進行推送調(diào)試了,在測試消息中點擊新建測試消息:

參數(shù)和提醒方式自行設(shè)置,發(fā)送方式選擇單播,輸入設(shè)備的DeviceToken,完成后提交即可:


確認后等待推送消息發(fā)送即可


至此,消息推送功能基本完成,更多功能請移步友盟iOS SDK集成指南。
