0

我从这里运行 Google GCM 测试代码,它在模拟器和手机上运行,​​但是当我发送消息时,通知仅适用于模拟器而不是手机。

GCMIntentService 通知

import android.support.v4.app.NotificationCompat;
...

// issues a notification to inform the user that server has sent a message
private void generateNotification(Context context, String message) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_launcher)
            .setTicker(context.getString(R.string.app_name)).setContentText(message);

    Intent notificationIntent = new Intent(context, Main.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(contentIntent);

    // add notification
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(FM_NOTIFICATION_ID, builder.build());
}

PHP 发送消息和发送函数

require_once 'push_db_functions.php';
$dbf = new push_db_functions();
$id = '40';
$message = "Test Message";

$response = $dbf->sendMessage($id, $message);
$response = json_decode($response);
print_r($response);

public function sendMessage($id, $message) {
    $results = mysql_query("SELECT regid FROM test WHERE id = '$id'");
    $processed = mysql_fetch_row($results);
    $url = 'https://android.googleapis.com/gcm/send';
    $apiKey = "AIzaSyD-xxx";
    $fields = array('registration_ids' => $processed, 'data' => array( "message" => $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);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
    // execute post
    $response = curl_exec($ch); 
    curl_close($ch);
    return $response;
}

发送消息响应

stdClass Object ( [multicast_id] => 5036849453049739126 [success] => 1 [failure] => 0 [canonical_ids] => 0 [results] => Array ( [0] => stdClass Object ( [message_id] => 0:1353799902066759%04108f12f9fd7ecd ) ) ) success

我的数据库中有 2 条 regId 记录,一条用于模拟器,一条用于手机 $id 39 和 40,并相应地更改它们以向任一设备发送消息。

数据库记录

id | regid
39 | APA91bFyMJx4b0ddI........ Emulator
40 | APA91bFhwEhHOClkg........ Phone

手机正在运行 Gingerbread,程序在其上注册并正常运行,除了在收到消息时不显示通知。

    mDisplay = (TextView) findViewById(R.id.display);
    registerReceiver(mHandleMessageReceiver, new IntentFilter(DISPLAY_MESSAGE_ACTION));
    final String regId = GCMRegistrar.getRegistrationId(this);
    Log.d("registrationID", "::" + regId);
    if (regId.equals("")) {
        // automatically registers application on startup.
        GCMRegistrar.register(this, SENDER_ID);
    }

任何帮助将不胜感激。

谢谢

4

1 回答 1

0

感谢所有回答这个问题的人。

我已经解决了这个问题:

在我的手机上帐户和同步,常规同步设置下,我发现后台数据同步框没有被勾选。设置后一切正常。

我应该在问这个问题之前弄清楚这一点,我很抱歉。

但是我希望这个答案可以对其他人有所帮助。

于 2012-11-25T04:49:21.593 回答