如何安排多个本地通知。在iOS 10中,UILocalNotification
更改为UNUserNotificationCenter
。
但我不知道如何根据日期/时间添加多个通知。如何在通知栏中单击特定通知时处理该通知。
以下是我的代码段:
// 这是我的App委托方法-------
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate ()<UNUserNotificationCenterDelegate>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
NSLog(@"request succeeded!");
}
}];
return YES;
}
// 这是我的View Controoller -------
// Local Notification -------
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = [NSString localizedUserNotificationStringForKey:@"Notification!" arguments:nil];
content.body = [NSString localizedUserNotificationStringForKey:@"This is local notification message!" arguments:nil];
content.userInfo = [content userInfo];
content.sound = [UNNotificationSound defaultSound];
/// 4. update application icon badge number
content.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1);
NSDateComponents* date = [[NSDateComponents alloc] init];
date.day = 26;
date.month = 10;
date.year = 2016;
date.hour = 14;
date.minute = time; // Here time is different different time I want to schedule notification.
NSLog(@"date : %@",date);
// Deliver the notification in five seconds.
UNCalendarNotificationTrigger* trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:date repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond" content:content trigger:trigger];
/// 3. schedule localNotification
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"add NotificationRequest succeeded!");
}
}];
我没有得到如何添加多个通知以及如何处理。我被困在最近2天。
请有人知道,帮帮我....