iOS 消息推送?
之前沒怎么關注 這塊 今天要做消息推送 ?就做個記錄 也算是知識的總結啦?
理一理頭緒 ?我們要做這件事 要哪些東西?
1.請求證書?
2.Xcode 打開 接受通知機制 (下面會配圖的)
3.NWPusher?(測試用的)
其他的興趣 你們自己了解 我只介紹如何實現(xiàn)消息推送
一 開始下載配置證書
https://developer.apple.com/account/ios/certificate/? (直接進入 證書界面)
上面寫的明白 檢查一下自己要開發(fā)的App ID 在不在里面?
創(chuàng)建完成之后 ?回到剛才的頁面 點擊剛才創(chuàng)建的 ID?
我們開始請求證書 ?首先要打開鑰匙串??
鑰匙串訪問 -> 證書助理 -> 從證書頒發(fā)機構請求證書..
好了 我們繼續(xù)生成開發(fā)環(huán)境下的證書
相同的方法 請求發(fā)布版的證書 這里我就不多重復了
在Keychain Access.app里選定這個新證書(Apple Development Push Services*),導出到桌面,保存為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ù)據(jù)
NSString *apsContent = [notification.request.content.userInfo objectForKey:@"aps"];
NSLog(@"%@",apsContent);
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
//在沒有啟動本App時,收到服務器推送消息瘦穆,下拉消息會有快捷回復的按鈕钮呀,點擊按鈕后調用的方法,根據(jù)identifier來判斷點擊的哪個按鈕
NSString *apsContent = [response.notification.request.content.userInfo objectForKey:@"aps"];
NSLog(@"didReceiveNotificationResponse:%@",response.notification.request.content.title);
NSLog(@"%@",apsContent);
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
//遠程推送APP在前臺
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
NSLog(@"didReceiveRemoteNotification:%@",userInfo);
}
- (void)setUpCategory
{
UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"enterApp" title:@"進入應用" 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(@"從消息啟動:%@",userInfo);
//? ? ? ? [BPush handleNotification:userInfo];
}
//角標清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 //后臺切換到前臺的 或者應用激活的時候調用
{
// 清除圖標數(shù)字
application.applicationIconBadgeNumber = 0;
}
下面開始 Xcode 里面的設置