首页 > 解决方案 > 当用户在“设置”应用中更改推送通知设置时如何获得通知?

问题描述

考虑以下场景:

已安装
应用程序启动
推送通知➝请求
推送通知➝不允许
应用程序关闭或在后台
设置➝用户启用推送通知

如何在将应用程序移至前台的情况下获得有关此更改的通知,以便即使用户未打开应用程序也可以将设备令牌发送到我们的后端服务器以接收通知?

UNUserNotifications我知道您可以使用这样的框架检查是否允许通知:

static func checkNotificationStatus() {
    UNUserNotificationCenter.current().getNotificationSettings { settings in

        switch settings.authorizationStatus {
        case .authorized: generatePushToken()
        case .provisional: generatePushToken()
        case .denied: break
        case .notDetermined:
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
                checkNotificationStatus()
            }
        @unknown default: break
        }
    }
}

但这仅在您的应用程序移至前台或再次启动后才会发生。

如果这些都没有发生怎么办?

标签: iosswiftapple-push-notificationsunusernotificationcenter

解决方案


推荐阅读