Xcode8之极光推送

好久没做推送了,最近越到了一些坑,但顺利搞定,简单说下步骤

一. 首先简单说下证书(注意所以推送相关证书尽量用项目名来命名,bundleid与工程保持一致,命名注意区分测试和发布证书)

1. 首先要有一个99$的苹果开发者账号,新建开发者证书(此处就省略了)

2. 创建appID,要勾选Push Notifications,bundleid要与工程保持一致(Xcode8在工程中选择对应的team后会自动在该账号上创建一个appid,如图),



3. 添加调试设备


iTunes上可获取设备UUID


4. 分别新建测试和发布的推送证书,下载后双击导入



5. 新建描述文件,也是测试和发布一对


6. 打开钥匙串会发现有一对你刚才导入的推送证书,右键到处为p12文件,注意区分测试和发布证书,注意自己设置的密码


7. 打开极光开发者服务(有个人账号),创建应用,应用名就是自己开发项目的名字,上传应用图标,在证书的地方上传刚刚导出的两个p12文件,bundleid与工程一致,然后点击创建应用,创建成功后如图


8. 记录,appkey是写在工程里,Master 是要给服务器的


二、配置工程

1. 首先下载极光的SDK并把需要的导入到工程

2.导入依赖库


3. Xcode8 要在这里打开开关,都为对号则表示正常,证书选自动



4.在appdelegate中倒入头文件


注意打开允许http请求的开关

然后上代码

static NSString *appKey = @"这里填极光上面的appkey";

static NSString *channel = @"Publish channel";

static BOOL isProduction = FALSE;

接受协议<JPUSHRegisterDelegate>

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

/*

......

*/



#pragma mark -- 远程推送

NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];

//Required

if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {

JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];

entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound;

[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];

}

else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {

//可以添加自定义categories

[JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |

UIUserNotificationTypeSound |

UIUserNotificationTypeAlert)

categories:nil];

}

else {

//categories 必须为nil

[JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |

UIRemoteNotificationTypeSound |

UIRemoteNotificationTypeAlert)

categories:nil];

}

//如不需要使用IDFA,advertisingIdentifier 可为nil

[JPUSHService setupWithOption:launchOptions appKey:appKey

channel:channel

apsForProduction:isProduction

advertisingIdentifier:nil];

//2.1.9版本新增获取registration id block接口。

[JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {

if(resCode == 0){

NSLog(@"registrationID获取成功:%@",registrationID);

}

else{

NSLog(@"registrationID获取失败,code:%d",resCode);

}

}];


// Override point for customization after application launch.

return YES;

}

#pragma mark - 获取 device Token

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

// 告诉服务器 服务器保存

[JPUSHService registerDeviceToken:deviceToken];

NSLog(@"%@", deviceToken);

}

#pragma mark - 获取信息

/**

*  当收到远程推送通知时调用

*

*  @param application 应用程序

*  @param userInfo    具体内容

*/

//注册失败

- (void)application:(UIApplication *)application

didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {

NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);

}

#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1

- (void)application:(UIApplication *)application

didRegisterUserNotificationSettings:

(UIUserNotificationSettings *)notificationSettings {

}

// Called when your app has been activated by the user selecting an action from

// a local notification.

// A nil action identifier indicates the default action.

// You should call the completion handler as soon as you've finished handling

// the action.

- (void)application:(UIApplication *)application

handleActionWithIdentifier:(NSString *)identifier

forLocalNotification:(UILocalNotification *)notification

completionHandler:(void (^)())completionHandler {

}

// Called when your app has been activated by the user selecting an action from

// a remote notification.

// A nil action identifier indicates the default action.

// You should call the completion handler as soon as you've finished handling

// the action.

- (void)application:(UIApplication *)application

handleActionWithIdentifier:(NSString *)identifier

forRemoteNotification:(NSDictionary *)userInfo

completionHandler:(void (^)())completionHandler {

}

#endif

// 收到通知

- (void)application:(UIApplication *)application

didReceiveRemoteNotification:(NSDictionary *)userInfo

fetchCompletionHandler:

(void (^)(UIBackgroundFetchResult))completionHandler {

[JPUSHService handleRemoteNotification:userInfo];

//    NSLog(@"iOS7及以上系统,收到通知:%@", [self logDic:userInfo]);

if ([[UIDevice currentDevice].systemVersion floatValue]<10.0 || application.applicationState>0) {

//        [rootViewController addNotificationCount];

}

completionHandler(UIBackgroundFetchResultNewData);

}

- (void)application:(UIApplication *)application

didReceiveLocalNotification:(UILocalNotification *)notification {

[JPUSHService showLocalNotificationAtFront:notification identifierKey:nil];

}

#ifdef NSFoundationVersionNumber_iOS_9_x_Max

#pragma mark- JPUSHRegisterDelegate

- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {

NSDictionary * userInfo = notification.request.content.userInfo;

UNNotificationRequest *request = notification.request; // 收到推送的请求

UNNotificationContent *content = request.content; // 收到推送的消息内容

NSNumber *badge = content.badge;  // 推送消息的角标

NSString *body = content.body;    // 推送消息体

UNNotificationSound *sound = content.sound;  // 推送消息的声音

NSString *subtitle = content.subtitle;  // 推送消息的副标题

NSString *title = content.title;  // 推送消息的标题

if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {

[JPUSHService handleRemoteNotification:userInfo];

NSLog(@"iOS10 前台收到远程通知:%@", [self logDic:userInfo]);

//        [rootViewController addNotificationCount];

}

else {

// 判断为本地通知

NSLog(@"iOS10 前台收到本地通知:{\nbody:%@,\ntitle:%@,\nsubtitle:%@,\nbadge:%@,\nsound:%@,\nuserInfo:%@\n}",body,title,subtitle,badge,sound,userInfo);

}

completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert); // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以设置

}

- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {

NSDictionary * userInfo = response.notification.request.content.userInfo;

UNNotificationRequest *request = response.notification.request; // 收到推送的请求

UNNotificationContent *content = request.content; // 收到推送的消息内容

NSNumber *badge = content.badge;  // 推送消息的角标

NSString *body = content.body;    // 推送消息体

UNNotificationSound *sound = content.sound;  // 推送消息的声音

NSString *subtitle = content.subtitle;  // 推送消息的副标题

NSString *title = content.title;  // 推送消息的标题

if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {

[JPUSHService handleRemoteNotification:userInfo];

//        NSLog(@"iOS10 收到远程通知:%@", [self logDic:userInfo]);

//        [rootViewController addNotificationCount];

}

else {

// 判断为本地通知

NSLog(@"iOS10 收到本地通知:{\nbody:%@,\ntitle:%@,\nsubtitle:%@,\nbadge:%@,\nsound:%@,\nuserInfo:%@\n}",body,title,subtitle,badge,sound,userInfo);

}

completionHandler();  // 系统要求执行这个方法

}

#endif

// if not ,log will be \Uxxx

- (NSString *)logDic:(NSDictionary *)dic {

if (![dic count]) {

return nil;

}

NSString *tempStr1 =

[[dic description] stringByReplacingOccurrencesOfString:@"\\u"

withString:@"\\U"];

NSString *tempStr2 =

[tempStr1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];

NSString *tempStr3 =

[[@"\"" stringByAppendingString:tempStr2] stringByAppendingString:@"\""];

NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];

NSString *str =

[NSPropertyListSerialization propertyListFromData:tempData

mutabilityOption:NSPropertyListImmutable

format:NULL

errorDescription:NULL];

return str;

}


这些代码极光的开发文档里都有

然后连接设备真机调试安装,在设备提示上选择允许发送通知

5. 安装完退出应用,打开极光的应用管理页面,选择要推送消息的应用,选择推送




点击立即发送,打开记录,设备会有提示音,弹窗,推送成功


6. 本人只说出自己的成功经验,如有问题请提出指导意见。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 196,099评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,473评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,229评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,570评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,427评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,335评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,737评论 3 386
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,392评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,693评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,730评论 2 312
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,512评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,349评论 3 314
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,750评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,017评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,290评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,706评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,904评论 2 335

推荐阅读更多精彩内容