簡體   English   中英

Apple推送通知(APN)不一致?

[英]Apple Push Notifications (APN) Inconsistency?

當通過APN使用Apple的推送通知時,我們遇到了一個令人困惑的問題。 我們有以下場景(非常標准我猜):

當我們的應用程序(我在這里稱之為“MyApp”)安裝並啟動時,我們第一次要求用戶通過“MyApp”向他發送推送通知的權限。

在這個例子中,AppDelegate看起來像這樣:

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。

現在困擾我的部分:

用戶還可以選擇稍后通過iPhone設置關閉推送通知,如下所示:設置>“MyApp”>通知>允許通知>關閉開關

我們的API現在具有針對APN的deviceToken,但用戶通過iPhone-Settings關閉了推送通知。

問題”:

在用戶關閉推送通知后,我們仍然可以向設備發送靜默推送通知,“MyApp”可以正確地獲取數據而不會出現任何問題。

但在另一種情況下:用戶安裝並啟動“MyApp”並在第一次啟動Push Notifications時拒絕從Apple獲取deviceToken。 我試圖從Apple獲得一個deviceToken,即使用戶拒絕了這樣的推送通知警告:(但這不起作用 - 我想Apple如果用戶拒絕則不提供我的任何功能)

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)

我在這里誤解了一件事嗎? 這似乎與我不一致。

我試圖澄清我的問題,這樣每個人都能理解我的要求。 請原諒我的“壞”英語,作為一個非母語人士,在這里提出具體問題並不容易。 無論如何,如果您需要進一步的信息,或者您不理解我要求的一個或多個要點,請告訴我,我將提供詳細的信息並澄清我的問題。

我不知道這是否重要,但目前我們正在使用APN-Development-Certificate(尚未提供分發證書)

好問題,

問題是,如果用戶允許您發送推送通知(給他/她的設備令牌),您將能夠發送推送。 通過通知的推送數據可以在沒有用戶通知的情況下發送(靜音通知),您可以在此處閱讀更多相關信息: https//medium.com/@m.imadali10/ios-silent-push-notifications-84009d57794c

這就是為什么即使用戶阻止通知顯示你也能發送推送的原因。 該設置僅控制顯示外觀,但由於他/她為您提供了令牌,您仍然可以向他們發送數據。 用戶實際上無法在授予令牌后禁用該令牌。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM