我有一个调度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");
}];请帮帮我!非常感谢!
发布于 2016-12-15 11:05:03
你应该检查一下你的trigger.nextDate。我猜您的trigger.nextDate是您设置触发器的时间,但是当请求被添加到中心时,you时间晚于trigger.nextDate。所以UNUserNotification从来没出现过。试一试
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * theError){
NSLog(@"Scheduled : %@", [dateComponents date]);
NSLog(@"Scheduled1 : %@", [trigger nextDate]);
NSLog(@"Scheduled2 : %@", [NSDate date]);
NSLog(@"WScheduled");
}];https://stackoverflow.com/questions/40253997
复制相似问题