1. 程式人生 > >IOS10 Notification推送通知(二)

IOS10 Notification推送通知(二)

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

這裡寫圖片描述

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