iOS10推送基礎(chǔ)

推送基礎(chǔ)

簡單適配

相對簡單的推送證書以及環(huán)境的問題,我就不在這里講啦你虹,我在這里說的并徘,是指原有工程的適配遣钳。

1.首先我們需要打開下面的開關(guān)。所有的推送平臺饮亏,不管是極光還是什么的耍贾,要想收到推送阅爽,這個是必須打開的喲~

屏幕快照 2016-10-28 上午11.05.49.png

之后,系統(tǒng)會生成一個我們以前沒見過的文件荐开,如圖:

屏幕快照 2016-10-28 上午11.07.29.png

可能產(chǎn)生的問題:之前有朋友反饋過付翁,將開發(fā)環(huán)境由 development 變成 production ,在開關(guān)這里會產(chǎn)生錯誤,如圖:

屏幕快照 2016-10-28 上午11.08.01.png

如果大家點(diǎn)擊Fix issue之后晃听,會驚奇的發(fā)現(xiàn)百侧,APS Environment由 production 又變成 development 了。

解決辦法:沒有做任何修改

打包之后能扒,生成的ipa包內(nèi)佣渴,是沒有這個.entitlements 文件的。經(jīng)過測試初斑,我發(fā)現(xiàn)是可以正常收到推送信息的辛润。測試的方法如下,大家也可以測試一下见秤。

測試方法:打包之后安裝ipa文件砂竖,然后利用極光推送,選擇生產(chǎn)環(huán)境鹃答,推送乎澄,即可。

經(jīng)過上面的操作测摔,你就會驚奇的發(fā)現(xiàn)置济,推送已經(jīng)適配完畢了,iOS10的系統(tǒng)锋八,已經(jīng)可以正常接收通知了浙于。

1.系統(tǒng)自帶方法

1.iOS10的系統(tǒng)導(dǎo)入

#import <UserNotifications/UserNotifications.h>

2.注冊通知

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //系統(tǒng)方法
    //注冊通知
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
        //iOS10版本
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        center.delegate = self;
        [center requestAuthorizationWithOptions:UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError * _Nullable error) {
            if (granted) {
                //點(diǎn)擊允許
                NSLog(@"注冊成功");
            } else {
                //點(diǎn)擊允許
                NSLog(@"注冊失敗");
            }
        }];
    } else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0){
        //iOS8和9的方法
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]];
    } else {
        //iOS8以下
        [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound];
    }
    //注冊
    [[UIApplication sharedApplication] registerForRemoteNotifications];
    //jpush
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
#ifdef NSFoundationVersionNumber_iOS_9_x_Max 
        JPUSHRegisterEntity *entity = [[JPUSHRegisterEntity alloc] init];
        entity.types = UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound;
        [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
#endif
    } else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        [JPUSHService registerForRemoteNotificationTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:nil];
    }
    [JPUSHService setupWithOption:launchOptions appKey:JPUSH_appkey channel:@"Publish channel" apsForProduction:YES];
    
    return YES;
}

3.獲取deviceToken系統(tǒng)和jpush方法一致

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    //獲取deviceToken
    NSLog(@"deviceToken是 -- %@",[NSString stringWithFormat:@"%@",deviceToken]);
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    //獲取deviceToken失敗
    NSLog(@"獲取deviceToken失敗 -- %@",error);
}

4.接受通知

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
    NSLog(@"前臺 -- willPresentNotification ");
    //iOS 10收到通知
    UNNotificationRequest *request = notification.request;//收到推送的請求
    NSDictionary *userInfo = request.content.userInfo;
    UNNotificationContent *content = request.content;//收到推送的消息內(nèi)容
    NSNumber *badge = content.badge;//角標(biāo)
    NSString *body = content.body;//
    UNNotificationSound *sound = content.sound;//
    NSString *subtitle = content.subtitle;//
    NSString *title = content.title;//
    
    if ([request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        NSLog(@"iOS 10 前臺收到遠(yuǎn)程通知 : %@",[self logDic:userInfo]);
    } else {
        NSLog(@"iOS10 前臺收到本地通知:{\\\\nbody:%@,\\\\ntitle:%@,\\\\nsubtitle:%@,\\\\nbadge:%@查库,\\\\nsound:%@路媚,\\\\nuserInfo:%@\\\\n}",body,title,subtitle,badge,sound,userInfo);
    }
  //系統(tǒng)方法
    completionHandler(UNNotificationPresentationOptionBadge | UNAuthorizationOptionSound | UNNotificationPresentationOptionAlert);//需要執(zhí)行這個方法,選擇是否提醒用戶樊销,有badge整慎、sound、alert三種類型可以設(shè)置
}

5.通知的點(diǎn)擊事件(系統(tǒng))

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
    NSLog(@" 后臺 -- didReceiveNotificationResponse ");
    //iOS 10收到通知
    UNNotificationRequest *request = response.notification.request;//收到推送的請求
    NSDictionary *userInfo = request.content.userInfo;
    UNNotificationContent *content = request.content;//收到推送的消息內(nèi)容
    NSNumber *badge = content.badge;//角標(biāo)
    NSString *body = content.body;//
    UNNotificationSound *sound = content.sound;//
    NSString *subtitle = content.subtitle;//
    NSString *title = content.title;//
    
    if ([request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        NSLog(@"iOS 10 前臺收到遠(yuǎn)程通知 : %@",[self logDic:userInfo]);
    } else {
        NSLog(@"iOS10 前臺收到本地通知:{\\\\nbody:%@围苫,\\\\ntitle:%@,\\\\nsubtitle:%@,\\\\nbadge:%@裤园,\\\\nsound:%@,\\\\nuserInfo:%@\\\\n}",body,title,subtitle,badge,sound,userInfo);
    }
  //系統(tǒng)方法
    completionHandler(UNNotificationPresentationOptionBadge | UNAuthorizationOptionSound | UNNotificationPresentationOptionAlert);//需要執(zhí)行這個方法剂府,選擇是否提醒用戶拧揽,有badge、sound、alert三種類型可以設(shè)置
}

舊版本的通知

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    NSLog(@"iOS7及以上系統(tǒng)淤袜,收到通知:%@",[self logDic:userInfo]);
    //jpush
    [JPUSHService handleRemoteNotification:userInfo];
    //系統(tǒng)要求
    completionHandler(UIBackgroundFetchResultNewData);
}

6.jpushDelegate

-(void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
    NSDictionary *userInfo = notification.request.content.userInfo;
    if ([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];
    }
    //系統(tǒng)要求
    completionHandler(UNNotificationPresentationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound);
}
-(void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
    NSDictionary *useInfo = response.notification.request.content.userInfo;
    if ([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:useInfo];
    }
    //系統(tǒng)要求
    completionHandler(UNNotificationPresentationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound);
}

7.logDic的方法

- (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;
}

如果用極光的方法痒谴,則寫方法1.2.3.6.7.

參考iOS開發(fā) iOS10推送必看(基礎(chǔ)篇)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市铡羡,隨后出現(xiàn)的幾起案子积蔚,更是在濱河造成了極大的恐慌,老刑警劉巖烦周,帶你破解...
    沈念sama閱讀 217,826評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件尽爆,死亡現(xiàn)場離奇詭異,居然都是意外死亡读慎,警方通過查閱死者的電腦和手機(jī)漱贱,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來夭委,“玉大人幅狮,你說我怎么就攤上這事≈昃模” “怎么了彪笼?”我有些...
    開封第一講書人閱讀 164,234評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長蚂且。 經(jīng)常有香客問我,道長幅恋,這世上最難降的妖魔是什么杏死? 我笑而不...
    開封第一講書人閱讀 58,562評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮捆交,結(jié)果婚禮上淑翼,老公的妹妹穿的比我還像新娘。我一直安慰自己品追,他們只是感情好玄括,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,611評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著肉瓦,像睡著了一般遭京。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上泞莉,一...
    開封第一講書人閱讀 51,482評論 1 302
  • 那天哪雕,我揣著相機(jī)與錄音,去河邊找鬼鲫趁。 笑死斯嚎,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播堡僻,決...
    沈念sama閱讀 40,271評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼糠惫,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了钉疫?” 一聲冷哼從身側(cè)響起硼讽,我...
    開封第一講書人閱讀 39,166評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎陌选,沒想到半個月后理郑,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,608評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡咨油,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,814評論 3 336
  • 正文 我和宋清朗相戀三年您炉,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片役电。...
    茶點(diǎn)故事閱讀 39,926評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡赚爵,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出法瑟,到底是詐尸還是另有隱情冀膝,我是刑警寧澤,帶...
    沈念sama閱讀 35,644評論 5 346
  • 正文 年R本政府宣布霎挟,位于F島的核電站窝剖,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏酥夭。R本人自食惡果不足惜赐纱,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,249評論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望熬北。 院中可真熱鬧疙描,春花似錦、人聲如沸讶隐。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽巫延。三九已至效五,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間炉峰,已是汗流浹背火俄。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留讲冠,地道東北人瓜客。 一個月前我還...
    沈念sama閱讀 48,063評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親谱仪。 傳聞我的和親對象是個殘疾皇子玻熙,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,871評論 2 354

推薦閱讀更多精彩內(nèi)容