首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么Scheduled UNUserNotification从不触发?

为什么Scheduled UNUserNotification从不触发?
EN

Stack Overflow用户
提问于 2016-10-26 12:53:53
回答 1查看 427关注 0票数 1

我有一个调度UNUserNotification的函数,我把它放到一个示例xcode项目中,然后它就可以工作了--通知显示出来了。但是,当我把它带到真正的项目(公司的项目)中时,UNUserNotification就再也不会出现了。以下是我的代码

代码语言:javascript
复制
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");
}];

请帮帮我!非常感谢!

EN

回答 1

Stack Overflow用户

发布于 2016-12-15 11:05:03

你应该检查一下你的trigger.nextDate。我猜您的trigger.nextDate是您设置触发器的时间,但是当请求被添加到中心时,you时间晚于trigger.nextDate。所以UNUserNotification从来没出现过。试一试

代码语言:javascript
复制
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * theError){
    NSLog(@"Scheduled : %@", [dateComponents date]);
    NSLog(@"Scheduled1 : %@", [trigger nextDate]);
    NSLog(@"Scheduled2 : %@", [NSDate date]);
    NSLog(@"WScheduled");
}];
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40253997

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档