iOS 10 中不显示本地通知 [英] Local Notification is not displayed in iOS 10

查看:52
本文介绍了iOS 10 中不显示本地通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在 Appdelegates 中尝试过这个代码.

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

}

但不显示本地通知.因为我使用了两种 Delegates 方法,一种是用于前台的当前通知,另一种是在收到本地通知时必须调用的方法.

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.

请找出我错的地方

推荐答案

您无法看到本地通知,因为您的应用程序可能在前台.如果您在前台,则不会显示本地通知.

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

[self localNotification];

  1. 使应用程序进入后台.
  2. 等待 10 秒,您将收到本地通知.

在前台显示通知

如果您想在应用程序处于前台时显示通知,请实施

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

  1. AppDelegate.h

AppDelegate.m

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

这篇关于iOS 10 中不显示本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆