2014-12-02 247 views
0

我的代碼在下面有什麼問題?我希望MainActivity在觸摸/點擊通知後打開。我的代碼如下:點擊通知沒有打開活動

private void sendNotification(Quote quote) { 
    mNotificationManager = (NotificationManager) 
      this.getSystemService(Context.NOTIFICATION_SERVICE); 

    String message = quote.getQuote() + " - " + quote.getAuthor(); 

    // Creates an Intent for the Activity  
    Intent notifyIntent = new Intent(this, MainActivity.class); 
    notifyIntent.putExtra(Intent.EXTRA_SUBJECT, DailyQuotes.NOTIFICATION_QOD_MODE); 
    notifyIntent.putExtra(Intent.EXTRA_TEXT, quote.getQuoteID()); 
    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 


    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
       notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

    NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(this) 
    .setSmallIcon(R.drawable.ic_launcher) 
    .setContentTitle(getString(R.string.qod_title)) 
    .setStyle(new NotificationCompat.BigTextStyle() 
    .bigText(message)) 
    .setContentText(message); 

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

請任何人都可以幫助我解決這個問題。

+0

試試這個'PendingIntent contentIntent = PendingIntent.getActivity(this,0,notifyIntent,0);' – 2014-12-02 11:58:04

回答

0

試試這個:

mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
mBuilder = new NotificationCompat.Builder(this); 
int requestID = (int) System.currentTimeMillis(); 
Intent notificationIntent = new Intent(this, 
     MainActivity.class); 
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK 
     | Intent.FLAG_ACTIVITY_CLEAR_TOP); 
PendingIntent contentIntent = PendingIntent.getActivity(
     this, requestID, notificationIntent, 
     PendingIntent.FLAG_UPDATE_CURRENT); 
mBuilder.setContentText("This is test"); 
mBuilder.setContentIntent(contentIntent); 
mBuilder.setContentTitle("Title").setSmallIcon(
     R.drawable.ic_launcher); 
mNotifyManager.notify(requestID, mBuilder.build()); 

你可以改變的標誌。

希望它有幫助。我已經改變了請求ID。

+0

我試過了,它不起作用。還是一樣。我想我的代碼和你的代碼沒有太大的區別。 – yodann 2014-12-02 13:29:09

+0

我想你確實是對的。但我需要在清單文件上添加更多。我爲MainActivity添加了android:launchMode =「singleTop」。我不知道爲什麼,但事情似乎在事後工作。 – yodann 2014-12-02 14:32:58

相關問題