友盟推送---小白都能看的懂的教程(二)

上篇写了在进行推送时的前期的准备,这次就从集成代码开始写了

iOS10中因为增加了通知的扩展,所以对于扩展部分也需要一个APP id,并且这个id要和项目前缀类似。和以前的区别是,这个项目用了两个APP ID,4个Provisioning Profile文件(两个开发环境,两个生产环境)。以bundle id为com.umeng.umpmessage的项目为例:

请按着11.ios证书配置教程配置,他这个是以bundle id为com.umeng.umpmessage创建一个开发的描述文件,上传商店的描述文件,然后在以bundle id为com.umeng.umpmessage.*     创建一个开发的描述文件,上传商店的描述文件,这就他所说的iOS10中因为增加了通知的扩展,所以对于扩展部分也需要一个APP id,并且这个id要和项目前缀类似。和以前的区别是,这个项目用了两个APP ID,4个Provisioning Profile文件。 

友盟推送---小白都能看的懂的教程(二)_第1张图片

在上一篇教程中我们所用的bundleid为com.umeng.pushtestdemo 而且也已经创建了他的两种环境的描述文件,所以我们这需要在创建第二个com.umeng.pushtestdemo.*  创建他的两种环境的描述文件

然后按着11.ios证书配置教程放到自己的工程里面,到此工程里面的证书配置就完成了。然后就是依赖库的添加和代码集成了

应用配置

提示

请先在【友盟+】消息推送管理后台中创建App,获得AppKey和AppSecret

2.1.1  导入SDK

所需SDK文件夹:UMessage_Sdk_x.x.x

请在你的工程目录结构中,右键选择Add->Existing Files…,选择这个文件夹。或者将这个文件夹拖入XCode工程目录结构中,在弹出的界面中勾选Copy items into destination group's folder(if needed), 并确保Add To Targets勾选相应的target。

2.1.2  引入库文件

增加UserNotifications.framework到项目中。

具体操作如下:点击项目---->TARGET---->Build Phases---->Link Binary with Libraries ---->左侧+号---->搜索UserNotifications---->选中UserNotifications.framework---->点击Add

2.1.3  配置(可选)

SDK采用ARC管理内存,非ARC项目也是默认支持,如遇问题,请联系我们

如果您使用了-all_load,可能需要添加libz的库:

TARGETS-->Build Phases-->Link Binary With Libraries--> + -->libz.dylib

2.1.4  打开推送开关

点击项目---->TARGET---->Capabilities,将这里的Push Notification的开关打开,效果如图所示


友盟推送---小白都能看的懂的教程(二)_第2张图片

注意:一定要打开Push Notification,且两个steps都是正确的,否则会报如下错误:Code=3000 "未找到应用程序的“aps-environment”的授权字符串"

2.2  集成推送

打开*AppDelegate.m

2.2.1  引入头文件

引入UMessage.h,UserNotifications.h

2.2.2  设置代理

设置UNUserNotificationCenterDelegate


2.2.3完整的初始化代码


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {


//友盟推送适配Https

[UMessage startWithAppkey:@"5923e1257f2c7455780001cb" launchOptions:launchOptions httpsEnable:YES];

[UMessage openDebugMode:YES];

[UMessage addLaunchMessageWithWindow:self.window finishViewController:[[TabBarController alloc]init]];

[UMessage registerForRemoteNotifications];

//iOS10必须加下面这段代码。

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

center.delegate=self;

UNAuthorizationOptions types10=UNAuthorizationOptionBadge|UNAuthorizationOptionAlert|UNAuthorizationOptionSound;

[center requestAuthorizationWithOptions:types10 completionHandler:^(BOOL granted, NSError * _Nullable error) {

if (granted) {

//点击允许

} else {

//点击不允许

}

}];

//打开日志,方便调试

[UMessage setLogEnabled:YES];

}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken

{

// 注册devicetoken,并把nslog打印出来的devicetoken放到友盟的后台测试设备那

[UMessage registerDeviceToken:deviceToken];

NSLog(@"UmengDeviceToken-%@",[[[[deviceToken description] stringByReplacingOccurrencesOfString: @"<" withString: @""]

stringByReplacingOccurrencesOfString: @">" withString: @""]

stringByReplacingOccurrencesOfString: @" " withString: @""]);

}

//iOS10以下使用这个方法接收通知

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

{

[UMessage didReceiveRemoteNotification:userInfo];

self.userInfo = userInfo;

//定制自定的的弹出框

if([UIApplication sharedApplication].applicationState == UIApplicationStateActive)

{

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"标题"

message:@"Test On ApplicationStateActive"

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]]) {

//应用处于前台时的远程推送接受

//关闭U-Push自带的弹出框

[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{

//应用处于后台时的本地推送接受

}

}


到此就可以实现简单的推送了,但是这个时候点击推送消息并不能真正跳到消息的详情页,这个我在下篇中详细讲解,目前这个推送的集成是简单版的,如果想有富文本推送请参考友盟的官网

你可能感兴趣的:(友盟推送---小白都能看的懂的教程(二))