首页 > 解决方案 > 在第一次启动之后接受通知

问题描述

在我的应用程序中,用户可以使用从应用程序发送的通知。首次启动时,appdelegate 中会显示拒绝或允许通知的警报。如果我接受通知,一切正常。我制作了一个屏幕/视图控制器,用户可以在其中接受或不接受开关通知。如果用户在首次启动时拒绝通知,我如何访问 appdelegate 中的控件,就像我在首次启动时一样?

谢谢

标签: swiftxcodepush-notification

解决方案


根据Apple 的开发者文档和这个答案(为了澄清),推送通知权限只能请求一次。iOS 会在用户做出决定后存储用户的决定,并且无法再次请求。请为某些用户可能不想要通知的事实做好准备,您可以随时检查您的应用收到的权限状态:

let center = UNUserNotificationCenter.current()
center.getNotificationSettings { settings in
    guard settings.authorizationStatus == .authorized else { return }

    if settings.alertSetting == .enabled {
        // Schedule an alert-only notification.
    } else {
        // Schedule a notification with a badge and sound.
    }
}

推荐阅读