【问题标题】:Android GCM gives Null messageAndroid GCM 给出 Null 消息
【发布时间】:2015-02-15 16:11:23
【问题描述】:

大家好,我在我的应用中使用 Google 云消息传递。我正在关注我的应用程序的 android hive 教程。下面给出了教程的链接。

http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/

对我来说一切正常,但问题是当从远程服务器发送消息时,在客户端我收到消息警报,但没有消息只是写入空消息。我收到空消息。我该如何解决?

消息接收的 Java 代码

/** * 接收新消息时调用的方法 * */

  @Override
  protected void onMessage(Context context, Intent intent) {
    Log.i(TAG, "Received message");
    String message = intent.getExtras().getString("price");

    displayMessage(context, message);
    // notifies user
    generateNotification(context, message);
}

服务器端代码

//put your code here
// constructor
function __construct() {

}


/**
 * Sending Push Notification
 */
public function send_notification($registatoin_ids, $message) {
    // include config
    include_once($_SERVER["DOCUMENT_ROOT"] ."*****");

    $apiKey = "*******";
    // Set POST variables
    $url = 'https://android.googleapis.com/gcm/send';

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

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

    // Open connection
    $ch = curl_init();

    // Set the url, number of POST vars, POST data
    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // Disabling SSL Certificate support temporarly
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

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

    // Close connection
    curl_close($ch);
    echo $result;
}

}

消息数组

   $message = array("message" => $message);

提前致谢。

【问题讨论】:

    标签: android google-cloud-messaging


    【解决方案1】:

    这是一个猜测,确保从服务器发送的key 与您在客户端使用的相同。在本教程中,它的 price 。所以请确保两者相同。

    服务器端: $message = array("price" => $message);

    安卓:

    @Override
        protected void onMessage(Context context, Intent intent) {
            Log.i(TAG, "Received message");
            String message = intent.getExtras().getString("price");
    
            displayMessage(context, message);
            // notifies user
            generateNotification(context, message);
        }
    

    【讨论】:

    • 这不是一个实际的答案
    • 为什么伙计?我提到过,是个猜测,他有可能错过了。
    • 你应该建议他在猜测之前显示服务器端代码
    • 我在两边都使用我的关键信息
    • @Amy 哦。我以任何方式发布它。下次一定会考虑的。
    【解决方案2】:

    如果您从服务器发送“消息”参数:

    $message = array("message" => $message);
    

    您应该从以下位置更改客户端代码:

    String message = intent.getExtras().getString("price");
    

    到:

    String message = intent.getExtras().getString("message");
    

    这样,客户端代码会读取您从服务器发送给它的参数。

    【讨论】:

    • 从服务器通过带有“价格”键的产品传递整个阵列
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多