1

I have an app that has a notification generator. I'm able to receive and generate multiple notifications. my problem now is how can I know which notification that was clicked by user? each notification might act differently, and there might be at most 5 notifications at the same time in my app. I've searched online and found nothing about onClick event with notification....

I found thatonNewIntent can help, but it is not stable...since the activity might get killed by system before user clicks the notification. can someone please help??

thanks!!

user1865027
  • 3,505
  • 6
  • 33
  • 71
  • http://stackoverflow.com/questions/10634837/how-to-listen-onclick-event-for-notififation-bar-on-android – type-a1pha Jul 23 '13 at 01:26
  • I saw that thread also. that solution didn't work, and in fact, the answer below says that there's no onclick event. That's also what I've found out. Looking for a work around or the new `notification.build` would do the work? – user1865027 Jul 23 '13 at 18:57
  • You can attach data and wich activity to go to an intent and set this intent to a notification. It is enough information to know which one was clicked. – AlexBcn Jul 24 '13 at 09:21
  • but `onNewIntent` is called only if the user last exit the app by pressing home button. I tried to exit the app by pressing back button, and `onNewIntent` wasn't called. – user1865027 Jul 24 '13 at 18:17
  • according to [here](http://stackoverflow.com/questions/16618475/android-onnewintent-notification-is-not-acting-correctly), something in `onNewIntent` needs to be taken care in `onCreate` too, but where can I get data from intent in `onCreate`?? – user1865027 Jul 24 '13 at 18:19

1 Answers1

1

I suggest you make add data to the intent you add to the notification's intent.

For example.

intent.putExtra("Notification_id", id) //You can make this id the system time or some unique identifier.

(Or you can add this to your bundle of extras instead of just putting the extra.)

Then when you go to open the intent you can check the id.

id = intent.getExtra("Notification_id")

Hope this helps. Let me know if you have any questions or need more details.

UPDATE: You have to add data for in the intent and read it later both in your onNewIntent() and onResume(). You then remove the extra from the intent after you did whatever it is you need to do. Also adding some kind of unique data like System.currentTimeMillis() then it will prevent the intent from being reused.

RCB
  • 560
  • 4
  • 9
  • Can u please describe for this problem? Actually I can't understand what u actually saying to do here? – Maddy Sharma Nov 06 '13 at 12:24
  • I edited my comment a little for clarity. You pretty much put something in the intent then read it later in the app. – RCB Mar 12 '14 at 16:19