iOS10通知(一)--申请授权、注册和获取授权信息
来源:互联网 发布:zencart 建站 编辑:程序博客网 时间:2024/05/21 09:54
此系列工程开发环境为xcode 8.2
1、创建工程,开启通知权限。开启后如果确认证书和自己的Boundle ID设置正确的情况下,第一个出现红色的叉叉,可以尝试clear之后退出xcode
2、创建通知的代理管理类(NotificationHandle),这样可以将代码分类管理
#import <Foundation/Foundation.h>#import <UIKit/UIKit.h>#import <UserNotifications/UserNotifications.h>@interface NotificationHandle : NSObject<UNUserNotificationCenterDelegate>+(NotificationHandle *) shareInstance;-(void)authorizationPushNotificaton:(UIApplication *)application;@end
@implementation NotificationHandle+(NotificationHandle *) shareInstance{ static NotificationHandle *instance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ instance = [[[self class] alloc] init]; }); return instance;}-(void)authorizationPushNotificaton:(UIApplication *)application{ UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; center.delegate = self; //必须写代理 [center requestAuthorizationWithOptions:UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionCarPlay completionHandler:^(BOOL granted, NSError * _Nullable error) { //注册之后的回调 if (!error && granted) { NSLog(@"注册成功..."); } else{ NSLog(@"注册失败..."); } }]; //获取注册之后的权限设置 //注意UNNotificationSettings是只读对象哦,不能直接修改! [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { NSLog(@"通知配置信息:\n%@",settings); }]; //注册通知获取token [application registerForRemoteNotifications];}#pragma mark UNUserNotificationCenterDelegate//收到通知-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{ //收到推送的请求 UNNotificationRequest *request = notification.request; //收到的内容 UNNotificationContent *content = request.content; //收到用户的基本信息 NSDictionary *userInfo = content.userInfo; //收到消息的角标 NSNumber *badge = content.badge; //收到消息的body NSString *body = content.body; //收到消息的声音 UNNotificationSound *sound = content.sound; //推送消息的副标题 NSString *subtitle = content.subtitle; //推送消息的标题 NSString *title = content.title; if ([notification.request.trigger isKindOfClass:[UNNotificationTrigger class]]) { NSLog(@"前台收到通知:%@\n",userInfo); } else{ NSLog(@"前台收到通知:{\nbody:%@,\ntitle:%@,\nsubtitle:%@,\nbadge:%@,\nsound:%@,\nuserInfo:%@}",body,title,subtitle,badge,sound,userInfo); } //不管前台后台状态下。推送消息的横幅都可以展示出来!有Badge、Sound、Alert三种类型可以设置 completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);}//app通知的点击事件-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{ //收到推送的请求 UNNotificationRequest *request = response.notification.request; //收到的内容 UNNotificationContent *content = request.content; //收到用户的基本信息 NSDictionary *userInfo = content.userInfo; //收到消息的角标 NSNumber *badge = content.badge; //收到消息的body NSString *body = content.body; //收到消息的声音 UNNotificationSound *sound = content.sound; //推送消息的副标题 NSString *subtitle = content.subtitle; //推送消息的标题 NSString *title = content.title; if ([response.notification.request.trigger isKindOfClass:[UNNotificationTrigger class]]) { NSLog(@"点击了通知:%@\n",userInfo); } else{ NSLog(@"通知:{\nbody:%@,\ntitle:%@,\nsubtitle:%@,\nbadge:%@,\nsound:%@,\nuserInfo:%@}",body,title,subtitle,badge,sound,userInfo); } completionHandler();} @end3、在AppDelegate中申请授权
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. _window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; RootViewController *rootVC = [[RootViewController alloc]init]; UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:rootVC]; _window.rootViewController = nav; [_window makeKeyWindow]; [UNUserNotificationCenter currentNotificationCenter].delegate = [NotificationHandle shareInstance]; [[NotificationHandle shareInstance] authorizationPushNotificaton:application]; return YES;}
4、实现注册token成功和失败的处理
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{ NSString *deviceString = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]]; deviceString = [deviceString stringByReplacingOccurrencesOfString:@" " withString:@""]; NSLog(@"远端获取的deviceToken\n%@",deviceString); //存储得到的token,后面备用 [[NSUserDefaults standardUserDefaults] setValue:deviceString forKey:@"deviceToken"]; [[NSUserDefaults standardUserDefaults] synchronize];}-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{ NSLog(@"获取token失败:%@\n",error.localizedDescription);}
5、启动app,弹出通知授权,允许之后就可以进到授权信息界面实现获取授权信息了,界面UI自行布局,下面给出获取授权信息的关键代码
-(void)setData{ self.deviceTokenLabel.text = [[NSUserDefaults standardUserDefaults] valueForKey:@"deviceToken"]; UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; __weak typeof(&*self) weakSelf = self; [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { weakSelf.centerLabel.text = [weakSelf getSettingDescribe:settings.notificationCenterSetting]; weakSelf.soundLabel.text = [weakSelf getSettingDescribe:settings.soundSetting]; weakSelf.badgeLabel.text = [weakSelf getSettingDescribe:settings.badgeSetting]; weakSelf.lockScreenLabel.text = [weakSelf getSettingDescribe:settings.lockScreenSetting]; weakSelf.alertLabel.text = [weakSelf getSettingDescribe:settings.alertSetting]; weakSelf.carPlayLabel.text = [weakSelf getSettingDescribe:settings.carPlaySetting]; weakSelf.alertStyleLabel.text = [weakSelf getAlertStyleDescribe:settings.alertStyle]; }];}-(NSString *)getAlertStyleDescribe:(NSInteger)alertStyle{ NSString *backStr = @""; switch (alertStyle) { case 0: backStr = @"UNAlertStyleNone"; break; case 1: backStr = @"UNAlertStyleBanner"; break; case 2: backStr = @"UNAlertStyleAlert"; break; default: backStr = @"UNKNOW"; break; } return backStr;}-(NSString *)getSettingDescribe:(NSInteger)setting{ NSString *backStr = @""; switch (setting) { case 0: backStr = @"NotSupported"; break; case 1: backStr = @"Disabled"; break; case 2: backStr = @"Enabled"; break; default: backStr = @"UNKNOW"; break; } return backStr;}6、最后运行的效果图如下
0 0
- iOS10通知(一)--申请授权、注册和获取授权信息
- iOS10及以上版本的通知授权方式
- 获取用户创建和授权
- 获取定位授权和注意事项
- swift3.1:获取用户授权显示通知(iOS 10)
- 微信公众号开发二:关于授权网页获取用户微信信息注册用户的开发
- 授权
- 授权
- 授权
- 授权
- 授权
- 苹果开发者授权账号申请流程和网上教程不同
- IOS开发证书申请流程和给他人电脑授权
- IOS开发证书申请流程和给他人电脑授权
- IOS开发证书申请流程和给他人电脑授权
- PC网站扫一扫获取微信授权用户注册
- Android permission 动态申请、授权
- 用PBNI实现软件注册授权控制(一)
- JAVA 的两种流 字节流 InputStream OutputStream 字符流 Reader Writer
- 【bzoj2327】[HNOI2011]勾股定理
- 在Eclipse中使用JUnit4进行单元测试(初级篇)
- Notification回顾
- 1016. 部分A+B (15)
- iOS10通知(一)--申请授权、注册和获取授权信息
- hdu 2669 Romantic (扩展欧几里得)
- mybatis # $区别
- 嵌入式和单片机的关系
- material design(一)CardView的使用详解
- 程序员如何更好的活着
- UIScrollView 非常规的page 实现
- OSG学习笔记24——根据鼠标平移的位置正确设置物体平移量
- MySQL数据库消耗CPU过高