如何从HTTP gcm服务器获取gcm数据?

时间:2014-03-17 15:40:46

标签: android json android-intent google-cloud-messaging

我正在使用GCM进行推送消息,但无法从收到的通知中提取数据。

这是我的代码:

$registatoin_ids = array($requestedGPSID1);
$message = array("message" => "yes");
$idResult=send_notification($registatoin_ids,$message);                     

function send_notification($registatoin_ids, $message) {
    // include config


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

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

    $headers = array(
        'Authorization: key=' . GOOGLE_API_KEY,
        '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;
 }

在      onHandleIntent(意图意图)

我在做

 Bundle extra = intent.getExtras();

并接收

Bundle [{message = yes,android ...}]

 public void onReceive(Context context, Intent intent) {
    // Explicitly specify that GcmIntentService will handle the intent.
    ComponentName comp = new ComponentName(context.getPackageName(),
            GcmIntentService.class.getName());
    // Start the service, keeping the device awake while it is launching.
    startWakefulService(context, (intent.setComponent(comp)));
    setResultCode(Activity.RESULT_OK);
  }

在gcm广播接收器中 我想在字符串中获取该消息

我该怎么做?

...谢谢

1 个答案:

答案 0 :(得分:0)

您的信息包含在意图中。试试这个:

String msg = intent.getStringExtra("message");
// msg should be "yes"