Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set click listener for notification?

I am using the following code to launch a notification when a Service is started Via AlarmManager:

nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); CharSequence from = "App"; CharSequence message = "Getting Latest Info..."; PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(), 0); Notification notif = new Notification(R.drawable.icon,     "Getting Latest Info...", System.currentTimeMillis()); notif.setLatestEventInfo(this, from, message, contentIntent); nm.notify(1, notif); 

How do I set an intent for this item so that when the user clicks on it, it would launch a certain activity?

like image 987
yoshi24 Avatar asked Aug 25 '11 03:08

yoshi24


People also ask

How to set click listener on Notification Android?

you have to change the intent flag as android:launchMode="singleTask" in the manifest and override onNewIntent(Intent intent) inside the activity. this will enable you to receive the parameters.

How do I use notification listener?

You need to grant access to your app to read notifications: "Settings > Security > Notification access" and check your app. Show activity on this post.

How do you handle opening activity using Notifications?

Build and issue the notification: Create an Intent that starts the Activity . Set the Activity to start in a new, empty task by calling setFlags() with the flags FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TASK . Create a PendingIntent by calling getActivity() .

What is a notification listener service?

A notification listener service allows the Google App to intercept notifications posted by other applications. Notification Options in the Google App Notification Listener Services. Within the Android Manifest file is the inclusion of the new Notification Listener Service.


1 Answers

As for yoshi24's comment, you may be able to set extras like this.

final Intent intent = new Intent(this, MyActivity.class); intent.setData(data); intent.putExtra("key", "value"); final PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0); 

You need to be aware of this as well before going for pending intents

https://stackoverflow.com/questions/1198558/how-to-send-parameters-from-a-notification-click-to-an-activity

UPDATE some thing like this will work for you

int your mainfest

<activity android:name=".MyActivity" android:launchMode="singleTop" ... /> 

in your activity

@Override protected void onCreate(Bundle savedInstanceState) {     processIntent(getIntent()); }  @Override protected void onNewIntent(Intent intent) {          processIntent(intent); };  private void processIntent(Intent intent){     //get your extras } 
like image 57
Samuel Avatar answered Sep 30 '22 16:09

Samuel