友盟推送

友盟:友盟推送SDK IOS1.1.0集成教程(圖文)

原文:http://bbs.umeng.com/thread-6191-1-1.html

集成過程

客戶端集成如何快速集成友盟推送:

註冊友盟帳號=》創建推送應用=》下載SDK=》集成開發=》測試應用=》發佈應用

1、註冊友盟賬號

友盟開發者賬號的註冊地址:http://www.umeng.com/users/sign_up


2、創建新應用

創建應用之前需要去蘋果開發者網站申請一個App ID和配置一個用於推送的APNs證書,下載並安裝APNs推送證書後,打開鑰匙串從這個證書導出一個.P12的證書文件並保存下來用於創建應用。對APNs證書不瞭解的可以參考友盟提供的證書設置指南,這裏提供網站的鏈接:http://dev.umeng.com/message/ios/license-configuration-guide

首先進入友盟消息推送的應用中心,創建一個應用,鏈接地址;

http://message.umeng.com/appList

進入後選擇添加應用,如果是開發環境,就選擇上傳開發證書,準備上架,就選擇上傳生產證書:

<ignore_js_op>




應用創建完成後,點擊應用名稱進入應用詳情頁面,進入“應用信息”頁面,可以看到應用的AppKey和AppMasterSecret。

<ignore_js_op>




3、下載IOS SDK
友盟消息推送iOS SDK下載地址:
http://dev.umeng.com/message/ios/sdk-download

<ignore_js_op> 
下載完成後,解壓壓縮包,找到UMessage_Sdk_1.1.0文件夾,文件夾中包含如下圖所示兩個文件,這就是我們需要的SDK文件:
<ignore_js_op> 

4、集成開發
(1)新建一個工程
<ignore_js_op> 

Bundle Identifier要與在蘋果官方網站的APP ID中的Bundle ID一致。


(2)添加友盟SDK
將下載的SDK壓縮包中的UMessage_Sdk_1.1.0文件夾加入到項目工程中去,SDK對ARC或非ARC項目都是支持的。
如果在Other Linker Flag中設置了-all_load,則需要添加libz.dylib庫。
(3)基本功能集成
在AppDelegate.m文件中初始化並註冊友盟推送。
在didFinishLaunchingWithOptions:方法中添加如下代碼:
//初始化友盟推送
[UMessagestartWithAppkey:@"YOUR APP KEY"launchOptions:launchOptions];

由於iOS8的推送與以往版本不同,所以要針對iOS8以上的版本進行判斷:

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
//iOS8以上的推送
//options用NSNumericSearch逐個取出數字進行比較。先比較第一個數字,相等的話繼續比第二個,以此類推
//compare比較後的結果,分別是 = <>:NSOrderedSame = 0 NSOrderedAscending = -1 NSOrderedDescending = +1
if ([[[UIDevicecurrentDevice] systemVersion] compare:@"8.0"options:NSNumericSearch] != NSOrderedAscending)
{
UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationActionalloc]init];
action1.identifier = @"action1_identifier";
action1.title = @"Accept";
//當點擊的時候啓動程序
action1.activationMode = UIUserNotificationActivationModeForeground;

UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationActionalloc] init];
action2.identifier = @"action2_identifier";
action2.title = @"Reject";
//當點擊的時候不啓動程序,在後臺處理
action2.activationMode = UIUserNotificationActivationModeBackground;
//需要解鎖才能處理
action2.authenticationRequired = YES;
action2.destructive = YES;

UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategoryalloc] init];
//這組動作的唯一標示
categorys.identifier = @"category1";
[categorys setActions:@[action1,action2]forContext:(UIUserNotificationActionContextDefault)];

UIUserNotificationSettings *userSettings = [UIUserNotificationSettingssettingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlertcategories:[NSSetsetWithObject:categorys]];
[UMessageregisterRemoteNotificationAndUserNotificationSettings:userSettings];
}else{
//註冊消息推送類型
[UMessageregisterForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert];
}
#else
[UMessage registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound];
#endif
(4)推送的一些開關設置:

//打開調試日誌
[UMessagesetLogEnabled:YES];
//自動清空角標,默認YES
[UMessagesetBadgeClear:NO];
//當前APP發送渠道,默認App Store

[UMessagesetChannel:@"App Store"];
(5)獲取deviceToken成功後和推送消息時候的處理
[NSCharacterSetcharacterSetWithCharactersInString:@"<>"]];
NSString *tokenString = [token stringByReplacingOccurrencesOfString:@""withString:@""];
NSLog(@"deviceToken:%@",tokenString);
}
//處理收到的消息推送
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
//應用運行時的消息處理
[UMessagedidReceiveRemoteNotification:userInfo];
}
(6)添加測試設備
由於使用的是開發環境,所以推送的時候,需要在應用中添加一個測試設備用來測試。將前面的代碼集成到項目裏以後,運行項目提示註冊成功後可以在debug信息中看到獲取的DeviceToken,去掉空格和括號後會獲得一個64位的device_token。
<ignore_js_op> 

記錄下來這個device_token,然後進入友盟消息推送網站的應用界面,進入開發環境,選擇測試設備,添加測試設備。
設備描述用於填寫設備標識名稱,Device Token填寫上一步獲得的device_token。
<ignore_js_op> 
<ignore_js_op> 
主要功能
從網站控制檯發送推送消息,在開發環境下,選擇推送測試消息:
<ignore_js_op> 手機屏幕頂端接收到通知:
<ignore_js_op> 
通知中心提示:
<ignore_js_op> 

應用前臺運行時收到推送消息:
<ignore_js_op> 

功能特色
1、根據標籤推送:
需要先在項目中添加標籤代碼:
//添加標籤,addTag參數可以是string,也可以是array
NSArray *array = [self.tagTextfield.textcomponentsSeparatedByString:@","];
[UMessageaddTag:array response:^(id responseObject, NSInteger remain, NSError *error) {
if ([[(NSDictionary *)responseObject objectForKey:@"success"]isEqualToString:@"ok" ]) {
NSLog(@"添加tag成功");
}

}];
刪除標籤代碼:
//刪除標籤,removeTag參數可以是string,也可以是array
NSArray *array = [self.tagTextfield.textcomponentsSeparatedByString:@","];
[UMessageremoveTag:array response:^(id responseObject, NSInteger remain, NSError *error) {
if ([[(NSDictionary *)responseObject objectForKey:@"success"]isEqualToString:@"ok" ]) {
NSLog(@"刪除tag成功");
}
}]
添加標籤完成後,即可在網頁選擇根據標籤推送:
<ignore_js_op> 
2、根據地域推送:
需要在項目中添加設置推送經緯度座標代碼:
以下是按照地域推送設置當前經緯度座標的方法:
CLLocationManager *locManager = [[CLLocationManageralloc] init];
locManager.delegate = self;
locManager.desiredAccuracy = kCLLocationAccuracyBest;
[locManager startUpdatingLocation];
locManager.distanceFilter = 1000.0f;
CLLocation *cllocation = [[CLLocationalloc]initWithLatitude:locManager.location.coordinate.latitudelongitude:locManager.location.coordinate.longitude]; 
[UMessagesetLocation:cllocation];

添加完成後,可以根據地域推送:
<ignore_js_op> 

3、根據別名alias推送
需要在項目中添加別名alias代碼:
//添加別名
[UMessageaddAlias:self.textField.texttype:[self.aliasTypetitleForState:UIControlStateNormal] response:^(id responseObject, NSError *error) {
if ([[(NSDictionary *)responseObject objectForKey:@"success"]isEqualToString:@"ok" ]) {
NSLog(@"添加alias成功");
}];
//刪除別名
[UMessageremoveAlias:self.textField.texttype:[self.aliasTypetitleForState:UIControlStateNormal] response:^(id responseObject, NSError *error) {
if ([[(NSDictionary *)responseObject objectForKey:@"success"]isEqualToString:@"ok" ]) {
NSLog(@"刪除alias成功");
}];

添加完成後,可以根據別名alias推送:
<ignore_js_op> 
4、自定義消息彈窗和統計:
在didReceiveRemoteNotification:方法中添加代碼:


//自定義彈窗,應用內收到消息彈窗
if([UIApplicationsharedApplication].applicationState == UIApplicationStateActive)
{
UIAlertView *alertView = [[UIAlertViewalloc] initWithTitle:@"收到推送消息內容"
message:[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]
delegate:self
cancelButtonTitle:@"確定"
otherButtonTitles:nil];
[alertView show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//統計消息點擊事件
[UMessagesendClickReportForRemoteNotification:self.userInfo];
}

5、在測試消息中,可以看到推送消息歷史記錄:
<ignore_js_op> 

DEMO展示
部分測試demo展示:
- (void)hiddenKeyBoardAndPicker
{
for (UIView *subView inself.view.subviews) {
if ([subView isKindOfClass:[UITextFieldclass]]) {
[subView resignFirstResponder];
}
if ([subView isKindOfClass:[UIPickerViewclass]]) {
[UIViewanimateWithDuration:.3animations:^{
self.typePicker.frame = HIDDEN_FRAME;
}];
}
}
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
{
if (textField == _textField)
{
NSString *ill = [_textField.textstringByReplacingCharactersInRange:range withString:string];
if ([ill length]>20) {
_textField.text=[ill substringToIndex:20];
returnNO;
}
returnYES;
}
returnYES;
}
- (IBAction)aliasTouch:(id)sender {
[UIViewanimateWithDuration:.3animations:^{
self.typePicker.frame = PICKER_FRAME;
}];
}

更多測試DEMO可以下載評測DEMO包查看

測試日誌:
<ignore_js_op> 
遇到問題
1.創建應用的時候,需要保持Bundle Identifier 要和在蘋果開發者網站創建的應用的Bundle ID保持一致,否則無法推送信息;
2.如果不需要自動清除角標,需要在項目中使用[UMessagesetBadgeClear:NO];來關閉友盟推送的自動清空角標功能。
3.友盟消息的發送渠道如果不設置的話,默認是App Store,需要修改的,要使用setChannel方法。
上手難易
友盟推送上手簡單,開發者調用SDK的方法時,可以參考方法上的註釋,方便快速集成。最新版加入了對iOS8推送的支持很不錯。
開發文檔
關於友盟推送的iOS SDK簡單集成指南,請點擊查看
對於蘋果的推送系統實現不太瞭解的,請點擊參考
在使用友盟推送中遇到問題的請點擊參考友盟消息推送問題索引


來自友盟開發者社區(http://bbs.umeng.com
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章