1

I am unable to fire a UserNotification in iOS 10. I will start with my code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

//

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        UNAuthorizationOptions options = (UNAuthorizationOptionBadge | UNAuthorizationOptionAlert | UNAuthorizationOptionSound);

        center.delegate = self;

        [center requestAuthorizationWithOptions: options
                              completionHandler: ^(BOOL granted, NSError * _Nullable error) {
                                  if (granted) {
                                      NSLog(@"Granted notifications!");
                                  }
                              }];
//

This works, I see the log.

Then, I have:

-(void)application: (UIApplication *)application performFetchWithCompletionHandler: (void (^)(UIBackgroundFetchResult))completionHandler {
 //

UNMutableNotificationContent *content = [UNMutableNotificationContent new];
                content.title = NSLocalizedString(@"Updates are available", nil);

                UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval: 1
                                                                                                                repeats: NO];
                UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier: @"IDLocalNotification"
                                                                                      content: content
                                                                                      trigger: trigger];

                UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

                [center addNotificationRequest: request withCompletionHandler: ^(NSError * _Nullable error) {
                    if (!error) {
                        NSLog(@"Notification was requested!");
                    }
                }];

Again, this works, I see the log.

But I don't see a notification appear on my screen (the trigger delay is only 1 sec, but I tried larger values as well).

Also, the willPresentNotification delegate method is never reached.

What am I missing?

koen
  • 5,383
  • 7
  • 50
  • 89
  • You don't see you own notifications in your app when you launch one if it's in foreground. – Larme Jan 21 '17 at 22:32

1 Answers1

4

Found the problem.

It turns out that I need to set the body of the content, not just the title:

UNMutableNotificationContent *content = [UNMutableNotificationContent new];
content.body = [NSString localizedUserNotificationStringForKey: @"Updates are available" arguments: nil];

I can even leave the title out and it works.

koen
  • 5,383
  • 7
  • 50
  • 89