国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

IOS10 Notification推送通知(二)

2019-11-09 18:41:15
字體:
來源:轉載
供稿:網友

多媒體的推送,對一些媒體大小有些限制,看下蘋果官方的截圖

這里寫圖片描述

本地附件推送通知,只需給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遠程推送。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 富平县| 南城县| 贞丰县| 赤壁市| 金湖县| 永仁县| 青岛市| 潍坊市| 芒康县| 朔州市| 安徽省| 涿州市| 城步| 四平市| 民勤县| 陈巴尔虎旗| 岢岚县| 上蔡县| 金门县| 洪江市| 安庆市| 封丘县| 织金县| 九寨沟县| 祁门县| 德清县| 罗定市| 余江县| 南漳县| 海城市| 博白县| 万荣县| 通化县| 运城市| 大关县| 隆德县| 嘉黎县| 红桥区| 姜堰市| 阜新市| 白城市|