yodann
yodann

Reputation: 189

click on notification not opening the activity

what is wrong with my code below? I expect the MainActivity is opened up after notification is touched/clicked. my code is below:

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());
}

Please can anyone help me on this.

Upvotes: 0

Views: 696

Answers (2)

Milad Faridnia
Milad Faridnia

Reputation: 9477

try this:

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());

you can change the flags.

hope it helps. i have changed the request id.

Upvotes: 1

Meenaxi
Meenaxi

Reputation: 567

you should use unique id for each notification. have a look over here. How to create Multiple statusbar Notifications in android

Upvotes: 0

Related Questions