IOS 使用 PHP 发件人和 Swift 在后台未收到 GCM 推送通知
IOS GCM push notifications not received in background using PHP sender and Swift
我正在努力让后台通知在 IOS 上与 GCM 一起工作 - 非后台通知已经在工作。以下是我集成后台通知的步骤:
- 在 UIBackgroundmodes 中启用远程通知标签
- 将内容可用密钥添加到我的通知负载中。
- 在我的委托中写下 application: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;
}
?>
我已经尝试通过内部 "data" 数组和外部 "post" 数组发送内容可用标志,我已通过将其添加到两者来指示。
该消息未被 fetchCompletionHandler 函数接收,而是等到应用程序再次处于活动状态并由正常的 application:didRecieveRemoteNotification 函数接收。没有通过后台功能收到我的通知的原因可能是什么?
您应该向 GCM 提供 content_available,而不是 content-available(就像在 APN 中一样),您将能够在后台接收推送通知。
https://developers.google.com/cloud-messaging/server-ref#send-downstream
P.S。 - 不到五分钟前我遇到了完全相同的问题。我们应该更仔细地阅读文档...
我正在努力让后台通知在 IOS 上与 GCM 一起工作 - 非后台通知已经在工作。以下是我集成后台通知的步骤:
- 在 UIBackgroundmodes 中启用远程通知标签
- 将内容可用密钥添加到我的通知负载中。
- 在我的委托中写下 application: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;
}
?>
我已经尝试通过内部 "data" 数组和外部 "post" 数组发送内容可用标志,我已通过将其添加到两者来指示。
该消息未被 fetchCompletionHandler 函数接收,而是等到应用程序再次处于活动状态并由正常的 application:didRecieveRemoteNotification 函数接收。没有通过后台功能收到我的通知的原因可能是什么?
您应该向 GCM 提供 content_available,而不是 content-available(就像在 APN 中一样),您将能够在后台接收推送通知。
https://developers.google.com/cloud-messaging/server-ref#send-downstream
P.S。 - 不到五分钟前我遇到了完全相同的问题。我们应该更仔细地阅读文档...