我们可以在后台无提示通知中调用api吗?

时间:2019-08-05 11:27:41

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

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

预先感谢

1 个答案:

答案 0 :(得分:0)

可以在收到静默推送后进行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)

}