一、生成对应的生产通知证书和发布通知证书
证书生成部分就不做介绍了
二、开启应用的推送能力
这是会生成一个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进行测试