User Notifications Framework in iOS 10

效果圖

                    

 






UserNotificationsUI,iOS 10增加新的framework可以讓我們定義新的通知樣式。

使用步驟:

1、創建一個Request(UNNotificationRequest),主要是爲了請求通知中心發送通知。

2、Request 包含一個觸發器和內容。

觸發器trigger(UNNotificationTrigger)需要一個時間(NSDateComponents)去定時發通知;

內容UNMutableNotificationContent包含通知的標題、文本內容、附件(附件是一個數組, 經測試只能顯示一張圖片,且無論設置幾張圖片,也只是一張圖片)  

3、最後通過通知中心去分發通知。

NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar componentsInTimeZone:[NSTimeZone localTimeZone] fromDate:date];
    
    NSDateComponents *newComponents = [[NSDateComponents alloc] init];
    newComponents.calendar = calendar;
    newComponents.timeZone = [NSTimeZone localTimeZone];
    newComponents.month = components.month;
    newComponents.day = components.day;
    newComponents.hour = components.hour;
    newComponents.minute = components.minute;
    
    UNNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:newComponents repeats:NO];
    
    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
    content.title = @"Where am I";
    content.body = @"Just a moment to miss you";
    content.sound = [UNNotificationSound defaultSound];
    content.categoryIdentifier = @"MyCategory";
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"settingImage4@2x" ofType:@"png"];
    NSString *path2 = [[NSBundle mainBundle] pathForResource:@"settingImage3@2x" ofType:@"png"];
    if (path) {
        UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"logo" URL:[NSURL fileURLWithPath:path] options:nil error:nil];
        
        UNNotificationAttachment *attachment2 = [UNNotificationAttachment attachmentWithIdentifier:@"logo2" URL:[NSURL fileURLWithPath:path2] options:nil error:nil];
        if (attachment) {
            content.attachments = @[attachment, attachment2];
        }
    }
    
    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"textNotification" content:content trigger:trigger];
    [[UNUserNotificationCenter currentNotificationCenter] removeAllPendingNotificationRequests];
    [[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];
    
    [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        if (error) {
            NSLog(@"Uh oh! We had an error: %@", error);
        }
    }];


如果需要代理,比如實現收到通知後的點擊等事件。

#pragma mark UNUserNotificationCenterDelegate
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
    if ([response.actionIdentifier isEqualToString:@"remindLater"]) {
        NSDate *date = [NSDate dateWithTimeInterval:800 sinceDate:[NSDate date]];
    }
}

其次,如果需要給通知增加一個按鈕,可以初始化如下代碼

[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionAlert | UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (!granted) {
            NSLog(@"Notification access denied.");
        }
    }];
    
    UNNotificationAction *action = [UNNotificationAction actionWithIdentifier:@"remindLater" title:@"remind me later" options:UNNotificationActionOptionDestructive];
    UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"MyCategory" actions:@[action] intentIdentifiers:@[] options:UNNotificationCategoryOptionAllowInCarPlay];
    
    [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObjects:category, nil]];














 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章