当前位置: 首页 > 知识库问答 >
问题:

GCM与PHP(谷歌云消息)

双俊人
2023-03-14

更新:已弃用GCM,请使用FCM

如何将新的Google云消息集成到PHP后端?

共有1个答案

谷梁永年
2023-03-14

此代码将通过PHP CURL向多个注册ID发送GCM消息

// Payload data you want to send to Android device(s)
// (it will be accessible via intent extras)    
$data = array('message' => 'Hello World!');

// The recipient registration tokens for this notification
// https://developer.android.com/google/gcm/    
$ids = array('abc', 'def');

// Send push notification via Google Cloud Messaging
sendPushNotification($data, $ids);

function sendPushNotification($data, $ids) {
    // Insert real GCM API key from the Google APIs Console
    // https://code.google.com/apis/console/        
    $apiKey = 'abc';

    // Set POST request body
    $post = array(
                    'registration_ids'  => $ids,
                    'data'              => $data,
                 );

    // Set CURL request headers 
    $headers = array( 
                        'Authorization: key=' . $apiKey,
                        'Content-Type: application/json'
                    );

    // Initialize curl handle       
    $ch = curl_init();

    // Set URL to GCM push endpoint     
    curl_setopt($ch, CURLOPT_URL, 'https://gcm-http.googleapis.com/gcm/send');

    // Set request method to POST       
    curl_setopt($ch, CURLOPT_POST, true);

    // Set custom request headers       
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    // Get the response back as string instead of printing it       
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // Set JSON post data
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));

    // Actually send the request    
    $result = curl_exec($ch);

    // Handle errors
    if (curl_errno($ch)) {
        echo 'GCM error: ' . curl_error($ch);
    }

    // Close curl handle
    curl_close($ch);

    // Debug GCM response       
    echo $result;
}
 类似资料:
  • 我正在尝试,为了实施新的GCM,我遵循Google get声明:http://developer.android.com/guide/google/gcm/gs.html 我被困在获取我的设备注册ID! 我的应用程序一直试图与谷歌服务器连接,这里我的错误日志: 这是我的活动代码,询问身份证: 这是我的服务代码 这是我的Android清单: 我遵循谷歌的指示,但我坚持这个服务不可用的错误, 我做错了

  • 上面写着“Google Cloud Messaging(GCM)是一个免费服务”,但是为了使它能够运行,我需要在Google Cloud平台中创建一个项目,这需要花钱…那怎么免费呢?还是我错过了什么?

  • 我正在尝试在我的android应用程序中使用GCM服务。 为此,我使用Android留档从http://developer.android.com/guide/google/gcm/gcm.html 我用发送者id等创建了客户端注册过程,并在服务器端应用程序中使用注册id和发送者id发送消息。 当我通过Eclipse在手机中安装应用程序时,推送通知工作正常,因此我的发件人id是正确的。 然后,当我

  • 我正在使用GCM向Android应用程序发送和接收推送通知。我是从这里给出的一个例子中创建这个应用程序的(Android的谷歌云消息GCM和推送通知) 这些是我正在使用的java文件 最终语境=此; shareRegidTask=new AsyncTask() AsyncTask()上出错 通知兼容。Builder mBuilder=new NotificationCompat。建造商( 这).

  • 是否建议使用android云备份来备份GCM id?我将GCM id存储在SharedReferences中,因此我不必继续注册。 如果我备份此GCM sharedpref和用户更改设备,我是否需要将其更新为新的注册id?注册ID是否特定于设备?当用户更改设备并备份时会发生什么情况? 我试着从文档中读到: 您必须排除任何特定于设备的标识符,无论是由服务器发布的还是在设备上生成的。这包括谷歌云消息(

  • 我知道可以使用谷歌API视觉在网上找到类似的图片。我的目标是根据我的图像数据库找到类似的图像。我不想在网上看到类似的图片。 可能吗? 如果是,你能给我一些链接或建议吗? 谢谢