2016-10-26 3 views
1

У меня есть функция для расписания UNUserNotification, я помещаю ее в образец проекта xcode, тогда это работа - уведомление отображается. Но когда я привожу его в реальный проект (проект компании), то UNUserNotification никогда не показываются. вот мой кодПочему Запланированная 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"); 
}]; 

Пожалуйста, помогите мне! Спасибо большое!

ответ

1

Вы должны проверить свой trigger.nextDate. Я думаю, ваше - это время, когда вы устанавливаете триггер, но когда запрос добавляется в центр, теперь время больше, чем trigger.nextDate. Так UNUserNotification никогда не показывает. Попробуйте на

[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * theError){ 
    NSLog(@"Scheduled : %@", [dateComponents date]); 
    NSLog(@"Scheduled1 : %@", [trigger nextDate]); 
    NSLog(@"Scheduled2 : %@", [NSDate date]); 
    NSLog(@"WScheduled"); 
}]; 
+1

, пожалуйста, отформатируйте свой ответ –

Смежные вопросы