onclicklistener for notification [duplicate] - android

This question already has answers here:
How to set click listener for notification?
(3 answers)
Closed 9 years ago.
I had set a notification in my app. Its working fine. If i click the notification in statusbar it takes to my app.
Now i need to set some work if the notification is clicked where can i set this?
Is there any method that implicitly got invoked when a notification is clicked?
Also i want to remove that notification if it is clicked,how to do so?
this is my code
notifManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent inty=getIntent();
note = new Notification(R.drawable.icon, "New E-mail", System.currentTimeMillis());
PendingIntent intent = PendingIntent.getActivity(MainActivity.this, 0, inty, 0);
note.setLatestEventInfo(MainActivity.this, "New E-mail", "You have one unread message.", intent);
notifManager.notify(R.string.search_hint, note);

You could add some extra data to the intent and then in your activity look for it in the onCreate and onNewIntent methods.
For example:
inty.putExtra("came from notification", true);
You can then read that out via the intent passed to onNewIntent or in onCreate by using getIntent().
intent.getBooleanExtra("came from notification", false);

Try to call broadcastReceiver it may helpful for your requirement,
Intent notificationIntent = new Intent(this, dummy_activity.class);
notificationIntent.setAction("android.intent.action.MAIN");
notificationIntent.addCategory("android.intent.category.LAUNCHER");
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT |
Notification.FLAG_AUTO_CANCEL);
// Now, once this dummy activity starts send a broad cast to your parent activity and finish the pending activity
//remember you need to register your broadcast action here to receive.
BroadcastReceiver call_method = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action_name = intent.getAction();
if (action_name.equals("call_method")) {
// call your method here and do what ever you want.
}
}
};
registerReceiver(call_method, new IntentFilter("call_method"));

Related

Notification Button click - method call

I've created a Notification to be displayed on Status Bar.
In the notification I've added a Button, which performs some action.
To call a specific method, on button click I'm using the approach suggested here.
Code:
// Building notification - this code is present inside a Service
NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent activityIntent = new Intent(this, HomeActivity.class);
activityIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, activityIntent, 0);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this).setContentTitle(fileName.toString()).setContentText("Subject")
.setSmallIcon(R.drawable.ic_app_icon).setContentIntent(pendingIntent).setAutoCancel(false);
// code to add button
Intent openListIntent = new Intent();
openListIntent.putExtra("list_intent", true);
PendingIntent listPendingIntent = PendingIntent.getActivity(this, 0, openListIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.addAction(R.drawable.ic_open_list, "List", listPendingIntent);
Notification notification = notificationBuilder.build();
// Code in HomeActivity.java to perform operation on button click
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
boolean openListFlag = intent.getBooleanExtra("list_intent", false);
if (openListFlag) {
// code to perform operation
}
}
My approach is that I've passed an Extra boolean value, which is set to true, when button in notification is clicked.
But even if click on only the notification (and not on the button), still the action present in onNewIntent() is getting called.
That is, value for openListFlag is always setted as true.
How should I make sure, that operation is performed only on button click?
Just use intent action instead of extras, as suggested here:
Determine addAction click for Android notifications

Pending Intent from Broadcast Receiver to Activity - old data received

I found similar questions, but none of the solutions helped me.
I have a broadcast receiver intercepting sms and forwarding the sms text to another activity. The other activity should display the latest received text.
But the other activity is ALWAYS displaying the first text received after the app was started for the first time. New sms-texts are not displayed.
What I tried:
Played around with the parameters of Pending Intent (unique random int and all 4 update options) - didnt help so far.
PendingIntent contentIntent = PendingIntent.getActivity(context, **rand.nextInt(50) + 1**, intent, **FLAG_UPDATE_CURRENT**);
Here's the relevant code from the Broadcast Receiver, the notification which is also triggered shows the new and correct text each time.
private void showNotification(Context context) {
Intent intent = new Intent(context, DisplayMSG.class);
intent.putExtra(EXTRA_MESSAGE, str);
Random rand = new Random();
PendingIntent contentIntent = PendingIntent.getActivity(context, rand.nextInt(50) + 1, intent, FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("RPT Notification")
.setContentText(str + Integer.toString(n));
mBuilder.setContentIntent(contentIntent);
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
}
And here's the part from the receiving activity:
Intent intent = getIntent();
String message = intent.getStringExtra(SmsReceiver.EXTRA_MESSAGE);
TextView textView = (TextView) findViewById(R.id.tView);
textView.setText(message);
Let me know if you need more code. But I hope this should be sufficient, since it's basically working, it's just not transferring the new texts.
EDIT:
Thanks to CommonsWare here is the solution, I just added this method to the receiving activity:
#Override
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
TextView textView = (TextView) findViewById(R.id.tView);
textView.setText(intent.getStringExtra(SmsReceiver.EXTRA_MESSAGE));
}
And here's the part from the receiving activity
If the activity is already running, that is the wrong Intent. Override onNewIntent() and use the Intent that is passed in to it. getIntent() always returns the Intent used to create the activity, not any subsequent ones that brought an existing instance back to the foreground.

Implement setOnClickPendingIntent from notification

I build custom notification that contain button and i want to listin when user press on it.
The button should not open any activity but only logic staff like change song.
the Code:
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification);
contentView.setTextViewText(R.id.toptext, nowPlayingTitle);
//this not work
Intent intent = new Intent(this, receiver.class);
intent.putExtra("UNIQ", "1");
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, PendingIntent.FLAG_CANCEL_CURRENT)
contentView.setOnClickPendingIntent(R.id.imageButtonPlay,
pendingIntent);
notification.contentView = contentView;
// this is to return to my activity if click somwhere else in notification
Intent notificationIntent = new Intent(this, MYACTIVITY.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
mNotificationManager.notify(NOTIFICATION_ID, notification);
I don't get the hang of the setOnClickPendingIntent what need to be in the second param?
How can i just call a function after user press on the button?
im probably missing something cause i dont understand the receiver side and what happend after user press
You are missing the fact that the button you created actually doesn't belong to your application. It is created in another context, in another process. There is no way it can call your function.
Instead, when the user taps the button, that pending intent is fired. You can catch it by your receiver (in your activity), check some parameters and do the action.
Or you can implement a service and handle this intent in background. I'd prefer this way.
thanks for quick answer. I try using receiver but it never fired.
The code is in the main question and i created for the reciever class the following code:
public class receiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
}
}
but click on the notification never fire the receiver ( Test on debug mode )

Custom status bar notification

I got a custom status bar notification. There is a "Play" and "Next" image on it.
I want to send a Broadcast Intent to a Service when the "play" or "next" image is pressed.
notification = new Notification(R.drawable.ic_launcher, title + "\n"
+ text, System.currentTimeMillis());
contentIntent = PendingIntent.getActivity(context, 0, actionOnClick, 0);
notification.contentIntent = contentIntent;
contentView = new RemoteViews(context.getPackageName(),
R.layout.custom_player_notification);
contentView.setTextViewText(R.id.custom_player_notification_title,
title);
contentView.setTextViewText(R.id.custom_player_notification_text, text);
notification.contentView = contentView;
This is the code.
The ContentView offers the possibility to set "setOnClickPendingIntent" and "setOnClickFillInIntent". How can I use them to send this broadcast?
I want to send a broadcast because I dont want to launch an activity.
I want to send a Broadcast Intent to a Service when the "play" or "next" image is pressed
I would suggest that you want to send a command via startService() rather than a broadcast.
How can I use them to send this broadcast?
You can try calling setOnClickPendingIntent(), supplying a PendingIntent you create via the static getBroadcast() method on PendingIntent (for a broadcast) or getService() (for a command to be sent via startService()).
Bear in mind that none of this will work on most Android devices available at present. I think buttons-in-Notifications works starting on Android 4.0, perhaps 3.0, but definitely not before then.
You could put something similar to this in your onClick() method of the notification, directing it to a broadcast receiver.
Intent intent = new Intent(this, receiver.class);
intent.putExtra("SomeInfoMaybe", info);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, PendingIntent.FLAG_CANCEL_CURRENT);
And then the code for handling the next stage could go in here:
public class receiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
System.out.println("SomeInfoMaybe = " + bundle.getString("info"));
//etc...
}
}
calling .notify(); on your notification should help link to the broadcast receiver

Window already focused, ignoring focus gain of: com.android.internal.view

Unfortunately, I was unable to find a resolution to this issue in other StackOverflow posts so I apologize in advance for re-posting this question. Essentially, I have an AppWidget that creates a Notification. I want the user to click the Notification to launch an Activity. The AppWidget and Activity are in the same APK.
The following code is all in the AppWidget. Clicking on the Notification broadcasts a custom Intent which is received by the AppWidget.
The problem is that the Activity does not appear to launch, even though logcat demonstrates that the custom Intent has indeed been broadcast. The only clue is the logcat message reiterated in the post title.
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Constants.BROWSER_INTENT)) {
launchActivity(context, R.layout.appwidget, R.id.launch_button, Activity.class);
}
super.onReceive(context, intent);
}
private void displayNotification(Context context, String ticker, String title, String msg) {
Intent intent = new Intent(context, ThisWidget.class);
intent.setAction(Constants.BROWSER_INTENT);
PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon, ticker, System.currentTimeMillis());
notification.setLatestEventInfo(context, title, msg, pending);
notificationManager.notify(1, notification);
}
Some notes:
Pressing a button to execute launchActivity() works (it launches the
Activity)
launchActivity uses Intent.FLAG_ACTIVITY_NEW_TASK as
recommended by Android lifecycle documentation
Has anyone solved this issue when launching an Activity from a Notification press?
er, figures, I answered my own question just minutes after posting it. I guess I was over-complicating the implementation by using an Intent action. I changed these lines:
Intent intent = new Intent(context, ThisWidget.class);
intent.setAction(Constants.BROWSER_INTENT);
PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
to this:
Intent intent = new Intent(context, Activity.class);
PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
and it worked.
Sorry for the bogus post.

Categories

Resources