簡體   English   中英

點擊通知無法打開活動

[英]click on notification not opening the activity

我下面的代碼有什么問題? 我希望在觸摸/單擊通知后打開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());
}

請任何人可以幫助我。

嘗試這個:

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。

您應該為每個通知使用唯一的ID。 在這里看看。 如何在Android中創建多個狀態欄通知

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM