使用PHP发件人和Swift在后台收到IOS GCM推送通知

我正在使用GCM获取背景通知以在IOS上工作 – 非背景通知已经在工作。 以下是我整合背景通知的步骤:

  1. 在UIBackgroundmodes中启用remote-notifications标签
  2. 将内容可用密钥添加到我的通知负载。
  3. 编写应用程序:didRecieveRemoteNotification:fetchCompletionHandler:在我的委托。

以下是委托函数的代码:

func application( application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler handler: (UIBackgroundFetchResult) -> Void) { println("Notification received2: \(userInfo)") GCMService.sharedInstance().appDidReceiveMessage(userInfo); NSNotificationCenter.defaultCenter().postNotificationName(messageKey, object: nil, userInfo: userInfo) handler(UIBackgroundFetchResult.NoData); } 

这是服务器上php脚本的代码:

 <?php $regId = $_GET["regId"]; $message = $_GET["message"]; $data = array( 'price' => $message, 'sound' => 'default', 'body' => 'helloworld', 'title' => 'default', 'badge' => 12, 'content-available' => 1); $ids = array( $regId); sendGoogleCloudMessage( $data, $ids ); function sendGoogleCloudMessage( $data, $ids ) { $apiKey = THE-API-KEY-THAT-I-AM-USING; $url = 'https://android.googleapis.com/gcm/send'; $post = array( 'registration_ids' => $ids, 'data' => $data, 'content-available' => 1, ); $headers = array( 'Authorization: key=' . $apiKey, 'Content-Type: application/json' ); $ch = curl_init(); curl_setopt( $ch, CURLOPT_URL, $url ); curl_setopt( $ch, CURLOPT_POST, true ); curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $post ) ); $result = curl_exec( $ch ); if ( curl_errno( $ch ) ) { echo 'GCM error: ' . curl_error( $ch ); } curl_close( $ch ); echo $result; } ?> 

我已经尝试通过内部“数据”数组和外部“后”数组发送内容可用标志,我已经通过将它添加到两个表示。

该消息没有被fetchCompletionHandler函数接收到,而是一直等到该应用程序再次处于活动状态,并被正常应用程序didRecieveRemoteNotification函数接收。 什么可能是我的通知没有通过后台function收到的原因?

您应该向GCM提供content_available ,而不是内容可用 (如在APN中),并且您将能够在后台接收推送通知。

https://developers.google.com/cloud-messaging/server-ref#send-downstream

PS – 不到五分钟前我有完全相同的问题。 我们应该仔细阅读文档…