我已经宣布我的班级也符合UNUserNotificationCenterDelegate,我宣称该班级是didFinishLaunchingWithOptions中当前的UNNotificationCenter代表。
但是,无论我做什么,我都无法在自定义操作上收到userNotificationCenter didReceiveNotificationResponse方法的回调。代码如下。
有效的方法: - 能够看到通知正常 - 能够点击启动应用程序确实启动应用程序
不要做的事情: - 能够单击按钮Test,它只是解除通知,并且它不会调用方法userNotificationCenter didReceiveNotificationResponse
UNUserNotificationCenter *notificationCenter = [UNUserNotificationCenter currentNotificationCenter];
NSDate *now = [[NSDate alloc]init];
NSDate *inABit = [now dateByAddingTimeInterval:10];
NSDateComponents *components = [[NSCalendar currentCalendar] components: NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour | NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:inABit];
UNCalendarNotificationTrigger *calendarTrigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:NO];
//register category
UNNotificationAction *test = [UNNotificationAction actionWithIdentifier:@"test" title:@"Test" options:@[]];
UNNotificationAction *launchApp = [UNNotificationAction actionWithIdentifier:@"launchApp" title:@"Launch App" options:UNNotificationActionOptionForeground];
UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"notification" actions:@[test, launchApp] intentIdentifiers:@[] options:UNNotificationCategoryOptionNone];
[notificationCenter setNotificationCategories:[NSSet setWithObject:category]];
//schedule notification
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc]init];
[content setTitle:@"Hello"];
[content setBody:@"Hello notification!"];
[content setCategoryIdentifier:@"notification"];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"request" content:content trigger:calendarTrigger];
[notificationCenter addNotificationRequest:request withCompletionHandler:
^(NSError *error){
if(error!=nil)
{
NSLog(@"There was an error adding a notification request.");
}
}];
当然,我已经实现了方法(userNotificationCenter didReceiveNotificationResponse)我只是不想在这里显示它占用空间。该方法执行NSLog并在10秒后安排另一个通知。所有这些都不会因为没有调用方法而发生。