0

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.

yodann
  • 179
  • 1
  • 11
  • try this `PendingIntent contentIntent = PendingIntent.getActivity(this, 0,notifyIntent, 0);` – M D Dec 02 '14 at 11:58

2 Answers2

1

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.

Milad Faridnia
  • 9,113
  • 13
  • 65
  • 78
  • i just tried and it doesn't work. still the same. i guess there's not much different between my code and yours. – yodann Dec 02 '14 at 13:29
  • i think u are actually right. but i need to add extra more on manifest file. i added android:launchMode="singleTop" for MainActivity. i don't know why, but it seems working afterwards. – yodann Dec 02 '14 at 14:32
0

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

Community
  • 1
  • 1
Meenaxi
  • 567
  • 5
  • 17
  • i don't think it's about unique id. i haven't able to manage to get the activity called properly. – yodann Dec 02 '14 at 13:29