使用 PHP 发送器和 Swift 在后台未收到 IOS GCM 推送通知 [英] IOS GCM push notifications not received in background using PHP sender and Swift
问题描述
我正在使用 GCM 获取后台通知以在 IOS 上工作 - 非后台通知已经在工作.以下是我集成后台通知的步骤:
I'm working on getting background notifications to work on IOS with GCM - non-background notifications are already working. Here are my steps to integrate background notifications:
- 在 UIBackgroundmodes 中启用远程通知标签
- 将 content-available 密钥添加到我的通知负载中.
- 在我的委托中编写应用程序: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 脚本的代码:
This is the code for the on-server php script:
<?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;
}
?>
我已尝试通过内部data"数组和外部post"数组发送 content-available 标志,我已通过将其添加到两者中来表示.
I have tried sending the content-available flag through both the inner "data" array and the outer "post" array, which I have indicated by adding it to both.
消息不是由 fetchCompletionHandler 函数接收的,而是等待应用程序再次激活并由普通应用程序接收:didRecieveRemoteNotification 函数.没有通过后台功能收到我的通知的原因可能是什么?
The message is not received by the fetchCompletionHandler function, but rather waits until the app is active again and is received by the normal application:didRecieveRemoteNotification function. What could be the reason that my notification is not received via the background functionality?
推荐答案
您应该向 GCM 提供 content_available,而不是 content-available(如在 APNs 中),您将能够在后台接收推送通知.
You should provide content_available to GCM, not content-available (like in APNs) and you will be able to receive push notifications in background.
https://developers.google.com/cloud-messaging/server-ref#send-downstream
P.S.- 不到五分钟前,我遇到了完全相同的问题.我们应该更仔细地阅读文档...
这篇关于使用 PHP 发送器和 Swift 在后台未收到 IOS GCM 推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!