I am trying to implement local notifications on an app that I am building. Most of my code follows what is written In the documentation
I will post my code below. My current problem is that the notifications never appear. The first time I loaded the app the permission screen appeared and I said "Allow"
In AppDelegate in the didFinishLaunchingWithOptions method
didFinishLaunchingWithOptions
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError * _Nullable error) {
[self setupNotification];
}];
The following is also in AppDelegate
-(void)setupNotification {
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = [NSString localizedUserNotificationStringForKey:@"New:" arguments:nil];
content.body = [NSString localizedUserNotificationStringForKey:@"New Notification"
arguments:nil];
content.sound = [UNNotificationSound defaultSound];
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger
triggerWithTimeInterval:5
repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"NOTIFICATION"
content:content
trigger:trigger];
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"success");
}
}];
}
As I said before, the notification never appears and I cant figure out why. I set up the content, the trigger and the request, then I add the request to the UNUserNotificationCenter.
Does anyone have a working example of this or can tell me where I am going wrong?
I found a similar answer here but this answer doesnt address why UNTimeIntervalNotificationTrigger
isnt working and instead explains how to set up a UNCalendarNotificationTrigger
Thanks