一、生成對應的生產通知證書和發佈通知證書
證書生成部分就不做介紹了
二、開啓應用的推送能力
這是會生成一個TestPush.entitlements文件,裏面APS Environment屬性值爲development,直接運行一直是測試環境,打包爲ipa後則爲正式環境。
三、註冊遠程通知
1.導入頭文件
在Appdelegate.m文件裏引入UserNotifications/UserNotifications.h,同時要遵循UNUserNotificationCenterDelegate協議
#import <UserNotifications/UserNotifications.h>
2.註冊通知
// 在該函數裏進行註冊
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self registPushNotificationAuthorization:application];
return YES;
}
- (void)registPushNotificationAuthorization:(UIApplication *)application{
if (@available(iOS 10.0, *)) {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
//必須寫代理,不然無法監聽通知的接收與點擊事件
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error && granted) {
NSLog(@"註冊成功");
}else{
NSLog(@"註冊失敗");
}
}];
} else {
//iOS 8 - iOS 10系統
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[application registerUserNotificationSettings:settings];
}
//註冊遠端消息通知獲取device token
[application registerForRemoteNotifications];
}
3.獲取Device Token
//獲取DeviceToken成功
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
//錯誤寫法,string = nil
//NSString *string = [[NSString alloc] initWithData:deviceToken encoding:NSUTF8StringEncoding];
//正確寫法
NSString *deviceString = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
deviceString = [deviceString stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"deviceToken = %@",deviceString);
}
//獲取DeviceToken失敗
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
NSLog(@"get DeviceToken fail:%@\n",error.description);
}
4.接收通知
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
//7.0以後
// 處理通知
completionHandler(UIBackgroundFetchResultNewData);
}
//===================== UNUserNotificationCenterDelegate,10以後 ==========================
// 前臺收到通知調用
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
// 需要執行這個方法,選擇是否提醒用戶,有Badge、Sound、Alert三種類型可以設置
completionHandler(UNNotificationPresentationOptionBadge|
UNNotificationPresentationOptionSound|
UNNotificationPresentationOptionAlert);
}
//點擊通知調用
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler {
// 處理通知
completionHandler();
}
四、測試
可以使用第三方NWPusher進行測試