菜鸟教程小白 发表于 2022-12-11 20:06:04

ios - 即使注册成功,isRegisteredForRemoteNotifications 也会返回 false


                                            <p><h2>问题</h2>

<pre><code>isRegisteredForRemoteNotifications
</code></pre>

<p>return <strong>false</strong> 即使我成功收到了设备 token 。</p>

<h2>情况</h2>

<h3>1。注册</h3>

<p>用户安装我的应用程序后,在特定时间点,我检查 <code>isRegisteredForRemoteNotifications</code>,如果为假,我使用上面的代码要求用户允许通知</p>

<pre><code>func registerUserNotificationSettings() {
    let userNotificationTypes: UIUserNotificationType = ([.alert, .badge, .sound])
    let settings = UIUserNotificationSettings(types: userNotificationTypes, categories: nil)
    UIApplication.shared.registerUserNotificationSettings(settings)
}
</code></pre>

<p>在显示对话框并且以太用户推送允许或禁止之后,</p>

<pre><code>didRegister notificationSettings
</code></pre>

<p> 被调用。我们叫 </p>

<pre><code>application.registerForRemoteNotifications
</code></pre>

<p>在里面。哪个调用</p>

<pre><code>didRegisterForRemoteNotificationsWithDeviceToken
</code></pre>

<p>然后我们将 token 发送到我们的服务器。</p>

<h3>2。检查用户是否关闭了推送权限</h3>

<p>在上述操作之后,如果<strong>使用关闭通知表单 iOS 设置应用程序</strong>,我会显示一个模式警报,要求用户在某个时候将通知设置更改为 ON。我判断通过这段代码显示模态。</p>

<pre><code>func isApproved() -&gt; Bool {
    guard let setting = UIApplication.shared.currentUserNotificationSettings else {
      return false
    }
    return setting.types.rawValue &gt; 0
}
</code></pre>

<p>如果 isApproved == false,我们会显示模态。</p>

<h3>3。将用户发送到设置应用</h3>

<p>在模态框上,我们放置了一个调用以下方法的按钮</p>

<pre><code>func showAppropriateNotificationConfigView() {

    if UIApplication.shared.isRegisteredForRemoteNotifications == false {
      // case the app has never called registerUserNotificationSettings or registration failed
      registerUserNotificationSettings()

    } else {
      // registerUserNotificationSettings has beeen called and registration succeed
      UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)
    }
}
</code></pre>

<p>我们的大多数测试设备都返回 true(我们确认设备 token 已成功接收)</p>

<pre><code>UIApplication.shared.isRegisteredForRemoteNotifications
</code></pre>

<p>但我们只有一台设备(iPhone7 加 iOS 11.3)返回 false。
这样</p>

<pre><code>registerUserNotificationSettings
</code></pre>

<p>被调用并且不显示任何内容,而不是通过</p>将用户发送到设置应用程序

<pre><code>UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)
</code></pre>

<h2>问题</h2>

<p>我的代码有什么问题,我们该如何解决?</p>

<p>我了解某些方法已被弃用,但我需要临时快速修复它。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我通过更新所有相关的弃用方法解决了这个问题。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 即使注册成功,isRegisteredForRemoteNotifications 也会返回 false,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/50114522/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/50114522/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 即使注册成功,isRegisteredForRemoteNotifications 也会返回 false