gpt4 book ai didi

ios - 在应用程序生命周期的另一个时间请求远程通知授权?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:31:38 26 4
gpt4 key购买 nike

我遇到了一个奇怪的问题。当我使用以下代码在 didFinishLaunchingWithOptions 中请求远程通知授权时,我能够从我的服务器(Firebase 消息传递)获得推送远程通知,但是当我稍后尝试请求授权时 NOT INSIDE didFinishLaunchingWithOptions 我无法收到任何通知。

我不想在发布时请求授权。例如,我想在用户登录后请求授权。

Apple 文档说:

Always call this method before scheduling any local notifications and before registering with the Apple Push Notification Service. Typically, you call this method at launch time when configuring your app's notification support. However, you may call it at another time in your app's life cycle, providing that you call it before performing any other notification-related tasks.

 // For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self

let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]

UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })

UIApplication.shared.registerForRemoteNotifications()

对这个问题有什么建议/解决方案吗?

应用信息:部署目标:iOS 10.3iOS 设备:装有 iOS 10 的 iPhone 6/装有 iOS 11 的 iPhone 7

最佳答案

我是这样解决的:

在我的 didFinishLaunchingWithOptions 中,我像这样注册远程通知:

UNUserNotificationCenter.current().delegate = self
UIApplication.shared.registerForRemoteNotifications()

然后稍后,例如在用户登录后,我通过调用下面的方法请求授权;

 func reqeust()  {
// For iOS 10 display notification (sent via APNS)
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]

UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in



})

}

关于ios - 在应用程序生命周期的另一个时间请求远程通知授权?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46427693/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com