Apple 推送通知 (APN) 不一致? [英] Apple Push Notifications (APN) Inconsistency?

查看:25
本文介绍了Apple 推送通知 (APN) 不一致?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在通过 APN 使用 Apple 的推送通知时,我们遇到了一个令人困惑的问题.我们有以下场景(我猜很标准):

We are running into a confusing issue when using Apple's Push Notifications via APN. We have the following scenario (quite standard i guess):

当我们的应用程序(这里我们称之为MyApp")安装并首次启动时,我们会要求用户授予通过MyApp"向他发送推送通知的权限.

When our App, let's call it "MyApp" here, is installed and started for the first time we are asking the user for permissions to send him Push Notifications through "MyApp".

在这个例子中,AppDelegate 看起来像这样:

In this example the AppDelegate looks like this:

import UIKit
import UserNotifications

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        // Register Remote Notifications
        UNUserNotificationCenter.current().delegate = self
        self.registerForPushNotifications()

        return true
    }

    // MARK: - Remote Notifications

    func registerForPushNotifications() {
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in
            guard granted else {
                return
            }
            self.getNotificationSettings()
        }
    }

    func getNotificationSettings() {
        UNUserNotificationCenter.current().getNotificationSettings { (settings) in
            guard settings.authorizationStatus == .authorized else {
                return
            }
            DispatchQueue.main.async {
                UIApplication.shared.registerForRemoteNotifications()
            }
        }
    }

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        let tokenParts = deviceToken.map { (data) -> String in
            return String(format: "%02.2hhx", data)
        }
        let token = tokenParts.joined()
        print("ApnToken: \(token)")
    }

    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        print("did Fail to Register for RemoteNotifications")
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        print("willPresentNotification!")
        completionHandler([.badge, .sound, .alert])
    }

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

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        print("DidReceiveRemoteNotification!")
        completionHandler(.newData)
    }
}

因此用户安装并启动应用程序,并询问是否允许MyApp"向用户发送推送通知.如果用户接受推送通知 application(_:didRegisterForRemoteNotificationsWithDeviceToken:) 被调用,我们将收到的 deviceToken 提供给我们的 API.

So the user installs and the starts the app and is asked if "MyApp" is allowed to send the user Push Notifications. If the user accepts the Push Notifications application(_:didRegisterForRemoteNotificationsWithDeviceToken:) is called and we give the received deviceToken to our API.

现在让我困惑的部分:

用户还可以选择稍后通过 iPhone 设置关闭推送通知,如下所示:设置 >我的应用程序"> 通知 > 允许通知 > 关闭开关

The user also has the option to turn Push Notifications off later via the iPhone-Settings like this: Settings > "MyApp" > Notifications > Allow Notifications > Turns the Switch off

我们的 API 现在具有 APN 的 deviceToken,但用户通过 iPhone 设置关闭了推送通知.

Our API has now the deviceToken for APN but the user turned Push Notifications off via iPhone-Settings.

问题":

在用户关闭推送通知后,我们仍然可以向设备发送静默推送通知,并且MyApp"可以毫无问题地获取正确的数据.

After the user turned off Push Notifications we are still able to send silent Push Notifications to the device and "MyApp" gets the data correct without any problems.

但在另一种情况下:用户安装并启动MyApp"并在第一次启动时拒绝推送通知,无法从 Apple 获取 deviceToken.即使用户拒绝了这样的推送通知警报,我也尝试从 Apple 获取 deviceToken:(但这不起作用 - 我猜如果用户拒绝,Apple 不会提供我的任何)

But in the other scenario: The User installs and starts "MyApp" and declines at the first start Push Notifications it is not possible to get a deviceToken from Apple. I tried to get a deviceToken from Apple even if the user declined the Push Notification Alert like this: (But this doesn't work - I guess Apple doesn't provide my any if the user declines)

func registerForPushNotifications() {
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in
            self.getNotificationSettings()
        }
    }

    func getNotificationSettings() {
        UNUserNotificationCenter.current().getNotificationSettings { (settings) in
            DispatchQueue.main.async {
                UIApplication.shared.registerForRemoteNotifications()
            }
        }
    }

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        let tokenParts = deviceToken.map { (data) -> String in
            return String(format: "%02.2hhx", data)
        }
        let token = tokenParts.joined()
        print("ApnToken: \(token)")
    }

如果用户在第一次开始时接受推送通知,他似乎并不重要.我的意思是好的,我们不能通过横幅或任何东西向用户显示信息,但我们可以使用 APN 将数据传输到设备,即使用户稍后确实关闭了此设置.(但如果他在应用程序启动时拒绝,我们将无法发送任何内容 - 我们需要一次 deviceToken)

It seems like it doesn't matter what the user does if he accepts Push Notifications at the first start. I mean ok, we cannot show the Information via Banner or anything to the user but we can transfer data to the device using APN's even if the user did turn off this setting at a later time. (But we cannot send anything if he declines at start of the app - we need a deviceToken once)

我在这里误解了什么吗?这对我来说似乎不一致.

Am i misunderstanding a thing here? This seems inconsistent to me.

我试图澄清我的问题,以便每个人都能理解我的要求.请原谅我的糟糕"英语,作为非母语人士在这里提出具有大量上下文的特定问题并不容易.无论如何,如果您需要更多信息,或者您不明白我所问的一个或多个要点,请告诉我,我将提供详细信息并澄清我的问题.

I tried to clarify my question so everyone can understand what i am asking for. Excuse my "bad" english it is not easy as a non-native-speaker to ask specific questions with a lot of context here. Anyway, if you need further informations or you didn't understand one or more points from what i am asking let me know i will provide detailed informations and will clarify my question.

我不知道这是否重要,但目前我们正在使用 APN-Development-Certificate(还不是分发证书)

I don't know if this matters but at the moment we are using an APN-Development-Certificate (not a distribution certificate yet)

推荐答案

好问题,

问题是,如果用户允许您发送推送通知(给您他/她的设备令牌),您将能够发送推送.通过通知推送数据可以在没有通知用户的情况下发送(静音通知),您可以在此处阅读更多相关信息:https://medium.com/@m.imadali10/ios-silent-push-notifications-84009d57794c

The thing is that if user allows you to send push notification (gives you his/her device token) you would be able to send pushes. The push data via notification could be send without notification for a user (silence notification) you could read more about it here: https://medium.com/@m.imadali10/ios-silent-push-notifications-84009d57794c

这就是为什么即使用户阻止显示通知您也可以发送推送.该设置仅控制显示外观,但由于他/她给了您令牌,您仍然可以向他们发送数据.在授予令牌后,用户实际上无法禁用该令牌.

That's why you are able to send push even if user prevents notification from displaying. That settings controls only displaying appearance, but since he/she gives you the token you still could send data to them. There actually no way for a user to disable that token after it was granted.

这篇关于Apple 推送通知 (APN) 不一致?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆