I am attempting to use the UserNotifications Frameworks for iOS 10.
Notifications seem to work great if I do not pass in a trigger. They get presented right away in app. But I need to pass in a delay for the trigger. Can anyone see the flaw in my code? It should make it a 15 second delay but it never goes off.
UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
content.title = [NSString localizedUserNotificationStringForKey:@"Hello!" arguments:nil];
content.body = [NSString localizedUserNotificationStringForKey:@"Hello_message_body"
arguments:nil];
content.sound = [UNNotificationSound defaultSound];
content.categoryIdentifier = @"WhatUpsw";
NSString *imageName = @"hug2";
NSURL *url = [[NSBundle mainBundle]URLForResource:imageName withExtension:@"jpg"];
UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:imageName URL:url options:nil error:nil];
content.attachments = @[attachment];
// Deliver the notification in five seconds.
UNTimeIntervalNotificationTrigger* trigger = [ UNTimeIntervalNotificationTrigger
triggerWithTimeInterval:15 repeats:NO];
UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"FiveSecondsw"
content:content trigger:trigger];
// Schedule the notification.
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (error != nil) {
NSLog(@"Something went wrong: %@",error);
} else {
NSLog(@"All good!: %@", request);
}
}];
also checking pending requests i see:
<__NSArrayM 0x17024d170>(
<UNNotificationRequest: 0x1702235c0; identifier: FiveSecond, content: <UNNotificationContent: 0x17010bd00; title: Hello!, subtitle: (null), body: Hello_message_body, categoryIdentifier: , launchImageName: , peopleIdentifiers: (
), threadIdentifier: , attachments: (
), badge: (null), sound: <UNNotificationSound: 0x1700b4580>, hasDefaultAction: YES, defaultActionTitle: (null), shouldAddToNotificationsList: YES, shouldAlwaysAlertWhileAppIsForeground: NO, shouldLockDevice: NO, shouldPauseMedia: NO, isSnoozeable: NO, fromSnooze: NO, darwinNotificationName: (null), darwinSnoozedNotificationName: (null), trigger: <UNTimeIntervalNotificationTrigger: 0x170223780; repeats: NO, timeInterval: 10.000000>>
)
Does the problem occur only if your app is running in the foreground? If you close the app or switch to another one before the trigger time is reached, does the notification work properly?
In order to have notifications appear while the app is running, you need to set a UNUserNotificationCenterDelegate in your appController(_:didFinishLaunching:):
func appController(_ appController: TVApplicationController, didFinishLaunching options: [String : Any]?) {
// ...
UNUserNotificationCenter.current().delegate = self
// ...
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
completionHandler(.alert)
}
The delegate needs to be set before launching is complete.
I have spent the last 2 days fighting with this problem on iOS 13. It turns out that my notification identifier was either too long, or because I was using the same string for a localized string for the title/message it wasn't working.
Changing the ID of the notification so that it was 1. short, and 2. not the same as anything else solved my problems.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With