Notification - click fires wrong PendingIntent - android

I have a notification with following content intent:
clickIntent = PendingIntent.getService(
getApplicationContext(),
Constants.REQUEST_CODE_NOTIFICATION_OTHER,
new Intent(getApplicationContext(), MainService.class)
.putExtra(Constants.EVENT, Constants.EVENT_TOGGLE_BLACK),
PendingIntent.FLAG_UPDATE_CURRENT);
and one action with following action intent:
PendingIntent closeIntent = PendingIntent.getService(
getApplicationContext(),
Constants.REQUEST_CODE_NOTIFICATION_OTHER,
new Intent(getApplicationContext(), MainService.class)
.putExtra(Constants.EVENT, Constants.EVENT_DISABLE_SERVICE),
PendingIntent.FLAG_UPDATE_CURRENT);
When I click the notification, the close intent is fired. Why?
Here is how I create the notification:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(text))
.setStyle(new NotificationCompat.BigTextStyle().bigText(getString(details)))
.setSmallIcon(R.drawable.ic_not)
.setContentIntent(clickIntent)
.setOngoing(true);
boolean showCloseButton = MainApp.getPrefs().enableCloseServiceButtonInNotification();
if (showCloseButton)
builder.addAction(R.drawable.ic_close_black_24dp, getString(R.string.stop_service), closeIntent);

This is my guess... If does not work, I'll delete the answer...
I believe problem is that they are sharing same update number:
This is created:
clickIntent = PendingIntent.getService(
getApplicationContext(),
Constants.REQUEST_CODE_NOTIFICATION_OTHER,
new Intent(getApplicationContext(), MainService.class).putExtra(Constants.EVENT, Constants.EVENT_TOGGLE_BLACK),
PendingIntent.FLAG_UPDATE_CURRENT);
Then, this new one is created:
PendingIntent closeIntent = PendingIntent.getService(
getApplicationContext(),
Constants.REQUEST_CODE_NOTIFICATION_OTHER,
new Intent(getApplicationContext(), MainService.class).putExtra(Constants.EVENT, Constants.EVENT_DISABLE_SERVICE),
PendingIntent.FLAG_UPDATE_CURRENT);
It is similar to previous one and it is set PendingIntent.FLAG_UPDATE_CURRENT.
So, the second one UPDATES the first one.
Try to use a different code for different PendingIntents. Both are using Constants.REQUEST_CODE_NOTIFICATION_OTHER.
Then, Android will use that code to differentiate them.

Related

Send data to activity when clicked on notification?

i am opening a activity when notification is clicked . notification is activated from a service class .i want to send data to new activity opened from notification i am using intent1.putExtra("lable",lable); but in new activity it is giving me nullpointer exeption.
intent1 = new Intent(this.getApplicationContext(), simplestop.class);
intent1.putExtra("lable",lable);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent1, 0);
Notification mNotify = new Notification.Builder(this)
.setContentTitle("title" + "!")
.setContentText("Click me!")
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentIntent(pIntent)
.setAutoCancel(true)
.build();
I use this code for send data to activity.
Intent intent = new Intent(this, MainActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("from", "notification");
pIntent = PendingIntent.getActivity(this, 3, intent,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
May be you have problem in setting flag=0 or try to change request code=0 to any other integer.
Sending data.
Intent intent = new Intent(getApplicationContext(), simplestop.class);
intent.putExtra("lable", lable);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
final RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.yourLayoutOfNotification);
contentView.setOnClickPendingIntent(R.id.IdOfTheItemYouClick, contentIntent);
Try to retrieve data something like this in your simplestop.class
if (getIntent().getDataString() == null) {
String lable = Objects.requireNonNull(getIntent().getExtras()).getString("lable");
}
And I think you need to call the onNewIntent at the simplestop.class
And there to retrieve data.
And try to create a Log if the data are retrieved.
Log.d("Data", lable);
And tell what is the Log showing.

Two buttons, one Activity

I have two buttons in my notification which opens the same activity but sending as extra different data. Do I really need to create 2 pending intents and 2 intents? There is maybe some shorten version of this?
Intent intent1 = new Intent(this, ResponseActivity.class); //same
intent1.putExtra("RES","a");
intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent1 = PendingIntent.getActivity(this, 1, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
Intent intent2 = new Intent(this, ResponseActivity.class); //same
intent2.putExtra("RES","b"); //but different
intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent2 = PendingIntent.getActivity(this, 1, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_background)
.addAction(0, "A", pendingIntent1)
.addAction(0, "B", pendingIntent2)
.setLargeIcon(bitmap)
.setContentTitle(userDB.getName())
.setContentText(smallText)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setColor(getResources().getColor(R.color.colorPrimary))
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
You have a problem with your code. Your code will end up setting the same PendingIntent with the same Intent on both buttons. This is because the "extras" are not considered when comparing the Intents to determine if a PendingIntent already exists for a given Intent. In your case, the 2 Intents look the same, so the 2 calls to PendingIntent.getActivity() will return the same PendingIntent.
You need to change your code to look like this:
Intent intent = new Intent(this, ResponseActivity.class); //same
intent.putExtra("RES","a");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent1 = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Update the "extra" in the Intent
intent.putExtra("RES","b"); //but different
PendingIntent pendingIntent2 = PendingIntent.getActivity(this, 2, intent, PendingIntent.FLAG_UPDATE_CURRENT);
I used a single Intent to produce 2 different PendingIntents. The key here is to use different values for the requestCode (which is the 2nd parameter to PendingIntent.getActivity()). This ensures that you get 2 different PendingIntents.
Yes
An Intent isn't a call to open an activity, but is actually more of a message to an activity (which in most cases is a message to start the activity).
And so unfortunately, if you want two different messages, you need two different intents.
Considering putEtra() returns the Intent you could shorten your code like this :
public static PendingIntent getPendingIntent(Intent intent) {
return PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
Intent intent = new Intent(this, ResponseActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
.addAction(0, "A", getPendingIntent(intent.putExtra("RES","a")))
.addAction(0, "B", getPendingIntent(intent.putExtra("RES","b")))

Prevent device of turning the screen on when handling custom action in Notification

Would you please help me with the issue below?
I created a simple app that shows a notification for a incoming SMS. On that notification, I added a button to delete the SMS thru the notification.
Since I have a Samsung Gear S2, that Delete button is displayed on the Smart Watch and I can delete the SMS using my Gear S2.
Main problem is that when I delete the SMS using the Gear S2, the screen is wakening up. When I test using Gmail, same scenario just delete the email and keep the screen off.
So, could you please, help me to undestand why the screen is turning on?
Here, is how I create the notification (after receiving a SMS).
// Intent used to delete the SMS
Intent deleteIntent = new Intent(context, MessagingService.class);
deleteIntent.putExtra("notiID", id);
deleteIntent.putExtra("address", address);
deleteIntent.putExtra("date", date);
deleteIntent.putExtra("body", body);
PendingIntent deletePendingIntent = PendingIntent.getService(
context,
id,
deleteIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
// Intent used to start the app
Intent clickIntent = new Intent(context, MainActivity.class);
PendingIntent clickPendingIntent = PendingIntent.getActivity(
context,
id + 1,
clickIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
// Notification
NotificationCompat.Builder notiBuilder = new NotificationCompat.Builder(context);
notiBuilder.setSmallIcon(R.drawable.ic_message_white_32dp)
.setContentTitle(address)
.setContentText(body)
.setContentIntent(clickPendingIntent)
.addAction(R.drawable.ic_delete_white_32dp, context.getString(R.string.delete), deletePendingIntent)
.setLights(Color.BLUE, 3000, 3000);
Notification mNotificationBar = notiBuilder.build();
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Service.NOTIFICATION_SERVICE);
mNotificationManager.notify(id, mNotificationBar);
What I tested:
For now, I move the "SMS Deletion" code to a Service. That's why I'm using:
Intent deleteIntent = new Intent(context, MessagingService.class);
PendingIntent deletePendingIntent = PendingIntent.getService(....);
But I also tried to delete the SMS using the BroadcastReceiver (same result):
Intent deleteIntent = new Intent(context, SmsReceiver.class);
deleteIntent.setAction("com.test.simplesms.DELETE_MESSAGE");
PendingIntent deletePendingIntent = PendingIntent.getBroadcast(....);
So, I'm not sure why the action configured by deletePendingIntent is turning the screen on.
Eventually, I could fix the error and I'm sharing here for future reference.
After debugging and researching, I discovered that I should extend my notification for wearable devices via WearableExtender.
This way, addAction() add the actions to Notification Bar while extend() add an WearableExtender which configure the actions that can be performed by the smartwatch (and this way, you can configure different stuff for Notification Bar and the Smartwatch)
// Intent used to delete the SMS
Intent deleteIntent = new Intent(context, SmsReceiver.class);
deleteIntent.putExtra("notiID", id);
deleteIntent.putExtra("address", address);
deleteIntent.putExtra("date", date);
deleteIntent.putExtra("body", body);
PendingIntent deletePendingIntent = PendingIntent.getBroadcast(
context,
id,
deleteIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
// Intent used to start the app
Intent clickIntent = new Intent(context, MainActivity.class);
PendingIntent clickPendingIntent = PendingIntent.getActivity(
context,
id + 1,
clickIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
// ADD THIS
// Add a wearable extender.. an wearable action
NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender();
wearableExtender.addAction(new NotificationCompat.Action(R.drawable.ic_delete_white_32dp, context.getString(R.string.delete), deletePendingIntent));
// Notification
NotificationCompat.Builder notiBuilder = new NotificationCompat.Builder(context);
notiBuilder.setSmallIcon(R.drawable.ic_message_white_32dp)
.setContentTitle(address)
.setContentText(body)
.setContentIntent(clickPendingIntent)
.extend(wearableExtender) // ----> ADD THE WEARABLE HERE
.addAction(R.drawable.ic_delete_white_32dp, context.getString(R.string.delete), deletePendingIntent)
.setLights(Color.BLUE, 3000, 3000);
Notification mNotificationBar = notiBuilder.build();
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Service.NOTIFICATION_SERVICE);
mNotificationManager.notify(id, mNotificationBar);
I think your SmsReceiver class extends from WakefulBroadcastReceiver

Sometimes this happens Couldn't expand RemoteViews for: StatusBarNotification

So, my app always work perfectly, but sometimes show this error
Bad notification posted from package com.myapp: Couldn't expand RemoteViews for: StatusBarNotification
Intent intentRadar = new Intent(this, RadarActivity.class);
intentRadar.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, (int)System.currentTimeMillis(), intentRadar, 0);
//Intent to close app
Intent intentClose = new Intent(this, CloseAppReceiver.class);
PendingIntent closeIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intentClose,0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setTicker(getResources().getString(R.string.titulo_app_executando));
builder.setContentTitle(getString(R.string.titulo_app_executando));
builder.setContentText(getString(R.string.msg_app_run));
builder.setSmallIcon(R.drawable.ic_notification);
builder.setContentIntent(pendingIntent);//Seta a intent do radar para voltar
builder.setOngoing(true);
//action off app
builder.addAction(R.drawable.turn_off, getResources().getString(R.string.off_app), closeIntent);
builder.setWhen(0);
builder.setPriority(Notification.PRIORITY_MAX);
notificationAppRun = builder.build();
nm.notify(R.drawable.ic_notification,notificationAppRun);//show notification
Why won't you use remoteView?
nm.notify(R.drawable.ic_notification, notificationAppRun); //show notification
I think the first param is wrong. You need to set an id here, not the drawable id

Two buttons with PendingIntents - Widget

I'm creating a widget with two buttons.
One of them updates the content of the widget and the second one must launch an activity.
I have two PendingIntent for each action, but I can't make them both work. If one works the other one doesn't.
I've revised the code and can't understand what's wrong.
Any help will be very appreciated.
This is the code.
RemoteViews controls = new RemoteViews(context.getPackageName(), R.layout.miwidget);
Intent intent = new Intent("actony.com.ACTUALIZAR_WIDGET");
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
Intent intentSettings = new Intent();
intentSettings.setClass(context,WidgetConfig.class);
PendingIntent pendingIntentUpdate = PendingIntent.getBroadcast(context, widgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
controls.setOnClickPendingIntent(R.id.BtnActualizar, pendingIntentUpdate);
PendingIntent pendingIntentSettings = PendingIntent.getActivity(context, 0, intentSettings, 0);
controls.setOnClickPendingIntent(R.id.botonSettings, pendingIntentSettings);
Try adding the getActivity PendingIntent.FLAG_UPDATE_CURRENT aswell...
PendingIntent pendingIntentSettings =
PendingIntent.getActivity(context, 0, intentSettings, PendingIntent.FLAG_UPDATE_CURRENT);
and if multiple widget's are possible add the widgetId there too.
Make sure both of the activities/broadcasts are listed in the manifest file.
Moreover, try creating the Intent with this constructor:
Intent intent = new Intent(context,ACTUALIZAR_WIDGET.class);
Intent intentSettings = new Intent(context,WidgetConfig.class);
add imports if needed.
Hope some of that will make you widget work.
Check this link to know which button has been clicked when there is two or more button in a widget..
https://stackoverflow.com/a/10733049/1331593
It should work... IF it does not work please let me know what is the problem...
You can try this code:
Intent read = new Intent(ctx, NotificationClick.class);
read.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
read.putExtra(Intent.EXTRA_SUBJECT, "READ");
PendingIntent readInt = PendingIntent.getActivity(ctx, 1, read, PendingIntent.FLAG_IMMUTABLE);
Intent reply = new Intent(ctx, NotificationClick.class);
reply.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
reply.putExtra(Intent.EXTRA_SUBJECT, "REPLY");
PendingIntent replyInt = PendingIntent.getActivity(ctx, 2, reply, PendingIntent.FLAG_IMMUTABLE);
NotificationManagerCompat nMgr = NotificationManagerCompat.from(ctx);
Notification newMessageNotification = new NotificationCompat.Builder(ctx, "MESSAGE_CHANNEL")
.setSmallIcon(R.drawable.user_account)
.setContentTitle(contact)
.setContentText(text)
.addAction(R.drawable.drafts, "Read", readInt)
.addAction(R.drawable.drafts, "Reply", replyInt)
.setGroup(MESSAGE_GROUP)
.build();
nMgr.notify(100, newMessageNotification);

Categories

Resources