IOS10 Notification推送通知(二)

来源:互联网 发布:剑灵猫猫捏脸数据 编辑:程序博客网 时间:2024/06/04 18:17

多媒体的推送,对一些媒体大小有些限制,看下苹果官方的截图

这里写图片描述

本地附件推送通知,只需给content.attachments设置UNNotificationAttachment附件对象
1、实现本地音乐推送的效果先看下效果图
这里写图片描述

//创建音乐本地推送- (void)createAudioNotification {    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];    //创建通知内容    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];    content.body     = [NSString localizedUserNotificationStringForKey:@"之前超火的韩剧《太阳的后裔》,里面有首口哨歌曲,一起来听听吧~~" arguments:nil];    content.title    = [NSString localizedUserNotificationStringForKey:@"来听听这首动听的歌曲吧~~" arguments:nil];    content.sound = [UNNotificationSound soundNamed:@"test.caf"];    content.userInfo = @{@"locatin":@"地址:上海浦东",@"user":@"yuna"};    //附件    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"太阳的后裔口哨OST" ofType:@"mp3"]];    UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"audioAttachment"                                                                                          URL:url                                                                                      options:nil                                                                                        error:nil];    content.attachments = @[attachment];     content.categoryIdentifier = @"audioNotification";    //创建延迟触发器    UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:3 repeats:NO];    //创建请求,并投递    UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:@"audioNotifacation"                                                                           content:content                                                                           trigger:trigger];    [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {        NSLog(@"添加本地音乐推送 : %@", error ? [NSString stringWithFormat:@"error : %@", error] : @"success");    }];}

在- (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions 中调用,跟上篇博客的调用一样

2、创建周期性的本地推送,看下效果
这里写图片描述

//2、创建周期性的本地推送- (void)createCalenderNotification{    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];    content.title = @"周期性本地定时推送";//    content.subtitle = @"subtitle";    content.body = @"这是本地周期推送~~~";    content.sound = [UNNotificationSound defaultSound];    //UNCalendarNotificationTrigger:创建一个在具体日起事件触发的通知    NSDateComponents* date = [[NSDateComponents alloc] init];    date.hour = 14;    date.minute = 23;    //每天的15:20 触发通知    UNCalendarNotificationTrigger* trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:date repeats:YES];    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"Calendar" content:content trigger:trigger];    [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {        NSLog(@"添加周期性定时推送 :%@", error ? [NSString stringWithFormat:@"error : %@", error] : @"success");    }];}

这是设置14:23的推送,时间一到就看到了推送。

3、还有个在设备进入或者离开某个地理位置时触发推送

//3、创建指定位置通知- (void)createRegionNotification {    //3、UNLocationNotificationTrigger:当设备进入或者离开某个地理位置,触发一个通知,可以在进入或者离开,或者都有    CLLocationCoordinate2D center = CLLocationCoordinate2DMake(31.2213933994,121.5299423947);    CLCircularRegion* region = [[CLCircularRegion alloc] initWithCenter:center radius:2000.0 identifier:@"MyAddress"];    region.notifyOnEntry = YES;    region.notifyOnExit = YES;    UNLocationNotificationTrigger* trigger = [UNLocationNotificationTrigger triggerWithRegion:region repeats:NO];    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];    content.title = @"指定位置通知";    //    content.subtitle = @"subtitle";    content.body = @"当你进入或者离开指定的地理位置,就会触发该通知";    content.sound = [UNNotificationSound defaultSound];    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"Calendar" content:content trigger:trigger];    [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {        NSLog(@"添加指定位置推送 :%@", error ? [NSString stringWithFormat:@"error : %@", error] : @"success");    }];}

4、创建带有movie的推送
这里写图片描述

//创建带有movie的推送- (void)createMovieNotification {    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];    //创建通知内容    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];    content.body     = [NSString localizedUserNotificationStringForKey:@"大家一起来学习吧~~~" arguments:nil];    content.title    = [NSString localizedUserNotificationStringForKey:@"学无止境啦~~" arguments:nil];    content.sound = [UNNotificationSound soundNamed:@"test.caf"];    content.userInfo = @{@"locatin":@"地址:上海浦东",@"user":@"yuna"};    //附件    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"10_循环语句" ofType:@"mp4"]];    UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"movieAttachment"                                                                                          URL:url                                                                                      options:nil                                                                                        error:nil];    content.attachments = @[attachment];    content.categoryIdentifier = @"movieNotification";    //创建延迟触发器    UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:3 repeats:NO];    //创建请求,并投递    UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:@"movieAttachmentNotifacation"                                                                           content:content                                                                           trigger:trigger];    [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {        NSLog(@"添加movie推送 : %@", error ? [NSString stringWithFormat:@"error : %@", error] : @"success");    }];}

至此关于本地通知的demo,已经差不多了,下篇将用Knuff工具来模拟APNs远程推送。

0 0