首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当应用程序处于后台时,不显示UNUserNotification

当应用程序处于后台时,不显示UNUserNotification
EN

Stack Overflow用户
提问于 2019-03-22 03:04:20
回答 1查看 507关注 0票数 1

我正在尝试获取一个本地通知,以便在指定的延迟后在后台触发,但由于某些原因,它在从我的应用程序调度时没有显示出来。我创建了一个简单的测试应用程序来测试相同的代码在指定的延迟之后显示预定通知的行为,因此我已经确认了我的代码是正确的。然而,相同的代码在我的应用程序中不会产生相同的结果。我有正确的权限等,但通知不显示。它将显示在前台,但不是当应用程序在后台时。我还检查了调度后的挂起通知,看看它是否在队列中,并且确实在那里。我在代码中搜索了所有对UNUserNotificationCenter.current()的引用,并添加了断点和注释代码,以确保没有其他内容与其交互。

下面是触发通知并验证通知是否已添加到队列的代码:

代码语言:javascript
复制
let content = UNMutableNotificationContent()
content.title = "Tap to trigger test"

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)

UNUserNotificationCenter.current().add(request) { (error) in
    print("error", error)
    UNUserNotificationCenter.current().getPendingNotificationRequests(completionHandler: { (requests) in
        print("requests", requests.count)
    })
}

下面是我的AppDelegate中的权限注册和UNUserNotificationCenterDelegate实现

代码语言:javascript
复制
public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    completionHandler(.alert)
}

public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    print(response)
}

public func registerForPushNotifications() {
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { [weak self] granted, error in
        print("Permission granted: \(granted)")
        guard granted else { return }
        self?.getNotificationSettings()
    }
}

public func getNotificationSettings() {
    UNUserNotificationCenter.current().getNotificationSettings { settings in
        print("Notification settings: \(settings)")
        guard settings.authorizationStatus == .authorized else { return }
        DispatchQueue.main.async {
            UIApplication.shared.registerForRemoteNotifications()
        }
    }
}

控制台输出:

代码语言:javascript
复制
Permission granted: true
Notification settings: <UNNotificationSettings: 0x280565260; authorizationStatus: Authorized, notificationCenterSetting: Enabled, soundSetting: Enabled, badgeSetting: Enabled, lockScreenSetting: Enabled, carPlaySetting: NotSupported, criticalAlertSetting: NotSupported, alertSetting: Enabled, alertStyle: Banner, providesAppNotificationSettings: No>
error nil 
requests 1

我还应该补充说,在测试中,它已经随机触发了几次。有一次,当我在添加请求完成块中设置断点时...

EN

回答 1

Stack Overflow用户

发布于 2019-03-22 04:43:27

尝试在内容中添加正文:

代码语言:javascript
复制
content.body = "Some content"

我不确定其他示例应用程序是如何工作的,而您的主应用程序不是使用相同的代码。但是我也不明白在没有content.body的情况下前台通知是如何显示的

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55287589

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档