2013-12-23 62 views
1

在我的應用程序中,GCM消息最初顯示在通知欄中。單擊該通知時,它會顯示通過GCM接收到的消息的應用程序活動。如果我正在閱讀GCM消息在我的應用程序活動中,並且我收到另一個GCM消息,它將首先顯示在通知區域中。如果我單擊新通知,它應該啓動應用程序的預期活動,並將數據傳遞給它並銷燬它自身。這種行爲發生的事情是:如果我已經閱讀GCM消息並單擊我剛剛收到的新通知,它就會向我顯示已經運行的舊消息,並且不會破壞通知。將多個GCM消息處理到單個設備

我處理通知的代碼是:

private void sendNotification(String msg) { 
     SharedPreferences prefs = getSharedPreferences(
       DataAccessServer.PREFS_NAME, MODE_PRIVATE); 
     mNotificationManager = (NotificationManager) this 
       .getSystemService(Context.NOTIFICATION_SERVICE); 
     Intent intent = new Intent(this, WarningDetails.class); 
     Bundle bundle = new Bundle(); 
     bundle.putString("warning", msg); 
     bundle.putInt("warningId", NOTIFICATION_ID); 
     intent.putExtras(bundle); 
     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
       intent, PendingIntent.FLAG_UPDATE_CURRENT); 

     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
       this).setSmallIcon(R.drawable.weather_alert_notification) 
       .setContentTitle("Weather Notification") 
       .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)) 
       .setContentText(msg); 
     String selectedSound = prefs.getString("selectedSound", ""); 
     if (!selectedSound.equals("")) { 
      Uri alarmSound = Uri.parse(selectedSound); 
      mBuilder.setSound(alarmSound); 

     } else { 
      Uri alarmSound = RingtoneManager 
        .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
      mBuilder.setSound(alarmSound); 
     } 

     if (prefs.getBoolean("isVibrateOn", false)) { 
      long[] pattern = { 500, 500, 500, 500, 500, 500, 500, 500, 500 }; 
      mBuilder.setVibrate(pattern); 
     } 

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

WarningDetails活動代碼:

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.warning_details); 
     warning = (TextView) findViewById(R.id.warningDetails); 
     Bundle extras = getIntent().getExtras(); 
     if (extras != null) { 
      String warningData = extras.getString("warning"); 
      //if (extras.getInt("warningId") != 0) { 
       NotificationManager mNotificationManager = (NotificationManager) this 
         .getSystemService(Context.NOTIFICATION_SERVICE); 
       mNotificationManager.cancel(1); 
      //} 

      if (warningData != null) { 
       warning.setText(warningData); 
      } else { 
       // Do nothing 
      } 

     } else { 
      // Do nothing 
     } 
    } 

我需要顯示新通知的數據並銷燬通知also.Please幫助

回答

0

要在通知被點擊時取消(銷燬)通知,您應該將.setAutoCancel(true)添加到NotificationCompat.Builder

要使用新通知中的數據更新您的活動,您必須將邏輯從onCreate移動到onResume,因爲只有在創建活動時纔會調用創建,並且在點擊新通知時活動已存在,則使用現有的活動。

相關問題