2014-04-01 88 views
0

我已經通過JSP從服務器端編寫了代碼,並且可以將消息發送到我的應用程序。 並且我的應用程序有通知。Android GCM無法從服務器獲取消息的內容

但是,內容丟失。 下面是服務器端代碼:

String message = "This is a test message."; 
String registrationId="xxxxxx"; 

Sender sender = new Sender("My API key"); 
Message gcmMessage = new Message.Builder().addData(registrationId,message).build(); 
Result result=sender.send(gcmMessage, registrationId, 5); 
out.println(result.toString()); 

而且下面是從哪裏接到消息,我的應用程序代碼的一部分:

protected void onMessage(Context context, Intent data) { 
    String message; 
    // Message from PHP server 
    message = data.getStringExtra("message"); 
    // Open a new activity called GCMMessageView 
    Intent intent = new Intent(this, GCMMessageView.class); 
    // Pass data to the new activity 
    //test 
    Log.v("sky", "MESSAGE RECEIVED : "+data.getExtras().toString()); 
    Log.v("sky","Message:"+message); 
    intent.putExtra("message", message); 
    // Starts the activity on notification click 
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 
      PendingIntent.FLAG_UPDATE_CURRENT); 
    // Create the notification with a notification builder 
    Notification notification = new Notification.Builder(this) 
      .setSmallIcon(R.drawable.ic_launcher) 
      .setWhen(System.currentTimeMillis()) 
      .setContentTitle("Android GCM Tutorial") 
      .setContentText(message).setContentIntent(pIntent) 
      .getNotification(); 
    // Remove the notification on click 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 

    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    manager.notify(R.string.app_name, notification); 

    { 
     // Wake Android Device when notification received 
     PowerManager pm = (PowerManager) context 
       .getSystemService(Context.POWER_SERVICE); 
     final PowerManager.WakeLock mWakelock = pm.newWakeLock(
       PowerManager.FULL_WAKE_LOCK 
         | PowerManager.ACQUIRE_CAUSES_WAKEUP, "GCM_PUSH"); 
     mWakelock.acquire(); 

     // Timer before putting Android Device to sleep mode. 
     Timer timer = new Timer(); 
     TimerTask task = new TimerTask() { 
      public void run() { 
       mWakelock.release(); 
      } 
     }; 
     timer.schedule(task, 5000); 
    } 

} 

在我data.getExtras().toString());一部分,我已經得到了我日誌中的文字,似乎我無法從結果中提取消息:

04-01 19:16:45.951: V/sky(21451): MESSAGE RECEIVED : Bundle[{xx My Register ID xxx=This is a test message., collapse_key=do_not_collapse, from=584034591331}] 

我可以知道如何獲取消息嗎?謝謝!

回答

0

這是你的錯誤:

Message gcmMessage = new Message.Builder().addData(registrationId,message).build(); 

是應該是:

Message gcmMessage = new Message.Builder().addData("message",message).build(); 

你在他的名字是你的註冊ID,這是沒有意義的參數發送郵件的內容。

相關問題