多UNUserNotifications我如下設置多個UNUsernotifications,未擊發
- (void)viewDidLoad {
[super viewDidLoad];
notifCount = 0;
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
NSLog(@"request succeeded!");
[self set10Notif];
}
}];
}
在set10Notif
方法,我設置(用於測試8)的多個隨時間通知從當前時間起10秒。
-(void) set10Notif
{
notifCount = notifCount+1;
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0") && notifCount < 10)
{
// create actions
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
[calendar setTimeZone:[NSTimeZone localTimeZone]];
NSDate *fireD = [[NSDate date] dateByAddingTimeInterval:notifCount*10];
NSString *fireStr = [self returnStringFromDate:fireD withFormat:@"hh/mm/ss dd/MM/yyyy"];
NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitTimeZone fromDate:fireD];
UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init];
objNotificationContent.title = [NSString localizedUserNotificationStringForKey:@"Notif!" arguments:nil];
objNotificationContent.body = [NSString localizedUserNotificationStringForKey:fireStr
arguments:nil];
objNotificationContent.sound = [UNNotificationSound defaultSound];
UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"Prayer"
content:objNotificationContent trigger:trigger];
UNUserNotificationCenter *userCenter = [UNUserNotificationCenter currentNotificationCenter];
[userCenter addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"Local Notification succeeded");
}
else {
NSLog(@"Local Notification failed");
}
[self set10Notif];
}];
#endif
}
}
設置了所有本地通知。但是隻有一個通知在設備中被觸發,這是最後一個。
爲什麼多個通知不能觸發? 我做錯了什麼?
你是怎麼做的工作是爲多個通知?我被困在同一個問題上。請看看:https://stackoverflow.com/questions/44132879/ios-local-notification-not-firing-second-time-but-shows-in-getpendingnotificatio –
@JamshedAlam,在UNNotificationRequest中給出不同的requestWithIdentifier值 *請求= [UNNotificationRequest requestWithIdentifier:@「禱告」 內容:objNotificationContent觸發器:觸發器];'爲每個用戶通知... – NAZIK
請看我的問題。我已經在那裏設置了不同的標識符。但是我只有第一個鬧鐘,對於另一個鬧鐘,當我改變我的MAC時間時,它不會觸發,重新運行第二個鬧鐘的項目。 https://stackoverflow.com/questions/44132879/ios-local-notification-not-firing-second-time-but-shows-in-getpendingnotificatio –