0

I need to change a variable only when a user clicks on a notification. I tried to use SharedPreferences in the activity after the notification redirects to it and then retake this variable next time I need it, but I don't know why it doesn't work.

Does anyone know where exactly does the onClick of the Notification is triggered ?

My notification is defined as follows:

protected Notification createNotification() {

    CharSequence title = "New notification";
    CharSequence content = "";

    Notification notification = new Notification(R.drawable.app_icon,
            title, System.currentTimeMillis());

    notification.flags = Notification.FLAG_AUTO_CANCEL;

    PendingIntent contentIntent = createPendingIntent();
    notification.setLatestEventInfo(this, title, content, contentIntent);
    return notification;
}

protected PendingIntent createPendingIntent() {
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                intent, 0);
        return contentIntent;
    }

protected void showNotification() {
    try {
        mNM.notify(NOTIFICATION_ID, createNotification());
    } catch (Exception e) {

    }
}

Thank you.

Phantom
  • 968
  • 3
  • 14
  • 32

1 Answers1

0

If you would use a PendingIntent when clicking on the notification you can make it open an activity, when that activity is opened via an intent you can make the activity call a method. If you first would add the attribute below to your activity-tag in the AndroidManifest.xml.

android:launchMode="singleTop"

Add this method to your activity. This method is triggered when you are calling a intent to the activity. This is where

@Override
protected void onNewIntent(Intent intent) {
      super.onNewIntent(intent);
}

An alternative solution would be to create a custom notification with your own layout and use onClickListeners on the elements.

timbillstrom
  • 1,176
  • 16
  • 33
  • I'll do few more tries with my current configuration, because it seems onNewIntent doesn't work either. Then I'll probably opt for you alternative solution. – Phantom Feb 17 '15 at 09:10
  • @Phantom Check this out, it might help http://stackoverflow.com/questions/16168553/create-custom-notification-android – timbillstrom Feb 17 '15 at 09:14
  • Thanks for the support. I managed to resolved the problem. I got rid of the confirmation boolean variable when user was clicking the notification and changed the variable I was interested in directly. – Phantom Feb 17 '15 at 10:02