我正在努力让后台通知在IOS上与GCM一起工作-非后台通知已经工作了。下面是我整合后台通知的步骤:
- 在UIBackgroundmodes中启用remote-notifications标签
- 将内容可用密钥添加到我的通知有效负载中。
- 在我的委托中编写应用程序: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;
}
?>
我已经尝试通过内部"数据"数组和外部"post"数组发送内容可用标志,我已经通过将其添加到两者中来指示。
消息没有被fetchCompletionHandler函数接收,而是等待,直到应用程序再次激活,并被正常的应用程序:didRecieveRemoteNotification函数接收。我的通知没有通过后台功能收到的原因是什么?
你应该提供content_available给GCM,而不是content-available(像apn),你将能够在后台接收推送通知。
https://developers.google.com/cloud-messaging/server-ref send-downstream
注:-不到五分钟前我也遇到了同样的问题。