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 在不在里面?



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


我們開始請(qǐng)求證書 ?首先要打開鑰匙串??
鑰匙串訪問 -> 證書助理 -> 從證書頒發(fā)機(jī)構(gòu)請(qǐng)求證書..


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




相同的方法 請(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?

看代碼
- (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è)置


