iOS 消息推送(UserNotifications)

iOS 消息推送?

之前沒怎么關(guān)注 這塊 今天要做消息推送 ?就做個(gè)記錄 也算是知識(shí)的總結(jié)啦?

理一理頭緒 ?我們要做這件事 要哪些東西?

1.請(qǐng)求證書?

2.Xcode 打開 接受通知機(jī)制 (下面會(huì)配圖的)

3.NWPusher?(測(cè)試用的)

其他的興趣 你們自己了解 我只介紹如何實(shí)現(xiàn)消息推送

一 開始下載配置證書

https://developer.apple.com/account/ios/certificate/? (直接進(jìn)入 證書界面)

上面寫的明白 檢查一下自己要開發(fā)的App ID 在不在里面?

如果沒有 自己添加一個(gè)



這里也是填寫你要用到的id
必須勾選上

創(chuàng)建完成之后 ?回到剛才的頁(yè)面 點(diǎn)擊剛才創(chuàng)建的 ID?

沒有配置證書都是 黃色的 ?點(diǎn)擊edit 進(jìn)入?
勾選上

我們開始請(qǐng)求證書 ?首先要打開鑰匙串??

鑰匙串訪問 -> 證書助理 -> 從證書頒發(fā)機(jī)構(gòu)請(qǐng)求證書..


郵箱我瞎寫的 忽略
知道放到哪里去了 直接保存就好了

好了 我們繼續(xù)生成開發(fā)環(huán)境下的證書

一直點(diǎn)擊下一步 你會(huì)看到
選擇你剛才創(chuàng)建的文件?
生成后 直接下載 雙擊安裝就好了 可以在鑰匙串里面找到?

相同的方法 請(qǐng)求發(fā)布版的證書 這里我就不多重復(fù)了

在Keychain Access.app里選定這個(gè)新證書(Apple Development Push Services*),導(dǎo)出到桌面,保存為Certificates.p12.

終端上執(zhí)行 打包證書

openssl pkcs12 -clcerts -nokeys -out cert.pem -in Certificates.p12

openssl pkcs12 -nocerts -out key.pem -in Certificates.p12

openssl rsa -in key.pem -out key.unencrypted.pem

cat cert.pem key.unencrypted.pem > ck.pem


下載 NWPusher?


使用界面就是這樣的 很簡(jiǎn)單,但是對(duì)應(yīng)的正式不要搞錯(cuò)了 下面的token 是蘋果返回的?

看代碼

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

{

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

NSString *deviceTokenStr = [[[[deviceToken description]

stringByReplacingOccurrencesOfString:@"<" withString:@""]

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

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

NSLog(@"deviceTokenStr:%@",deviceTokenStr);

}

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

NSLog(@"didFailToRegisterForRemoteNotificationsWithError:%@",error);

}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{

NSLog(@"willPresentNotification:%@",notification.request.content.title);

// 這里真實(shí)需要處理交互的地方

// 獲取通知所帶的數(shù)據(jù)

NSString *apsContent = [notification.request.content.userInfo objectForKey:@"aps"];

NSLog(@"%@",apsContent);

}

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

//在沒有啟動(dòng)本App時(shí),收到服務(wù)器推送消息,下拉消息會(huì)有快捷回復(fù)的按鈕,點(diǎn)擊按鈕后調(diào)用的方法,根據(jù)identifier來判斷點(diǎn)擊的哪個(gè)按鈕

NSString *apsContent = [response.notification.request.content.userInfo objectForKey:@"aps"];

NSLog(@"didReceiveNotificationResponse:%@",response.notification.request.content.title);

NSLog(@"%@",apsContent);

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

}

//遠(yuǎn)程推送APP在前臺(tái)

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{

NSLog(@"didReceiveRemoteNotification:%@",userInfo);

}

- (void)setUpCategory

{

UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"enterApp" title:@"進(jìn)入應(yīng)用" options:UNNotificationActionOptionForeground];

UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"destructive" title:@"忽略" options:UNNotificationActionOptionDestructive];

//UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"helloIdentifier" actions:@[action1,action2] minimalActions:@[action1,action2] intentIdentifiers:@[] options:UNNotificationCategoryOptionNone];

UNNotificationCategory *caregory = [UNNotificationCategory categoryWithIdentifier:@"helloIdentifier" actions:@[action1,action2] intentIdentifiers:@[action1,action2] options:UNNotificationCategoryOptionNone];

[[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObjects:caregory, nil]];

}

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

// Override point for customization after application launch.

// iOS10 下需要使用新的 API

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

#ifdef NSFoundationVersionNumber_iOS_9_x_Max

UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];

[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionBadge)

completionHandler:^(BOOL granted, NSError * _Nullable error) {

// Enable or disable features based on authorization.

if (granted) {

[application registerForRemoteNotifications];

}

}];

center.delegate = self;

#endif

}

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

UIUserNotificationType myTypes = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;

UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:myTypes categories:nil];

[[UIApplication sharedApplication] registerUserNotificationSettings:settings];

}

NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

if (userInfo) {

NSLog(@"從消息啟動(dòng):%@",userInfo);

//? ? ? ? [BPush handleNotification:userInfo];

}

//角標(biāo)清0

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

return YES;

}

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

{

NSLog(@"didReceiveRemoteNotification : %@",userInfo);

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{

NSLog(@"notification : %@",notification);

}


- (void)applicationDidBecomeActive:(UIApplication *)application //后臺(tái)切換到前臺(tái)的 或者應(yīng)用激活的時(shí)候調(diào)用

{

// 清除圖標(biāo)數(shù)字

application.applicationIconBadgeNumber = 0;

}


下面開始 Xcode 里面的設(shè)置


后臺(tái)設(shè)置?


關(guān)于消息推送和服務(wù)器的交互 還有角標(biāo)的清除 我會(huì)在下篇文章里面介紹 謝謝
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容