Local Notification is not displayed in iOS 10

我只是一个虾纸丫 提交于 2019-12-08 03:30:30

问题


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


回答1:


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);
    }



回答2:


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



来源:https://stackoverflow.com/questions/43177103/local-notification-is-not-displayed-in-ios-10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!