User Notifications Framework in iOS 10
来源:互联网 发布:apache ivy maven 编辑:程序博客网 时间:2024/06/05 19:12
效果图
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]];
0 0
- User Notifications Framework in iOS 10
- Dev Guide/Framework Topics/User Interface/Notifications
- Sending iOS Push Notifications via APNS in JavaScript
- Notifications in Android N
- 开源 toast-notifications-ios
- iOS Push Notifications | Parse
- User Interface Customization in iOS 5
- User Interface Customization in iOS 5
- Brief Intro to Notifications of Foundation Framework
- IOS接口编程之Notifications
- Notifications
- notifications
- Notifications
- Notifications
- Notifications
- Notifications
- notifications
- Notifications
- Android Studio报错 :在Android Studio中如何将依赖的jar包放在SDK的android.jar前?
- 27.1.5 示例--使用图表控件来两个年度的销售额
- 使用Android Studio2.2进行NDK编程创建示例
- 点击空白处隐藏软键盘(点击EditText以外的地方隐藏软键盘)
- openwrt交叉编译应用程序之helloworld
- User Notifications Framework in iOS 10
- 正则表达式-匹配密码验证(密码强弱等级,不能匹配中文,匹配数字 字母 字符)
- struct结构的内存对齐
- python小练习2:删除列表中重复的元素
- Android 反编译 -smali语法
- PHP模拟登陆
- ELK性能优化
- Codeforces 450B Jzzhu and Sequences(矩阵快速幂)
- Ubuntu下格式化U盘的方法(基于格式化命令)