How to invalidate RemoteView - android

I am making custom notification, and in receiver for intents I change text in element in layout of notification itself. When I update Notification nothing happens. Intents are being passed to receiver, I checked in log. Activity that makes notification, and receiver are separate classes. Any ideas? I know for that AppWidgetProvider for widget works great, but in this case there is no AppWidgetProvider, this is plain notification. Is there any other way to invalidate RemoteView?
//Receiver.onReceive() method
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.notification_layout);
//make dataString
views.setTextViewText(R.id.n_track_name, dataString);
//while searching i figured out this is something that should update notification
NotificationManager mNotificationManager = (NotificationManager)act.getSystemService(Activity.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, act.notification);
This is code which makes Notification for first time in other activity:
Notification notification = new Notification(icon, "Music Player", when);
NotificationManager mNotificationManager = (NotificationManager)MusicPlayerActivity.currentActivity.getSystemService(NOTIFICATION_SERVICE);
RemoteViews contentView = new RemoteViews(act.getPackageName(), R.layout.notification_layout);
//make pending intent on button
Intent intent = new Intent(act, NotificationReceiver.class);
intent.setAction(NotificationReceiver.ACTION_NOTIFICATION_NEXT);
PendingIntent actionPendingIntent = PendingIntent.getBroadcast(act, 0, intent, 0);
contentView.setOnClickPendingIntent(R.id.n_btnNext, actionPendingIntent);
//make other intents
//show notification
mNotificationManager.notify(1, notification);

Related

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

Notification is not being dismissed (Android)

Notification setAutoCancel(true) doesn't work if clicking on Action
I have a notification with an action within it. When I tap on the notification it gets removed from the list. However, when I click on the Action it successfully completes the Action (namely makes a call), but when I return to the list of notifications, it remains there.
Relative code of the AlarmReceiver:
public class AlarmReceiver extends BroadcastReceiver {
Meeting meeting;
/**
* Handle received notifications about meetings that are going to start
*/
#Override
public void onReceive(Context context, Intent intent) {
// Get extras from the notification intent
Bundle extras = intent.getExtras();
this.meeting = extras.getParcelable("MeetingParcel");
// build notification pending intent to go to the details page when click on the body of the notification
Intent notificationIntent = new Intent(context, MeetingDetails.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
notificationIntent.putExtra("MeetingParcel", meeting); // send meeting that we received to the MeetingDetails class
notificationIntent.putExtra("notificationIntent", true); // flag to know where the details screen is opening from
PendingIntent pIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
// build intents for the call now button
Intent phoneCall = Call._callIntent(meeting);
if (phoneCall != null) {
PendingIntent phoneCallIntent = PendingIntent.getActivity(context, 0, phoneCall, PendingIntent.FLAG_CANCEL_CURRENT);
int flags = Notification.FLAG_AUTO_CANCEL;
// build notification object
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Notification notification = builder.setContentTitle("Call In")
.setContentText(intent.getStringExtra("contextText"))
.setTicker("Call In Notification")
.setColor(ContextCompat.getColor(context, R.color.colorBluePrimary))
.setAutoCancel(true) // will remove notification from the status bar once is clicked
.setDefaults(Notification.DEFAULT_ALL) // Default vibration, default sound, default LED: requires VIBRATE permission
.setSmallIcon(R.drawable.icon_notifications)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(meeting.description))
.addAction(R.drawable.icon_device, "Call Now", phoneCallIntent)
.setCategory(Notification.CATEGORY_EVENT) // handle notification as a calendar event
.setPriority(Notification.PRIORITY_HIGH) // this will show the notification floating. Priority is high because it is a time sensitive notification
.setContentIntent(pIntent).build();
notification.flags = flags;
// tell the notification manager to notify the user with our custom notification
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
}
}
}
use this flag:
Notification.FLAG_AUTO_CANCEL
inside this:
int flags = Notification.FLAG_AUTO_CANCEL;
Notification notification = builder.build();
notification.flags = flags;
Documentation
Ok turns out it's a known problem already, and it needs extra code to be done (keeping reference to notification through id). Have no clue why API does not provide this, as it seems very logical to do. But anyways,
see this answer in stackoverflow:
When you called notify on the notification manager you gave it an id - that is the unique id you can use to access it later (this is from the notification manager:
notify(int id, Notification notification)
To cancel, you would call:
cancel(int id)
with the same id. So, basically, you need to keep track of the id or possibly put the id into a Bundle you add to the Intent inside the PendingIntent?
I faced this problem today, and found that FLAG_AUTO_CANCEL and setAutoCanel(true), both work when click on notification,
but not work for action click
so simply, in the target service or activity of action, cancel the notification
NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancelAll();
or if have more notification
manager.cancel(notificationId);
You have created two pending intent use in boths and change Flag too.
PendingIntent pIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), notificationIntent, 0);
PendingIntent phoneCallIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), phoneCall, PendingIntent.FLAG_UPDATE_CURRENT);
// CHANGE TO THIS LINE
PendingIntent pIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
PendingIntent phoneCallIntent = PendingIntent.getActivity(context, 0, phoneCall, PendingIntent.FLAG_CANCEL_CURRENT);

Unable to cancel the notification from the notification tray when different layouts used for expand/collapse

I have a share option in the notification tray. So, I have used 2 custom views in my code.
1. Expandable Notification Layout
2. Normal Notification Layout
The UI is working fine. But, notifications are misbehaving. If I click the first notification or share first item in the notification, it works perfectly. But, If I click the last notification, then it opens the app but notification is not cleared from the notification tray. Also, the strange thing is, after clicking the notification,small icon which comes in the status bar disappears and now if i click the notification, it doesn't respond. I'm cancelling the notification. That's why second time, when i click it doesn't work. This was not happening when I used default builder layout for normal view.
Here is my code:
//setup a expanded notification layout
RemoteViews expandedView=new RemoteViews(context.getPackageName(), R.layout.custom_notification);
expandedView.setImageViewResource(R.id.image_logo, R.drawable.ic_launcher);
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm aa");
String time = dateFormat.format(new Date(when));
expandedView.setTextViewText(R.id.current_time, time);
expandedView.setTextViewText(R.id.title, context.getResources().getString(R.string.app_name));
expandedView.setTextViewText(R.id.text, message);
expandedView.setTextViewCompoundDrawables(R.id.share, R.drawable.gcm_share, 0, 0, 0);
//set a normal notification layout
RemoteViews collapsedView=new RemoteViews(context.getPackageName(), R.layout.custom_notification_normal_layout);
collapsedView.setTextViewText(R.id.text, message);
notificationId = ((int) System.currentTimeMillis() % 1000000);
//register listenr for Share Icon
setBtnListeners(expandedView, requestID, message, context, notificationId);
`Bitmap largeIcon = ((BitmapDrawable) context.getResources().getDrawable(R.drawable.ic_launcher)).getBitmap()`;
//Construct notification Builder
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(context);
mNotifyBuilder
.setWhen(when)
.setSmallIcon(icon)
.setLargeIcon(largeIcon)
.setContentTitle(message)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message));
notification = mNotifyBuilder.build();
//assign the vies to the notification builder
notification.bigContentView = expandedView;
notification.contentView = collapsedView;
//create pending Intent
Intent notificationIntent;
//notificationIntent = new Intent(context, Launcher.class);
notificationIntent = new Intent(context, SplashActivity.class);//Sritapana189
notificationIntent.putExtra(BundleKeys.NORMAL_PUSH, 1);
notificationIntent.putExtra(BundleKeys.DEFAULT_NOTI_NAV, nav);
notificationIntent.putExtra(BundleKeys.FROM_GCM, true);
notificationIntent.putExtra(ApplicationConstants.GCMKeys.GCM_NOTIFICATION_ID, notificationId);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(context, requestID, notificationIntent, 0); //Modified
notification.contentIntent = contentIntent;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(notificationId, notification);
//Registering the share icon of notification tray
private static void setBtnListeners(RemoteViews notificationView, int requestId, String message, Context context, int notificationId) {
Intent shareBtnIntent = new Intent(context, GcmTransparentActivity.class);
shareBtnIntent.putExtra(ApplicationConstants.GCMKeys.BREAKING_NEWS, message);
shareBtnIntent.putExtra(ApplicationConstants.GCMKeys.GCM_NOTIFICATION_ID, notificationId);
PendingIntent pendingIntent = PendingIntent.getActivity(context, requestId, shareBtnIntent, 0);
notificationView.setOnClickPendingIntent(R.id.share, pendingIntent);
}
//SplashActivity recieving Pending Inent
//here I'm retrieving the payload data and saving it and then cancelling the notification
Utility.cancelNotification(gcmNotificationId, getApplicationContext());
public static void cancelNotification(int id, Context ctx) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager nMgr = (NotificationManager) ctx.getSystemService(ns);
nMgr.cancel(id);
}
The only change i did in my code is I added custom layout for the normal view.After this change, it's misbehaving when last notification item is clicked. Is there anything I'm missing. please help me to sort out this strange issue.
try this,
problem may be because you are not getting the notification id properly
quick fix can be,
Notification nNotificationManager nMgr = (NotificationManager) ctx.getSystemService(ns);
nMgr.cancel(id);
replace this with ,
Notification nNotificationManager nMgr = (NotificationManager) ctx.getSystemService(ns);
nMgr.cancelAll();
I resolved my issue. The problem was with the RemoteViews. I had made them local and making them final solved my issue
final RemoteViews expandedView=new RemoteViews(context.getPackageName(), R.layout.custom_notification);
final RemoteViews collapsedView=new RemoteViews(context.getPackageName(), R.layout.custom_notification_normal_layout)

How can I modify a textview from a non-activity class? Android

I want to modify a TextView from an alarmmanager class's onReceive method this class does not extend activity. The alarm sends an intent that creates a notification and I want to make it so clicking the notification will open a textview with a particular string that I get in this onReceive method.
RemoteViews contentView = new RemoteViews(context.getPackageName(),R.layout.notify_layout);
contentView.setTextViewText(R.id.notetext,my_message);
notif.contentView = contentView;
I tried to do this using the RemoteView above with the notification intent but it doesn't seem to work.
I know I can't access this using findViewById, but what approach should I take here?
This is from the code that handles the intent
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.notify_layout);
NotificationManager nm = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE); nm.cancel(getIntent().getExtras().getInt("com.myapp.NotificationDisp"));
Then I have this whole notification thing here. I know that its deprecated and I should be using builder but I need this to be compatible back to gingerbread which it seems builder is not.
Intent i = new Intent("com.MyApp.NotificationDisp");
i.putExtra("com.MyApp.NotificationDisp", 1);
nm = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
#SuppressWarnings("deprecation")
Notification notif = new Notification(R.drawable.ic_launcher,
"Myapp", System.currentTimeMillis());
//
RemoteViews contentView = new RemoteViews(context.getPackageName(),
R.layout.notify_layout);
contentView.setTextViewText(R.id.notetext,
my_message);
notif.contentView = contentView;
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
i, 0);
notif.contentIntent = contentIntent;
//
notif.setLatestEventInfo(context, "Myapp", my_message, contentIntent);
notif.vibrate = new long[] { 100, 250, 100, 500};
nm.notify(1, notif);
So this brings up a notification with the correct text in my status bar but when I click on it it only shows a blank textview. The xml notify_layout should be setup fine.

Android show notification from BroadcastReceiver

I have a class which extends BroadcastReceiver that gets called whenever new Wifi scan results are available (the receiver is registered in the manifest with the Scan_Results broadcast as the intent-filter).
From this class, I want to be able to show a notification to the user. Currently, I pass the context that is received as a parameter in the onReceive method of my broadcast intent class to a "show notification" method of another class.
When it gets to the line:
myNotificationManager.notify(notificationId, notification);
it fails with the following exception:
java.lang.IllegalArgumentException: contentView required: pkg=com.mumfordmedia.trackify id=2131034122 notification=Notification(vibrate=null,sound=null,defaults=0x0,flags=0x0)
Any idea why this is happening? All I can think of is because the context that I am getting from the onReceive parameter is not ... for lack of a better phrase, "right for the job"...
Any ideas?
Thanks,
Max.
ContentView is the view which is required when the notification is clicked. The code below works fine and setLatestEventInfo() is required method.
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,
"Hello from service", System.currentTimeMillis());
Intent intent = new Intent(this, MainActivity.class);
notification.setLatestEventInfo(this, "contentTitle", "contentText",
PendingIntent.getActivity(this, 1, intent, 0));
manager.notify(111, notification);
Not sure exactly why it wasnt working before but here is the code I got it working with:
Declare the following outside of any method:
int YOURAPP_NOTIFICATION_ID = 1234567890;
NotificationManager mNotificationManager;
Then in the onReceive method call the following:
mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
showNotification(context, R.drawable.icon, "short", false);
Then declare the following method:
private void showNotification(Context context, int statusBarIconID, String string, boolean showIconOnly) {
// This is who should be launched if the user selects our notification.
Intent contentIntent = new Intent();
// choose the ticker text
String tickerText = "ticket text";
Notification n = new Notification(R.drawable.icon, "ticker text", System.currentTimeMillis());
PendingIntent appIntent = PendingIntent.getActivity(context, 0, contentIntent, 0);
n.setLatestEventInfo(context, "1", "2", appIntent);
mNotificationManager.notify(YOURAPP_NOTIFICATION_ID, n);
}
You have to call Notification.setLatestEventInfo().
Use this code along with your notification
Intent intent = new Intent(this, MusicDroid.class);
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(this, "This is the title",
"This is the text", activity);
notification.number += 1;
nm.notify(NOTIFY_ID, notification);
From what I can tell, when you're creating you notification to pass to the Notification manager, you're not giving it a content view to display. Review the line where you actually create the notification to see if you're actually giving the notification a view to display.
For those who are using NotificationCompat, the following code will work:
NotificationCompat.Builder n = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon).setContentText("Notify Title").setContentText("Sample Text");
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Intent i = new Intent(this,MainActivity.class);
PendingIntent ac = PendingIntent.getActivity(this, 0, i, 0);
n.setContentIntent(ac);
nm.notify(12222, n.build());

Categories

Resources