2

我需要使用通知點擊事件,我有通知方法,但這種方法不打開我的活動。我怎樣才能打開活動時通知點擊

我的代碼:

private void sendNotification(String msg) { 

     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) 
     .setContentTitle("EXX") 
     .setSmallIcon(R.drawable.ic_launcher) 
     .setStyle(new NotificationCompat.BigTextStyle() 
     .bigText(msg)) 
     .setContentText(msg)   
     .setOngoing(true);   
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 
}  

這是可能的,

感謝。

+3

的可能的複製13716723 /點擊通知後打開應用程序) –

+0

不重複。 OP只是缺少'setContentIntent()' –

回答

3

是的,這是可能的。

改變你的方法,就像那樣;

private void sendNotification(String msg) { 
    Intent intent = new Intent(this, MainActivity.class); 
    intent.putExtra("yourpackage.notifyId", NOTIFICATION_ID); 
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 
      PendingIntent.FLAG_UPDATE_CURRENT); 
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) 
      .setContentTitle("EXX") 
      .setSmallIcon(R.drawable.ic_launcher) 
      .setStyle(new NotificationCompat.BigTextStyle() 
      .bigText(msg)) 
      .addAction(getNotificationIcon(), "Action Button", pIntent) 
      .setContentIntent(pIntent) 
      .setContentText(msg) 
      .setOngoing(true); 
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 
} 

,並添加您mainactivity

NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

這段代碼工作。

+0

哦謝謝!這段代碼對我有用!我愛你bro <3 –

+0

不客氣。 –

4

嘿@約翰這很簡單

你只需要做

Intent intent = new Intent(this, ResultActivity.class); 

... //因爲單擊通知打開一個新的( 「特殊」)的活動,有 //不需要創建一個人工後退棧。

PendingIntent pendingIntent = TaskStackBuilder.create(getApp()) 
      .addNextIntent(intent) 
      .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 

而且在mBuilder

private void sendNotification(String msg) { 
     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) 
     .setContentTitle("EXX") 
     .setSmallIcon(R.drawable.ic_launcher) 
     .setStyle(new NotificationCompat.BigTextStyle() 
     .bigText(msg)) 
     .setContentText(msg)   
     .setOngoing(true);  
     .setContentIntent(pendingIntent) 
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 
} 

組待決的意圖,你(http://stackoverflow.com/questions/ [點擊通知後,打開應用程序]做:)

相關問題