0

我一直在使用Google Cloud Messaging API開發Android Messenger。我一直在遵循在http://developer.android.com/google/gcm/index.html上給出的教程。我能夠在正確的設備上成功接收通知。但是我無法更改通知的內容。如何修改Google Cloud Messaging中的通知接收方?

我已經做了一個消息類,這是我實際發送。該消息類包含消息的基本細節。我添加了sender_namemessage_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方法的最終體看起來像以下

字符串體傳遞給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()); 
} 

i之後上發送按鈕是有關於MainActivity點擊我可以接收通知,但所有額外的屬性都丟失了。通知看起來像以下:

Snapshot of notification received

所有其他atributtes丟失。任何幫助將被大大承認。我已經浪費了很多時間,但仍然陷入同樣的​​問題。

的logcat的看起來像這樣(我張貼只是它的relevnat部分):

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.and[email protected]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() : co[email protected]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 co[email protected]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}] 

回答

0

extras.toString()

您試圖顯示一個東西。這就是爲什麼你看到一個特殊的代碼,誰是你的對象的字符串版本。這不是內容。

首先,您必須獲取內容,然後才能顯示它。 我不知道額外的類型內容,可能是一個字符串或數組? 嘗試在顯示之前使用extras.getXXX(「XXX」)。

相關問題