【问题标题】:How to modify the notification receiver in Google Cloud Messaging?如何修改 Google Cloud Messaging 中的通知接收者?
【发布时间】:2014-08-29 09:39:52
【问题描述】:

我一直在使用 Google Cloud Messaging API 开发 Android Messenger。我一直在关注http://developer.android.com/google/gcm/index.html 上给出的教程。我能够在正确的设备上成功接收通知。但我无法更改通知的内容。

我创建了一个 Message 类,这就是我实际发送的内容。此消息类包括消息的基本详细信息。我添加了 sender_name 和 message_body 作为 GCM 示例中没有的附加字段。

Message.java

private final String collapseKey;
private final Boolean delayWhileIdle;
private final Integer timeToLive;
private final Map<String, String> data;
private final Boolean dryRun;
private final String restrictedPackageName;
private final String sender_name;
private final String message_body;
private final String TAG = "Akshay Messenger";

我无法发布整个代码,因为它太长了。该类中的各个字段也实现了get和set方法。

随便举个例子,传递给Http post方法的最终body如下所示

字符串体传递给 Http post 方法

  registration_id=this_is_the_registration_id&delay_while_idle=0&collapse_key=Akshay&sender_name=sfvshh&messaage_body=dhbvsh&time_to_live=108

用于发送post请求的函数是

Sender.java

protected HttpURLConnection post(String url, String contentType, String body) throws IOException
{
    Log.i(TAG, "Sender Class Making a HTTP Post request to a given url using a content .");

    if (url == null || body == null)
    {
        Log.i(TAG, "Sender Class URL is null or body is null.");

        throw new IllegalArgumentException("arguments cannot be null");
    }

    if (!url.startsWith("https://"))
    {
        Log.i(TAG, "Sender Class URL does not use https: " + url);
    }

    Log.i(TAG, "Sender Class Sending POST to " + url);
    Log.i(TAG, "Sender Class POST body: " + body);

    byte[] bytes = body.getBytes();
    HttpURLConnection conn = getConnection(url);

    conn.setDoOutput(true);
    conn.setUseCaches(false);
    conn.setFixedLengthStreamingMode(bytes.length);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", contentType);
    conn.setRequestProperty("Authorization", "key=" + key);

    OutputStream out = conn.getOutputStream();

    try
    {
        out.write(bytes);

        Log.i(TAG, "Sender Class bytes : " + new String(bytes));
    }
    finally
    {
        close(out);
    }

    Log.i(TAG, "Sender Class HTTP Post object : " + conn);
    Log.i(TAG, "Sender Class HTTP Post Object OutStream Object " + out.toString());

    return conn;
}

用于接收通知的类如下:

GCMIntentService.java

protected void onHandleIntent(Intent intent)
{
    Log.i(TAG, "GCM Intent Service Inside GCMIntentService class.");

    Bundle extras = intent.getExtras();

    Log.i(TAG, "GCM Intent Service extras.toString() : " + extras.toString());

    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

    String messageType = gcm.getMessageType(intent);

    if (!extras.isEmpty())
    {
        Log.i(TAG, "GCM Intent Service Message recieved.");

        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))
        {
            Log.i(TAG, "GCM Intent Service Message recieved in a correct format.");

            sendNotification("Received: " + extras.toString());

            Log.i(TAG, "GCM Intent Service Received: " + extras.toString());
        }

        else
        {
            Log.i(TAG, "GCM Intent Service Message Sent taking back the control.");
        }

    }
}

private void sendNotification(String msg)
{
    Log.i(TAG, "GCM Intent Service Notificaton Manager called.");

    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.ic_stat_gcm).setContentTitle(TAG).setStyle(new NotificationCompat.BigTextStyle().bigText(msg)).setContentText(msg);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

点击 MainActivity 上的发送按钮后,我可以收到通知,但缺少所有额外属性。通知如下所示:

所有其他属性都丢失了。任何帮助将不胜感激。我已经在这方面浪费了很多时间,但仍然停留在同一个问题上。

Logcat 看起来像这样(我只发布了它的相关部分):

Logcat

08-29 14:43:47.237: I/Akshay Messenger(2909): Main Activity Main Activity Inside Main Activity Application started.
08-29 14:43:47.247: I/Akshay Messenger(2909): Main Activity Checking for Google Play Services.
08-29 14:43:47.297: I/Akshay Messenger(2909): Main Activity Google Services SDK found.
08-29 14:43:47.297: I/Akshay Messenger(2909): Main Activity Getting the registration ID for the device.
08-29 14:43:47.297: I/Akshay Messenger(2909): Main Activity Getting the GCM Preferences Context Variables.
08-29 14:43:47.297: I/Akshay Messenger(2909): Main Activity Getting the App Version.
08-29 14:43:47.307: I/Akshay Messenger(2909): Main Activity App version = 1
08-29 14:43:47.307: I/Akshay Messenger(2909): Main Activity Registeration ID found, Registeration ID = THIS_IS_MY_REGISTRATION_ID
08-29 14:43:47.307: I/Akshay Messenger(2909): Main Activity Resuming the activity.
08-29 14:43:47.307: I/Akshay Messenger(2909): Main Activity Checking for Google Play Services.
08-29 14:43:47.307: I/Akshay Messenger(2909): Main Activity Google Services SDK found.
08-29 14:43:47.307: I/Akshay Messenger(2909): Main Activity Getting the registration ID for the device.
08-29 14:43:47.307: I/Akshay Messenger(2909): Main Activity Getting the GCM Preferences Context Variables.
08-29 14:43:47.307: I/Akshay Messenger(2909): Main Activity Getting the App Version.
08-29 14:43:47.307: I/Akshay Messenger(2909): Main Activity App version = 1
08-29 14:43:47.307: I/Akshay Messenger(2909): Main Activity Registeration ID found, Registeration ID = THIS_IS_MY_REGISTRATION_ID
08-29 14:43:58.218: I/Akshay Messenger(2909): Main Activity Send Button Pressed.
08-29 14:43:58.258: I/Akshay Messenger(2909): message_content : ghjkl
08-29 14:43:58.298: I/Akshay Messenger(2909): Main Activity Sending the registration ID to Backend Server.
08-29 14:43:58.298: I/Akshay Messenger(2909): Main Activity sender_name : asdf
08-29 14:43:58.298: I/Akshay Messenger(2909): Main Activity message_body : ghjkl
08-29 14:43:58.328: I/Akshay Messenger(2909): Sender Class API Key Recieved : THIS_IS_THE_API_KEY
08-29 14:43:58.328: I/Akshay Messenger(2909): Sender Class sendNoRetry function called.
08-29 14:43:58.328: I/Akshay Messenger(2909): Message Converting message object to string.
08-29 14:43:58.328: I/Akshay Messenger(2909): Message CollapseKey not null
08-29 14:43:58.328: I/Akshay Messenger(2909): Message timeToLive not null
08-29 14:43:58.328: I/Akshay Messenger(2909): Message delayWhileIdle not null
08-29 14:43:58.328: I/Akshay Messenger(2909): Message sender_name not null
08-29 14:43:58.328: I/Akshay Messenger(2909): Message message_body not null
08-29 14:43:58.328: I/Akshay Messenger(2909): Message final builder.toString() : Message(collapseKey=Akshay, timeToLive=108, delayWhileIdle=false, sender_name=asdf, message_body=ghjkl)
08-29 14:43:58.328: I/Akshay Messenger(2909): Sender Class Message message : Message(collapseKey=Akshay, timeToLive=108, delayWhileIdle=false, sender_name=asdf, message_body=ghjkl)
08-29 14:43:58.328: I/Akshay Messenger(2909): Sender Class String registrationId :  THIS_IS_MY_REGISTRATION_ID
08-29 14:43:58.328: I/Akshay Messenger(2909): Sender Class delayWhileIdle bit is not null passing to body.
08-29 14:43:58.328: I/Akshay Messenger(2909): Sender Class collapseKey bit not null passing to body.
08-29 14:43:58.328: I/Akshay Messenger(2909): Sender Class sender_name is not null passing to body.
08-29 14:43:58.328: I/Akshay Messenger(2909): Sender Class message_body is not null passing to body.
08-29 14:43:58.328: I/Akshay Messenger(2909): Sender Class timeToLive bit not null passing to body.
08-29 14:43:58.328: I/Akshay Messenger(2909): Sender HTTP requestBody : registration_id=THIS_IS_MY_REGISTRATION_ID&delay_while_idle=0&collapse_key=Akshay&sender_name=asdf&messaage_body=ghjkl&time_to_live=108
08-29 14:43:58.328: I/Akshay Messenger(2909): Sender Class Making the Http Post request.
08-29 14:43:58.328: I/Akshay Messenger(2909): Sender Class making a HTTP Post request to a given URL.
08-29 14:43:58.328: I/Akshay Messenger(2909): Sender Class Making a HTTP Post request to a given url using a content .
08-29 14:43:58.328: I/Akshay Messenger(2909): Sender Class Sending POST to https://android.googleapis.com/gcm/send
08-29 14:43:58.328: I/Akshay Messenger(2909): Sender Class POST body: registration_id=THIS_IS_MY_REGISTRATION_ID&delay_while_idle=0&collapse_key=Akshay&sender_name=asdf&messaage_body=ghjkl&time_to_live=108
08-29 14:44:05.535: I/Akshay Messenger(2909): Sender Class bytes : registration_id=THIS_IS_MY_REGISTRATION_ID&delay_while_idle=0&collapse_key=Akshay&sender_name=asdf&messaage_body=ghjkl&time_to_live=108
08-29 14:44:05.535: I/Akshay Messenger(2909): Sender Class HTTP Post object : com.android.okhttp.internal.http.HttpsURLConnectionImpl$HttpUrlConnectionDelegate:https://android.googleapis.com/gcm/send
08-29 14:44:05.535: I/Akshay Messenger(2909): Sender Class HTTP Post Object OutStream Object com.android.okhttp.internal.http.HttpTransport$FixedLengthOutputStream@44944cc8
08-29 14:44:05.535: I/Akshay Messenger(2909): Sender Class Post request completed.
08-29 14:44:06.215: I/Akshay Messenger(2909): conn.getInputStream() : com.android.okhttp.internal.http.HttpTransport$ChunkedInputStream@44949468
08-29 14:44:06.215: I/Akshay Messenger(2909): Sender Class Status code : 200
08-29 14:44:06.215: I/Akshay Messenger(2909): Sender Class Successfull message status return code.
08-29 14:44:06.215: I/Akshay Messenger(2909): Sender Class inside getAndClose function.
08-29 14:44:06.215: I/Akshay Messenger(2909): Sender class getAndClose stream com.android.okhttp.internal.http.HttpTransport$ChunkedInputStream@44949468
08-29 14:44:06.225: I/Akshay Messenger(2909): Sender Class inside getString function.
08-29 14:44:06.225: I/Akshay Messenger(2909): Sender Class responseBody : id=0:1409303399939497%ca7e668b7504ac97
08-29 14:44:06.225: I/Akshay Messenger(2909): lines.length : 1
08-29 14:44:06.225: I/Akshay Messenger(2909): Sender Class TOKEN_MESSAGE_ID match found.
08-29 14:44:06.225: I/Akshay Messenger(2909): Sender Class Checking for messageId = 0:1409303399939497%ca7e668b7504ac97 in Result Class.
08-29 14:44:06.225: I/Akshay Messenger(2909): Inside Result.Builder.build()
08-29 14:44:06.225: I/Akshay Messenger(2909): Sender Class result = [ messageId=0:1409303399939497%ca7e668b7504ac97 ]
08-29 14:44:06.225: I/Akshay Messenger(2909): Sender Class Message created successfully.
08-29 14:44:06.225: I/Akshay Messenger(2909): Main Activity Sender function called.
08-29 14:44:06.225: I/Akshay Messenger(2909): Main Activity message sending status : Sent message msgID : 1 Message : ghjkl
08-29 14:44:06.446: I/Akshay Messenger(2909): GCM Intent Service Inside GCMIntentService class.
08-29 14:44:06.446: I/Akshay Messenger(2909): GCM Intent Service extras.toString() : Bundle[mParcelledData.dataSize=256]
08-29 14:44:06.446: I/Akshay Messenger(2909): GCM Intent Service Message recieved.
08-29 14:44:06.446: I/Akshay Messenger(2909): GCM Intent Service Message Sent taking back the control.
08-29 14:44:06.516: I/Akshay Messenger(2909): GCM Intent Service Inside GCMIntentService class.
08-29 14:44:06.516: I/Akshay Messenger(2909): GCM Intent Service extras.toString() : Bundle[mParcelledData.dataSize=208]
08-29 14:44:06.516: I/Akshay Messenger(2909): GCM Intent Service Message recieved.
08-29 14:44:06.516: I/Akshay Messenger(2909): GCM Intent Service Message recieved in a correct format.
08-29 14:44:06.516: I/Akshay Messenger(2909): GCM Intent Service Notificaton Manager called.
08-29 14:44:06.516: I/Akshay Messenger(2909): GCM Intent Service Received: Bundle[{from=678478691968, android.support.content.wakelockid=2, collapse_key=Akshay}]

【问题讨论】:

    标签: java android android-intent android-notifications google-cloud-messaging


    【解决方案1】:

    extras.toString()

    您正在尝试显示一个对象。这就是为什么你会看到一个特殊的代码,它是你的对象的字符串版本。那不是内容。

    首先,您必须获取内容,然后才能显示它。 我不知道额外的类型内容,可能是字符串还是数组? 在显示之前尝试使用 extras.getXXX("XXX")。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多