极光推送(registrationID的使用)

视频https://community.jiguang.cn/t/jpush-ios-sdk/4247

1.iOS SDK 概述http://docs.jiguang.cn/jpush/client/iOS/ios_sdk/

APNs通知:

应用退出,后台以及打开状态都能收到APNS———>能够拿到通知的内容等。

如果应用后台或退出,会有系统的APNS提醒。如果应用处于打开状态,则不展示———>不以badge sound alert形式展现。

2.iOS SDK 集成指南http://docs.jiguang.cn/jpush/client/iOS/ios_guide_new/

SDK说明

创建应用(开发环境 生产环境)

配置工程(允许Xcode7支持Http传输方法)

3.iOS SDK 调试指南http://docs.jiguang.cn/jpush/client/iOS/ios_debug_guide/

iOS SDK 调试思维导图

确认证书

开发环境测试

发布环境测试

4.iOS 证书设置指南***********http://docs.jiguang.cn/jpush/client/iOS/ios_cer_guide/

5.iOS 新特性汇总http://docs.jiguang.cn/jpush/client/iOS/ios_new_fetures/

iOS 10 Service Extension—>Xcode没有发现Notification Service Extension这个文件

6.iOS SDK APIhttp://docs.jiguang.cn/jpush/client/iOS/ios_api/

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

//(第一步):注册极光推送

if([[UIDevicecurrentDevice].systemVersionfloatValue] >=10.0) {

#ifdef NSFoundationVersionNumber_iOS_9_x_Max

JPUSHRegisterEntity* entity = [[JPUSHRegisterEntityalloc]init];

entity.types=UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound;

[JPUSHServiceregisterForRemoteNotificationConfig:entitydelegate:self];

#endif

}elseif([[UIDevicecurrentDevice].systemVersionfloatValue] >=8.0) {

//可以添加自定义categories

[JPUSHServiceregisterForRemoteNotificationTypes:(UIUserNotificationTypeBadge|

UIUserNotificationTypeSound|

UIUserNotificationTypeAlert)

categories:nil];

}else{

//categories必须为nil

[JPUSHServiceregisterForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge|

UIRemoteNotificationTypeSound|

UIRemoteNotificationTypeAlert)

categories:nil];

}

//如不需要使用IDFA,advertisingIdentifier可为nil———>>IDFA是什么?

[JPUSHServicesetupWithOption:launchOptionsappKey:JPushSDK_AppKey

channel:nil

apsForProduction:isProduction

advertisingIdentifier:nil];

//(第二步):2.1.9版本新增获取registration id block接口——>>Jpush是以广播的形势发送id,发送成功就回调用block块

[JPUSHServiceregistrationIDCompletionHandler:^(intresCode,NSString*registrationID) {

if(resCode ==0){

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

JPushregistrationID= registrationID;

NSLog(@"7777777JPushRegisterID:%@",JPushregistrationID);//测试OK

// 2.存本地

[[NSUserDefaultsstandardUserDefaults]setObject:JPushregistrationIDforKey:JPushID];

[[NSUserDefaultsstandardUserDefaults]synchronize];

// 3.跟新极光token接口

//判断是否登录(已登录:跟新未登录:不做处理)

BOOLisLogin = [MyStringisOnline];

if(!isLogin) {

}else{

//"极光推送token更新"(新增)接口

[selfupdateJPushTokenWith:registrationID];

}

}

else{

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

}

}];

//这个判断是在程序没有运行的情况下收到通知并点击通知时---逻辑-->>跳转物流中心页面

if(launchOptions) {

NSDictionary* remoteNotification = [launchOptionsobjectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

if(remoteNotification) {

NSLog(@"推送消息==== %@",remoteNotification);

//跳转物流中心页面(因为发的通知是物流到货通知)

[selfgoToMessageViewController];

}

}

//收到自定消息(通过监听者来监听(纯测试用))

NSNotificationCenter*defaultCenter = [NSNotificationCenterdefaultCenter];

[defaultCenteraddObserver:self

selector:@selector(networkDidReceiveMessage:)

name:kJPFNetworkDidReceiveMessageNotification

object:nil];

returnYES;

}

#pragma mark--->>(第三步)注册DeviceToke

- (void)application:(UIApplication*)application

didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {

/// Required -注册DeviceToken

[JPUSHServiceregisterDeviceToken:deviceToken];

NSLog(@"deviceToken%@",deviceToken);

}

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(nonnullNSError*)error {

NSLog(@"注册远程失败");

}

#pragma mark

#pragma mark---->>(第四步)获取APNs(通知)推送内容(不同系统版本适配不同方法)

- (void)application:(UIApplication*)application

didReceiveRemoteNotification:(NSDictionary*)userInfo {

[JPUSHServicehandleRemoteNotification:userInfo];

NSLog(@"iOS6及以下系统,收到通知:%@", [selflogDic:userInfo]);

}

- (void)application:(UIApplication*)application

didReceiveRemoteNotification:(NSDictionary*)userInfo

fetchCompletionHandler:

(void(^)(UIBackgroundFetchResult))completionHandler {

[JPUSHServicehandleRemoteNotification:userInfo];

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

//应用正处于前台状态下,不会收到推送消息,因此在此处需要额外处理一下

if(application.applicationState==UIApplicationStateActive) {

UIAlertController*alert = [selfalertControllerTitle:@"收到推送消息"message:userInfo[@"aps"][@"alert"]];

[self.window.rootViewControllerpresentViewController:alertanimated:YEScompletion:nil];

}

if(application.applicationState==UIApplicationStateInactive) {

[selfgoToMessageViewController];

}

completionHandler(UIBackgroundFetchResultNewData);

}

//本地通知的处理

- (void)application:(UIApplication*)application

didReceiveLocalNotification:(UILocalNotification*)notification {

[JPUSHServiceshowLocalNotificationAtFront:notificationidentifierKey:nil];

}

#ifdef NSFoundationVersionNumber_iOS_9_x_Max

#pragma mark- JPUSHRegisterDelegate

//App在前台获取通知———>但是不能展现

- (void)jpushNotificationCenter:(UNUserNotificationCenter*)centerwillPresentNotification:(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;//推送消息的标题

//UNPushNotificationTrigger(远程通知触发)

if([notification.request.triggerisKindOfClass:[UNPushNotificationTriggerclass]]) {

[JPUSHServicehandleRemoteNotification:userInfo];

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

}

//本地通知触发

else{

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

}

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

}

//点击通知进入App

- (void)jpushNotificationCenter:(UNUserNotificationCenter*)centerdidReceiveNotificationResponse:(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;//推送消息的标题

//UNPushNotificationTrigger(远程通知触发)

if([response.notification.request.triggerisKindOfClass:[UNPushNotificationTriggerclass]]) {

[JPUSHServicehandleRemoteNotification:userInfo];

NSLog(@"iOS10程序关闭后 通过点击推送进入程序弹出的通知:%@", [selflogDic:userInfo]);

[selfgoToMessageViewController];

}

//本地通知触发

else{

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

}

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

}

#endif

// log NSSet with UTF8

// if not ,log will be \Uxxx

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

if(![diccount]) {

returnnil;

}

NSString*tempStr1 =

[[dicdescription]stringByReplacingOccurrencesOfString:@"\\u"

withString:@"\\U"];

NSString*tempStr2 =

[tempStr1stringByReplacingOccurrencesOfString:@"\""withString:@"\\\""];

NSString*tempStr3 =

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

NSData*tempData = [tempStr3dataUsingEncoding:NSUTF8StringEncoding];

NSString*str =

[NSPropertyListSerializationpropertyListFromData:tempData

mutabilityOption:NSPropertyListImmutable

format:NULL

errorDescription:NULL];

returnstr;

}

#pragma mark弹窗处理

-(UIAlertController*)alertControllerTitle:(NSString*)title

message:(NSString*)msg{

UIAlertController*alertController = [UIAlertControlleralertControllerWithTitle:titlemessage:msgpreferredStyle:UIAlertControllerStyleAlert];//Alert警告

//通过UIAlertActionStyle,您可以选择如下三种动作样式:常规(default)、取消(cancel)以及警示(destruective)。

UIAlertAction*cancelAction = [UIAlertActionactionWithTitle:@"知道了"style:UIAlertActionStyleCancelhandler:nil];

__weaktypeof(self)weakSelf =self;

UIAlertAction*doAction = [UIAlertActionactionWithTitle:@"去看看"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction) {

//取出物流消息控制器

[weakSelfgoToMessageViewController];

}];

[alertControlleraddAction:cancelAction];

[alertControlleraddAction:doAction];

returnalertController;

}

7.iOS SDK FAQhttp://docs.jiguang.cn/jpush/client/iOS/ios_faq/

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

推荐阅读更多精彩内容