简体   繁体   English

Android Google Cloud消息传递应用程序无法发送消息并在php中给出错误

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

Friends, I am working on a simple android test app with the basic task to send a message from a php application server to device from gcm. 朋友,我正在开发一个简单的android测试应用程序,其基本任务是将消息从php应用程序服务器发送到gcm的设备。 I am able to register on gcm from the device and then i try to send the registration id to the web application. 我可以通过设备在gcm上进行注册,然后尝试将注册ID发送到Web应用程序。 following are the main files associated with the app where I think something may needs to be rectified: 以下是与该应用程序相关联的主要文件,我认为可能需要纠正一些问题:

   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";

}

This is the error that I am getting when i try to send a push message from the php file. 这是我尝试从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"}]}

Kindly help me guys. 请帮助我。

Use this below code, its work for me. 使用下面的代码,对我有用。 Just pass the device id and message it will send a notification. 只需传递设备ID和消息,它将发送通知。 Check it 核实

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 " means Registration id that you sent is not registered. InvalidRegistration ”表示您发送的注册ID未注册。 Check API key and registration id correctly. 正确检查API密钥和注册ID。 Check the formatting of the registration ID that you pass to the server. 检查传递给服务器的注册ID的格式。 So basically you are making an error in sending the DeviceID received by the phone to the server. 因此,基本上,将手机接收到的DeviceID发送到服务器时会出错。

You may check the part of Receive a downstream messaging here: https://developers.google.com/cloud-messaging/ 您可以在此处查看“接收下游消息”部分: https : //developers.google.com/cloud-messaging/

Here's a useful link regarding Registration Token: https://developers.google.com/cloud-messaging/android/client#sample-register 这是有关注册令牌的有用链接: https : //developers.google.com/cloud-messaging/android/client#sample-register

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM