iOS APNS
一、开启APNS
1.创建证书
见证书创建
2.Xcode 设置
开启后台状态模式
二、代码实现
1.注册APNS
在需要实现的类中注册apns,非必需AppDelegate。
-(void)requestAPNS:(void(^)(BOOL grant))finish{
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
//请求
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (granted) {
// 点击允许
//注册通知
[[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
// 点击不允许
NSLog(@"APNSPush注册失败,用户不允许");
}
finish(granted);
});
}];
// 写代理,可监听通知的接收与点击,代理实现下边接收会讲到
center.delegate = self<