小白集成友盟推送IOS1.1.0(图文并茂)

2016-03-14 12:37:20 |发布者: 安智宝

集成过程

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

注册友盟帐号=》创建推送应用=》下载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   友盟统计sdk

首先进入友盟消息推送的应用中心,创建一个应用,链接地址;

http://message.umeng.com/appList

进入后选择添加应用,如果是开发环境,就选择上传开发证书,准备上架,就选择上传生产证书:

应用创建完成后,点击应用名称进入应用详情页面,进入“应用信息”页面,可以看到应用的AppKey和AppMasterSecret。

3、下载IOS SDK

友盟消息推送iOS SDK下载地址:

http://dev.umeng.com/message/ios/sdk-download   微软应用商店

下载完成后,解压压缩包,找到UMessage_Sdk_1.1.0文件夹,文件夹中包含如下图所示两个文件,这就是我们需要的SDK文件:

4、集成开发

(1)新建一个工程

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。 安卓激活

记录下来这个device_token,然后进入友盟消息推送网站的应用界面,进入开发环境,选择测试设备,添加测试设备。

设备描述用于填写设备标识名称,Device Token填写上一步获得的device_token。

主要功能

从网站控制台发送推送消息,在开发环境下,选择推送测试消息:

手机屏幕顶端接收到通知:

通知中心提示:

应用前台运行时收到推送消息:

功能特色

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成功");

    }

}]

添加标签完成后,即可在网页选择根据标签推送:

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];

添加完成后,可以根据地域推送:

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推送:

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、在测试消息中,可以看到推送消息历史记录:

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包查看

测试日志:

遇到问题

1.创建应用的时候,需要保持Bundle Identifier 要和在苹果开发者网站创建的应用的Bundle ID保持一致,否则无法推送信息;

2.如果不需要自动清除角标,需要在项目中使用[UMessagesetBadgeClear:NO];来关闭友盟推送的自动清空角标功能。

3.友盟消息的发送渠道如果不设置的话,默认是App Store,需要修改的,要使用setChannel方法。

上手难易

友盟推送上手简单,开发者调用SDK的方法时,可以参考方法上的注释,方便快速集成。最新版加入了对iOS8推送的支持很不错。


关于安智宝

    安智宝是主打效果的安卓应用推广平台。通过安智宝,用户可以通过自助方式 进行应用推广。作为效果性的推广平台,安智宝利用云技术智能的进行渠道匹 配,对渠道的高效利用,节约用户的每一分钱。用户可以精确的控制应用投放 每天的投放量、渠道质量(留存率等),做到覆盖广、活跃度高、控制精准。

联系客服

Copyright © 2016 - 2020 anzhibao.com . All Right Reserved.

安智宝  版权所有