效果图
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]];