【问题标题】:Testing silent push notifications in Xcode11.4 beta在 Xcode 11.4 beta 中测试静默推送通知
【发布时间】:2020-05-28 06:20:11
【问题描述】:

我想在 Xcode 11.4 模拟器中测试通知,除了静默通知之外,一切都运行良好。 didReceiveRemoteNotification 未触发。

我做了什么:

在后台模式下启用推送通知/在功能中启用远程通知

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { granted, _ in
        if granted {
            DispatchQueue.main.async {
                UIApplication.shared.registerForRemoteNotifications()
            }
        }
    }
    return true
}

didReceiveRemoteNotification 我只是在 UserDefaults 中设置了值以便以后使用它

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    print(userInfo)
    UserDefaults.standard.set("Hello world", forKey: "hello")
    completionHandler(.newData)
}

然后在 sceneDidBecomeActive 从 UserDefaults 中读取这个值的内容

    func sceneDidBecomeActive(_ scene: UIScene) {
    print(UserDefaults.standard.value(forKey: "hello")) // always nil
}

这是我的 .apns json

{
"Simulator Target Bundle": "xxx",
 "aps" : {
    "content-available" : 1
 }
}

【问题讨论】:

    标签: swift xcode push-notification apple-push-notifications


    【解决方案1】:

    看来,可能是由于一个错误(我相信已报告),apns 文件导致调用performFetchWithCompletionHandler,而不是调用didReceiveRemoteNotification

    【讨论】:

      【解决方案2】:

      Jeff 的解决方案对我有用,此外我必须向应用添加“后台获取”模式

      【讨论】:

        猜你喜欢
        • 2020-10-03
        • 2018-02-17
        • 2018-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-24
        • 2015-04-26
        • 1970-01-01
        相关资源
        最近更新 更多