2

我正在尝试在我的应用程序中实现 Google Cloud Messaging。我仍然无法弄清楚为什么我的手机没有收到正确的消息。我的服务器发送一条消息,GCM 服务器对此作出响应并将一条消息发送回我的手机。这条消息如下所示

{\"multicast_id\":8186678237008516542,\"success\":1,\"failure\":0,\"canonical_ids\":0,\"results\":[{\"message_id\":\"0:1356727074650189%12aaaeccf9fd7ecd\"}]}"

我认为这意味着我收到一条消息,问题是我的应用程序只显示空值。我现在正在使用 Browser Api 密钥并获得这些结果,但我尝试使用服务器密钥(理论上更适合我的需要),但我收到错误 401。

为了接收消息,我使用广播接收器

  public void onReceive(Context context, Intent intent){

String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);}

EXTRA_MESSAGE = 消息

这是我在服务器中使用的代码。

$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));

有谁知道可能是什么问题??

4

2 回答 2

1

我认为您的响应字符串名称不匹配,因此请检查您的响应字符串名称。我在我的服务器端代码和我的 android 端代码中使用它作为“价格”。你可以在下面的图片中看到它。

服务器端文件:send_message.php

在此处输入图像描述

应用程序端文件:GCMNotificationIntentService

public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;

public GCMNotificationIntentService() {
    super("GcmIntentService");
}

public static final String TAG = "GCMNotificationIntentService";

@Override
protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

    String messageType = gcm.getMessageType(intent);

    if (!extras.isEmpty()) {
        if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
                .equals(messageType)) {
            sendNotification("Send error: " + extras.toString());
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
                .equals(messageType)) {
            sendNotification("Deleted messages on server: "
                    + extras.toString());
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
                .equals(messageType)) {

            for (int i = 0; i < 3; i++) {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                }

            }
            sendNotification("Message Received from Google GCM Server: "
                    + extras.get("price"));
        }
    }
    GcmBroadcastReceiver.completeWakefulIntent(intent);
}

private void sendNotification(String msg) {
    //Log.d(TAG, "Preparing to send notification...: " + msg);
    mNotificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, MainActivity.class), 0);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.gcm_cloud)
            .setContentTitle("GCM Notification")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setContentText(msg);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    //Log.d(TAG, "Notification sent successfully.");
}
于 2016-01-23T13:37:33.000 回答
0

调整你的代码

'data' => $message,

变成这样:

'&data.message=' => $message,

并且您在 GCMIntentService 中的 onMessage() 方法应该如下所示:

protected void onMessage(Context ctx, Intent intent) {
        // TODO Auto-generated method stub
        String message =intent.getStringExtra("message");;

    } 
于 2012-12-28T21:32:33.577 回答