1.准备app推送证书,证书创建
钥匙串访问->证书助理->从证书颁发机构申请
2.上Account Developer 创建推送证书
2.1先创建应用,在创建推送证书
2.2 创建 有推送功能的开发或者发布证书
2.3 下载证书 —》再安装证书 —导出证书—等待上传到友盟
3.友盟创建应用
4.项目配置
5.下载sdk集成
http://dev.umeng.com/sdk_integate/ios-integrate-guide/common
6.代码
#import <UMCommon/UMCommon.h> // 公共组件是所有友盟产品的基础组件,必选
#import <UMPush/UMessage.h> // Push组件
#import <UserNotifications/UserNotifications.h>// Push组件必须的系统库
#define UMAppKey @“5b06be93f43e485fae000077"
@interface AppDelegate ()<UNUserNotificationCenterDelegate>
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
// 配置友盟SDK产品并并统一初始化
[UMConfigureinitWithAppkey:UMAppKeychannel:@"App Store"];
// Push组件基本功能配置
[UNUserNotificationCentercurrentNotificationCenter].delegate= self;
UMessageRegisterEntity* entity = [[UMessageRegisterEntityalloc] init];
//type是对推送的几个参数的选择,可以选择一个或者多个。默认是三个全部打开,即:声音,弹窗,角标等
entity.types= UMessageAuthorizationOptionBadge|UMessageAuthorizationOptionAlert;
[UNUserNotificationCentercurrentNotificationCenter].delegate= self;
[UMessageregisterForRemoteNotificationsWithLaunchOptions:launchOptions Entity:entity completionHandler:^(BOOLgranted, NSError* _Nullableerror) {
if(granted) {
// 用户选择了接收Push消息
}else{
// 用户拒绝接收Push消息
}
}];
// Override point for customization after application launch.
returnYES;
}
pragma -mark ———————————————————友盟推送代理————————————————————
//iOS10以下使用这两个方法接收通知,
-(void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo fetchCompletionHandler:(void(^)(UIBackgroundFetchResult))completionHandler
{
[UMessagesetAutoAlert:NO];
//统计点击数
[UMessagedidReceiveRemoteNotification:userInfo];
if([[[UIDevicecurrentDevice] systemVersion]intValue] < 10){
[UMessagedidReceiveRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}
}
//iOS10新增:处理前台收到通知的代理方法
-(void)userNotificationCenter:(UNUserNotificationCenter*)center willPresentNotification:(UNNotification*)notification withCompletionHandler:(void(^)(UNNotificationPresentationOptions))completionHandler{
NSDictionary* userInfo = notification.request.content.userInfo;
if([notification.request.triggerisKindOfClass:[UNPushNotificationTriggerclass]]) {
//应用处于前台时的远程推送接受
//关闭U-Push自带的弹出框
[UMessagesetAutoAlert:NO];
//(前台、后台)的消息处理
[UMessagedidReceiveRemoteNotification: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.triggerisKindOfClass:[UNPushNotificationTriggerclass]]) {
//应用处于后台时的远程推送接受
//(前台、后台)的消息处理
[UMessagedidReceiveRemoteNotification:userInfo];
if(userInfo.count>0){
//消息处理
NSLog(@"跳转到你想要的");
}
}
else{ //应用处于后台时的本地推送接受
}
}
//打印设备注册码,需要在友盟测试设备上自己添加deviceToken
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
[UMessageregisterDeviceToken:deviceToken];
NSLog(@"didRegisterForRemoteNotificationsWithDeviceToken success");
NSLog(@"deviceToken————>>>%@",[[[[deviceToken description] stringByReplacingOccurrencesOfString: @"<"withString: @""]
stringByReplacingOccurrencesOfString: @">"withString: @""]
stringByReplacingOccurrencesOfString: @" "withString: @""]);
}
//放上推送成功截图😁😁
别名推送
我的项目需求是登录成功后绑定别名,退出登录是移除别名
/*
* test:一般用用户ID,后台返回的用户唯一标识
*UMENGTEST:类型随便填,要和后台的类型一样,这点要注意
*/
//多设备绑定别名
[UMessage addAlias:@"test@umeng.com" type:@"UMENGTEST" response:^(id _Nonnull responseObject, NSError * _Nonnull error) {
}];
//重置别名:只允许一个设备收到通知
+ (void)setAlias:(NSString *__nonnull )name type:(NSString * __nonnull)type response:(nullable void (^)(id __nonnull responseObject,NSError *__nonnull error))handle;
//移除别名
[UMessage removeAlias:@"test@umeng.com" type:@"UMENGTEST" response:^(id _Nonnull responseObject, NSError * _Nonnull error) {
}];
还未上线的应用只能在测试环境收到推送,所以要是后台把环境改为正式环境收不到消息不要慌,要是真想在未上线也能收到正式环境推送,那么就搞个正式环境的来玩呗。
- 用AD Hoc方式打包项目,上传蒲公英进行下载安装,就可以收到正式环境的推送啦。
- AD HOC 打包教程
- iOS 友盟推送(二)推送处理