【问题标题】:How to get gcm data from HTTP gcm server?如何从 HTTP gcm 服务器获取 gcm 数据?
【发布时间】:2014-04-22 21:58:56
【问题描述】:

我正在使用 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();

和接收

捆绑[{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 广播接收器中 我想在字符串中获取该消息

我该怎么做?

谢谢...

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-30
      • 2012-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-26
      相关资源
      最近更新 更多