【问题标题】:Can we call api on silent notification in background?我们可以在后台调用 api 进行静默通知吗?
【发布时间】:2019-12-12 22:07:54
【问题描述】:

是否可以调用 API 将数据发送到服务器,同时在 iOS 中获得静默推送通知?任何帮助将不胜感激。

提前致谢

【问题讨论】:

标签: ios apple-push-notifications silent-notification


【解决方案1】:

可以在收到静默推送后进行 API 调用。但是,如果应用被用户杀死,则无法调用。如果这是一个问题,请看这个answer

如果不是这样,这里是如何做到的。您需要在 XCode 的应用程序功能屏幕上的背景模式中启用“后台获取”和“远程通知”。

然后,将此application(_:didReceiveRemoteNotification:fetchCompletionHandler:) 方法添加到您的 AppDelegate 中。您可以在此方法中进行 API 调用。

例如:

 func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

   apiCall(fetchCompletionHandler: completionHandler)

}
func apiCall(fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void){

  //Make call here

    completionHandler(UIBackgroundFetchResult.noData)

}

【讨论】:

猜你喜欢
  • 2016-05-23
  • 2016-03-21
  • 1970-01-01
  • 2015-10-05
  • 2017-11-18
  • 2021-01-05
  • 1970-01-01
  • 1970-01-01
  • 2019-01-10
相关资源
最近更新 更多