簡體   English   中英

Android Google Cloud消息傳遞應用程序無法發送消息並在php中給出錯誤

[英]android google cloud messaging app not able to send messages and giving error in php

朋友,我正在開發一個簡單的android測試應用程序,其基本任務是將消息從php應用程序服務器發送到gcm的設備。 我可以通過設備在gcm上進行注冊,然后嘗試將注冊ID發送到Web應用程序。 以下是與該應用程序相關聯的主要文件,我認為可能需要糾正一些問題:

   Congfig.java

public interface Config {

    // used to share GCM regId with application server - using php app server
    static final String APP_SERVER_URL = "http://49.249.146.129/gcm/gcm.php?shareRegId=1";

    // GCM server using java
    // static final String APP_SERVER_URL =
    // "http://192.168.1.17:8080/GCM-App-Server/GCMNotification?shareRegId=1";

    // Google Project Number
    //static final String GOOGLE_PROJECT_ID = "512218038480";
    static final String GOOGLE_PROJECT_ID = "1190";
    static final String MESSAGE_KEY = "message";

}

這是我嘗試從php文件發送推送消息時遇到的錯誤。

           Warning: file_get_contents(GCMRegId.txt): failed to open stream: No such file or directory in C:\xampp\htdocs\gcm\gcm.php on line 37
Google Cloud Messaging (GCM) Server in PHP
  {"multicast_id":7821558258646259390,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}

請幫助我。

使用下面的代碼,對我有用。 只需傳遞設備ID和消息,它將發送通知。 核實

function send_notification($registatoin_ids, $message) {
    $url = 'https://android.googleapis.com/gcm/send';

    $fields = array(
        'registration_ids' => $registatoin_ids,
        'data' => $message,
    );

    define("GOOGLE_API_KEY", "API Key Here");

    $headers = array(
        'Authorization: key=' . GOOGLE_API_KEY,
        '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_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

    $result = curl_exec($ch);
    if ($result === FALSE) {
        die('Curl failed: ' . curl_error($ch));
    }

    curl_close($ch);
    return $result;
}

 $pushMessage = $name." was created meeting to you.";

            if(isset($gcmRegIds) && isset($pushMessage)) {
                $message = array('message' => $pushMessage, 'type' => 'Request', 'token' => $token);
                $pushStatus = $this->send_notification($gcmRegIds, $message);
            }

InvalidRegistration ”表示您發送的注冊ID未注冊。 正確檢查API密鑰和注冊ID。 檢查傳遞給服務器的注冊ID的格式。 因此,基本上,將手機接收到的DeviceID發送到服務器時會出錯。

您可以在此處查看“接收下游消息”部分: https : //developers.google.com/cloud-messaging/

這是有關注冊令牌的有用鏈接: https : //developers.google.com/cloud-messaging/android/client#sample-register

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM