当应用程序在后台运行时,在无提示推送通知时显示用户通知

时间:2020-07-19 09:59:10

标签: ios swift push-notification unusernotificationcenter

这是我的情况: 我的应用收到带有有效负载的推送通知

"aps": {
            "content-available": 1,
            "alert": "hello",
            "sound": ""
          }

收到后,根据有效负载中的其他值,我决定是否以这种方式显示UserNotification:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
 if mylogic = true{
 self.scheduleNotification(userInfo: userInfo, inSeconds: 1, completion: { success in
                    if success {
                      print("Successfully scheduled notification")
                    } else {
                      print("Error scheduling notification")
                    }
                  })
}
}

当应用程序位于前台而不是后台时,此方法正常工作

条件: -发展概况 -不受限制的调试 -Xcode 12 Beta

这是预期的行为吗?如果是这样,当应用程序在后台运行时如何显示用户通知?

1 个答案:

答案 0 :(得分:0)

好的,我已经阅读了一些文档... 这里 https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/sending_notification_requests_to_apns/ 如果找到以下内容:

“背景 将背景推送类型用于在后台传递内容且不触发任何用户交互的通知。如果设置此推送类型,则apns-topic标头字段必须使用您应用的捆绑软件ID作为主题。“

那对我来说很成功