我已經實現了iOS 10本地通知,它適用於第一次鬧鐘,但不適用於其他鬧鐘。我已經導入了iOS 10庫,實現了委託,在委託中第一次接收它,但它不會稍後觸發。iOS本地通知不是第二次觸發,而是在取消通知請求中顯示
我的日期,甲酸是這樣的:2017年5月28日14時04分07秒+0000
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setTimeZone:[NSTimeZone localTimeZone]];
//(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute)
NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:now];
NSDate *todaySehri = [calendar dateFromComponents:components];
UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init];
objNotificationContent.title = [NSString localizedUserNotificationStringForKey:[NSString stringWithFormat:@"Local.."]] arguments:nil];
objNotificationContent.body = [NSString localizedUserNotificationStringForKey:@"Incoming Local Notification" arguments:nil];
objNotificationContent.sound = [UNNotificationSound soundNamed:[NSString stringWithFormat:@"%@",soundFileBtn.titleLabel.text]]; // [UNNotificationSound defaultSound];//soundFileBtn.titleLabel.text;// [UNNotificationSound defaultSound];
NSDictionary *dict = @{@"alarmID":self.myKey,
@"SOUND_TYPE":[NSString stringWithFormat:@"%li",(long)self.audio_segment.selectedSegmentIndex]
};
NSArray *array = [NSArray arrayWithObjects:dict, nil];
NSDictionary *data = [NSDictionary dictionaryWithObject:array forKey:@"payload"];
[objNotificationContent setUserInfo:data];
//update application icon badge number
objNotificationContent.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 0);
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *compss= [cal components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute)
fromDate:todaySehri];
UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:compss repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:[todaySehri description]
content:objNotificationContent trigger:trigger];
//schedule localNotification
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"Local Notification succeeded");
}
else {
NSLog(@"Local Notification failed");
}
}];
嘗試打印您爲每個觸發器設置的'identifier',以檢查它們是否不同。另外你如何測試通知?通過手動更改設備上的時間或等待時間? – bhakti123
標識符在我的NSLog中不同。即使我附加一個額外的隨機字符串到我的標識符。我正在模擬器上檢查報警。我設置了3個獨立的警報。我設置我的MacBook時間接近第一次報警。當它工作,然後我再次改變我的MacBook時間接近第二次報警並運行我的項目。但它不適用於第二次或其他警報。 @ bhakti123 –
檢查是否所有的警報都是預定的。他們的下一個觸發日期是什麼。然後嘗試根據該觸發日期更改時間並查看它是否有效。 – bhakti123