简体   繁体   English

iOS 10中不显示本地通知

[英]Local Notification is not displayed in iOS 10

i have tried this Code in Appdelegates. 我曾在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. 由于我使用了两种Delegates方法,一种是在前台进行当前通知,另一种是在收到本地通知时必须调用的方法。

that Delegates methods does not called at any Scenario. 在任何情况下都不会调用Delegates方法。

please find Silly Mistake where im wrong 请在我错的地方找到愚蠢的错误

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. 将触发时间增加到10秒。 UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:10 repeats:NO];
  2. Added following in applicationDidEnterBackground applicationDidEnterBackground添加了以下内容

[self localNotification];

  1. Make the application to go background. 使应用程序进入后台。
  2. Wait for 10 seconds, your will get local notification. 等待10秒钟,您将收到本地通知。

Show Notification on Foreground 在前台显示通知

If you want to show notification when your application is in foreground then, implement 如果您想在应用程序处于前台时显示通知,请实施

  1. Conform UNUserNotificationCenterDelegate in AppDelegate.h AppDelegate.h符合UNUserNotificationCenterDelegate

  2. Add following code in AppDelegate.m AppDelegate.m添加以下代码

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

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://github.com/pikacode/EBForeNotification

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM