ios 10 之后 更新变动最大的就是通知了,今天自己写了一个本地推送,做一下总结
快速添加一个通知,这里用 UNMutableNotification
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
ViewController *vc = [[ViewController alloc] init];
UINavigationController *navc = [[UINavigationController alloc] initWithRootViewController:vc];
self.window.rootViewController = navc;
[self.window makeKeyAndVisible];
//处理iOS8本地推送不能收到
//通知
UNUserNotificationCenter *notificenter = [UNUserNotificationCenter currentNotificationCenter];
//代理
notificenter.delegate = self;
//ios10 之后的注册 标题+声音
[notificenter requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) {
NSLog(@"注册");
}];
//获取当前的通知设置,UNNotificationSettings 是只读对象,不能直接修改,只能通过以下方法获取
[notificenter getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
NSLog(@"设置");
}];
// Override point for customization after application launch.
return YES;
}
#pragma mark -- 本地通知代理的方法
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
{
NSLog(@"%@",notification);
//前台调用通知
completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);
}
通知内容 UNMutableNotificationContent,此处你可以用到应该触发的位置
#pragma mark -- 本地推送
- (void)createNotification
{
// 使用 UNUserNotificationCenter 来管理通知
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
//需创建一个包含待通知内容的 UNMutableNotificationContent 对象,注意不是 UNNotificationContent ,此对象为不可变对象。
UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
content.title = [NSString localizedUserNotificationStringForKey:@"标题" arguments:nil];
content.body = [NSString localizedUserNotificationStringForKey:@"副标题(多用于内容)" arguments:nil];
//声音文件
content.sound = [UNNotificationSound soundNamed:@"sound.caf"];
// 在 alertTime 后推送本地推送
UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger
triggerWithTimeInterval:0.5 repeats:NO];
UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond"
content:content trigger:trigger];
//添加推送成功后的处理!
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
// UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"本地通知" message:@"成功添加推送" preferredStyle:UIAlertControllerStyleAlert];
// UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
// [alert addAction:cancelAction];
// [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
}];
}
形式如下:
声音的设置需要一个类 UNNotificationSound
需要注意的事,很多时候有些会无效,这种情况我们需要
1.检查你的注册
completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);
是否添加接收的声音方法
2.是否上传的声音文件正确,一般这里是 右键 -> add Files to XXX,添加你的声音文件到bundle里面,然后通过 target -> Build Phases -> Copy Bundle Resources 是否存在你添加的文件
3.添加通知的声音有时候会无效。这应该是iOS10存在的一个bug,删除掉程序,再安装运行就好了。
触发机制UNNotificationTrigger
Trigger是新加入的一个功能,通过此类可设置本地通知触发条件。它一共有一下几种类型:
1、UNPushNotificaitonTrigger
推送服务的Trigger,由系统创建
2、UNTimeIntervalNotificaitonTrigger
时间触发器,可以设置多长时间以后触发,是否重复。如果设置重复,重复时长要大于60s
3、UNCalendarNotificaitonTrigger
日期触发器,可以设置某一日期触发。例如,9:47触发:
content.sound = [UNNotificationSound soundNamed:@"sound.caf"];
NSDateComponents *components = [[NSDateComponents alloc] init];
components.hour = 9;
components.minute = 47; // components 日期
UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES];
UNNotificationationAttachment 可以在通知添加图片,音频,视频
代码如下:
NSString *imageFile = [[NSBundle mainBundle] pathForResource:@"新默认头像" ofType:@"png"];
UNNotificationAttachment *imageAttachment = [UNNotificationAttachment attachmentWithIdentifier:@"iamgeAttachment" URL:[NSURL fileURLWithPath:imageFile] options:nil error:nil];
content.attachments = @[imageAttachment];//虽然是数组,但是添加多个只能显示第一个
效果:
ios10 之前的通知
注册通知
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 注册通知,如果已经获得发送通知的授权则创建本地通知,否则请求授权(注意:如果不请求授权在设置中是没有对应的通知设置项的,也就是说如果从来没有发送过请求,即使通过设置也打不开消息允许设置)
if ([[UIApplication sharedApplication] currentUserNotificationSettings].types != UIUserNotificationTypeNone) {
[self addLocalNotificationForOldVersion];
} else {
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
return YES;
}
添加通知
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 注册通知,如果已经获得发送通知的授权则创建本地通知,否则请求授权(注意:如果不请求授权在设置中是没有对应的通知设置项的,也就是说如果从来没有发送过请求,即使通过设置也打不开消息允许设置)
if ([[UIApplication sharedApplication] currentUserNotificationSettings].types != UIUserNotificationTypeNone) {
[self addLocalNotificationForOldVersion];
} else {
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
return YES;
}
代理方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 注册通知,如果已经获得发送通知的授权则创建本地通知,否则请求授权(注意:如果不请求授权在设置中是没有对应的通知设置项的,也就是说如果从来没有发送过请求,即使通过设置也打不开消息允许设置)
if ([[UIApplication sharedApplication] currentUserNotificationSettings].types != UIUserNotificationTypeNone) {
[self addLocalNotificationForOldVersion];
} else {
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
return YES;
}
参考demo:https://github.com/jinshuaier/HggLocalNotification
欢迎留言提问,一起学习!