Mad Burea
Mad Burea

Reputation: 541

Local Notification is not displayed in iOS 10

i have tried this Code in Appdelegates.

@import UserNotifications;


UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert)
        completionHandler:^(BOOL granted, NSError * _Nullable error) {
            if (!error) {
            NSLog(@"request authorization succeeded!");

                center.delegate = self;
            }
    }];

And the local notification is fired by this code

-(void)localNotification{

NSLog(@"local notification fired");

UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init];

objNotificationContent.title = @"local notificatin";

objNotificationContent.body = @"message";

objNotificationContent.sound = [UNNotificationSound defaultSound];

/// 4. update application icon badge number
objNotificationContent.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1);

// Deliver the notification in five seconds.
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger
                                              triggerWithTimeInterval:0.3 repeats:NO];

    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"three"
                                                                      content:objNotificationContent trigger:trigger];

/// 3. schedule localNotification
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
    if (!error) {
        NSLog(@"Local Notification succeeded");
    }
    else {
        NSLog(@"Local Notification failed");
    }
}];

}

But Local notification is not displayed. As i have used two Delegates method one is for present notification in foreground and one method that Must be called when local notification received.

that Delegates methods does not called at any Scenario.

please find Silly Mistake where i m wrong

Upvotes: 3

Views: 7479

Answers (2)

aashish tamsya
aashish tamsya

Reputation: 4959

You are unable to see the local notification because your application might be in foreground. Local Notification doesn't show up if you are in foreground.

Your code is okay, but I would suggest to make some changes & test your app again.

  1. Increase trigger time to 10 seconds. UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:10 repeats:NO];
  2. Added following in applicationDidEnterBackground

[self localNotification];

  1. Make the application to go background.
  2. Wait for 10 seconds, your will get local notification.

Show Notification on Foreground

If you want to show notification when your application is in foreground then, implement

  1. Conform UNUserNotificationCenterDelegate in AppDelegate.h

  2. Add following code in AppDelegate.m

    -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
    completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge);
    }

Upvotes: 9

carlos21
carlos21

Reputation: 485

If you're in foreground, your delegate method will be called but no visual notification will be displayed. If you really want to show a notification in foreground you can use something like:

https://github.com/pikacode/EBForeNotification

Upvotes: 0

Related Questions