2015-04-07 58 views
-1

我收到來自服務器的通知,但我無法正確解析數據。如何解析從GCM收到的消息?

這是從extras.toString()獲取的內容;

捆綁[{爲messageType = 1,contentText =內容主體,從= 23105361257,contentTitle =內容標題,消息=測試消息,android.support.content.wakelockid = 1,collapse_key的= do_not_collapse,tickerText =股票文本消息}]

我想只打印通知中的包中的消息值。 以及如果我收到另一個通知..我想在Bigview風格的相同通知中顯示它。這怎麼可能?

例如。

測試消息

另一個測試消息

public class GCMNotificationIntentService extends IntentService { 
    public static final int notifyID = 9001; 
    public static final String TAG = "GCM Demo"; 

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

    @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 < 5; i++) { 
        Log.i(TAG, "Working... " + (i + 1) 
          + "/5 @ " + SystemClock.elapsedRealtime()); 
        try { 
         Thread.sleep(5000); 
        } catch (InterruptedException e) { 
        } 
       } 
       Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime()); 
       sendNotification(extras.toString()); 
       Log.i(TAG, "Received: " + extras.toString()); 
      } 
     } 
     GcmBroadcastReceiver.completeWakefulIntent(intent); 
    } 


    private void sendNotification(String msg) { 
     Intent resultIntent = new Intent(this, HomeActivity.class); 
     resultIntent.putExtra("message", msg); 
     PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, 
       resultIntent, PendingIntent.FLAG_ONE_SHOT); 

     NotificationCompat.Builder mNotifyBuilder; 
     NotificationManager mNotificationManager; 

     mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

     mNotifyBuilder = new NotificationCompat.Builder(this) 
       .setContentTitle("Notification") 
       .setContentText("New Notification") 
       .setSmallIcon(R.drawable.launcher_icons) 
       .setStyle(new NotificationCompat.BigTextStyle() 
       .bigText(msg)); 

     mNotifyBuilder.setContentIntent(resultPendingIntent); 

     int defaults = 0; 
     defaults = defaults | Notification.DEFAULT_LIGHTS; 
     defaults = defaults | Notification.DEFAULT_VIBRATE; 
     defaults = defaults | Notification.DEFAULT_SOUND; 

     mNotifyBuilder.setDefaults(defaults); 
     mNotifyBuilder.setContentText(msg); 
     mNotifyBuilder.setAutoCancel(true); 
     mNotificationManager.notify(notifyID, mNotifyBuilder.build()); 
    } 
} 
+0

'extras.getString(「message」,「not set!」)''? – Yazan

+0

如果'HomeActivity.class'獲得了什麼Intent? –

+0

@ PratikButani-AndroidButs:請幫我解決第二部分問題嗎?如何在大視野中顯示消息。例如。 WhatsApp的。如果我從同一用戶多次獲取消息,它顯示他們在通知..如何做到這一點?目前..通知被新的替換。 – Veer3383

回答

0

你要發送類似:

sendNotification(extras.getString("message"));

代替

sendNotification(extras.toString());

所以,你將在sendNotification()說法得到測試消息,然後你可以通過它在Intent

希望你能得到它。謝謝。