点击通知不打开活动

时间:2014-12-02 11:56:28

标签: android notifications intentservice

我的代码下面有什么问题?我希望在触摸/点击通知后打开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());
}

请允许任何人帮助我。

2 个答案:

答案 0 :(得分: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。

答案 1 :(得分:0)

您应该为每个通知使用唯一ID。 看看这里。 How to create Multiple statusbar Notifications in android