我有一个安排UNUserNotification的功能,我将它放入一个示例xcode项目然后它的工作 - 显示通知。 但是,当我把它带到真正的项目(公司的项目)时,UNUserNotification永远不会显示。 这是我的代码
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
// create a category
UNNotificationCategory *inviteCategory = [UNNotificationCategory categoryWithIdentifier:@"kNotificationCategoryToDoIdentifier"
actions:@[]
intentIdentifiers:@[]
options:UNNotificationCategoryOptionCustomDismissAction];
NSSet *categories = [NSSet setWithObject:inviteCategory];
// registration
[center setNotificationCategories:categories];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *fireDateomponents = [gregorian components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond)
fromDate:[NSDate date]];
NSDateComponents * dateComponents = [[NSDateComponents alloc] init];
dateComponents.calendar = gregorian;
dateComponents.timeZone = [NSTimeZone localTimeZone];
dateComponents.year = fireDateomponents.year;
dateComponents.month = fireDateomponents.month;
dateComponents.day = fireDateomponents.day;
dateComponents.hour = fireDateomponents.hour;
dateComponents.minute = fireDateomponents.minute;
dateComponents.second = fireDateomponents.second + 10;
//Setup notification Triger
UNCalendarNotificationTrigger * trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:dateComponents
repeats:NO];
UNMutableNotificationContent * content = [UNMutableNotificationContent new];
content.title = @"params.title";
content.body = @"params.body";
content.sound = [UNNotificationSound defaultSound];
content.badge = @(10);
content.categoryIdentifier = @"kNotificationCategoryToDoIdentifier";
//Crreate Notification Request
UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:@"requestId"
content:content trigger:trigger];
//Schedule Notification
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * theError){
NSLog(@"Scheduled : %@", [dateComponents date]);
NSLog(@"WScheduled");
}];
请帮帮我! 非常感谢!
答案 0 :(得分:1)
您应该检查trigger.nextDate
。我猜你的trigger.nextDate
是你设置触发器的时间,但是当请求被添加到中心时,nowtime晚于trigger.nextDate
。所以UNUserNotification
从未表现出来。
试试
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * theError){
NSLog(@"Scheduled : %@", [dateComponents date]);
NSLog(@"Scheduled1 : %@", [trigger nextDate]);
NSLog(@"Scheduled2 : %@", [NSDate date]);
NSLog(@"WScheduled");
}];