我已经声明我的类也符合 UNUserNotificationCenterDelegate,我在 didFinishLaunchingWithOptions 中将所述类声明为当前的 UNNotificationCenter 委托。
但是,无论我做什么,我都无法在我的自定义操作中收到对方法 userNotificationCenter didReceiveNotificationResponse 的回调。代码附在下面。
有效的事情: - 能够很好地看到通知 - 能够点击按钮启动应用程序确实可以启动应用程序
没有的事情: - 能够单击按钮测试,它只是关闭通知,它不调用方法 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 秒后安排另一个通知。所有这些都不会发生,因为没有调用该方法。