I'm creating a notification with multiple actions. I'm using broadcast intents to communicate that one has been pushed and take specific action. There are 4 buttons, and I've created 4 separate intents. Each one has the same Action string, but a different StringExtra.
Intent intNow = new Intent(mThis, MyReceiver.class).setAction(actionNotify).putExtra("button", ACT_NOW);
Intent intEmail = new Intent(mThis, MyReceiver.class).setAction(actionNotify).putExtra("button", ACT_EMAIL);
Intent intLater = new Intent(mThis, MyReceiver.class).setAction(actionNotify).putExtra("button", ACT_LATER);
Intent intNever = new Intent(mThis, MyReceiver.class).setAction(actionNotify).putExtra("button", ACT_NEVER);
Notification.Builder myRatingNotification = new Notification.Builder(mThis)
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(R.mipmap.ic_launcher)
.addAction(0, mThis.getString(R.string.Rate_Act_Now), PendingIntent.getBroadcast(mThis, 0, intNow, PendingIntent.FLAG_UPDATE_CURRENT))
.addAction(0, mThis.getString(R.string.Rate_App_Email), PendingIntent.getBroadcast(mThis, 0, intEmail, PendingIntent.FLAG_UPDATE_CURRENT))
.addAction(0, mThis.getString(R.string.Rate_Act_Later), PendingIntent.getBroadcast(mThis, 0, intLater, PendingIntent.FLAG_UPDATE_CURRENT))
.addAction(0, mThis.getString(R.string.Rate_Act_Never), PendingIntent.getBroadcast(mThis, 0, intNever, PendingIntent.FLAG_UPDATE_CURRENT))
.setAutoCancel(true);
Notification notification = new Notification.BigTextStyle(myRatingNotification).bigText(text).build();
((NotificationManager) mThis.getSystemService(Context.NOTIFICATION_SERVICE)).notify(notificationId, notification);
So the notification is created successfully. The buttons are there. But no matter which one I push, the extra that is passed to the receiver is always the last action defined. That is, in the example above, every button returns a String Extra equal to ACT_NEVER. If I reorder the .addAction so intLater is last, the receiver tells me that the String Extra is equal to ACT_LATER, no matter which button I push.
I can't figure out why - the 4 Intents are completely independent of each other. The actions specify the correct Intent. What's going on? I'm stumped.
you should set an icon as first parameter,not 0.
your current result because you use same action and same requestCode to construct a PendingIntent,so the 4 PendingIntent will be the same,and you use PendingIntent.FLAG_UPDATE_CURRENT,so the last PendingIntent's extra will replace pre one.
So to solve your problem,you just need to set different requestCode for four PendingIntent,like this:
.addAction(0, mThis.getString(R.string.Rate_Act_Now), PendingIntent.getBroadcast(mThis, 0, intNow, PendingIntent.FLAG_UPDATE_CURRENT))
.addAction(0, mThis.getString(R.string.Rate_App_Email), PendingIntent.getBroadcast(mThis, 1, intEmail, PendingIntent.FLAG_UPDATE_CURRENT))
.addAction(0, mThis.getString(R.string.Rate_Act_Later), PendingIntent.getBroadcast(mThis, 2, intLater, PendingIntent.FLAG_UPDATE_CURRENT))
.addAction(0, mThis.getString(R.string.Rate_Act_Never), PendingIntent.getBroadcast(mThis, 3, intNever, PendingIntent.FLAG_UPDATE_CURRENT))
I have read documentation about addAction(), and quite interesting stuff is there:
A notification in its expanded form can display up to 3 actions
Every action must have an icon
You use 0 as a Icon, and 4 actions, maybe this have some impact on behavior
Related
I am using XMPP (smack) to create a messaging application and I am sending notifications whenever I receive a new message. The problem is that if I receive messages from two different users I can only see the last notification. How can I change it? Here is my code.
Intent thisIntent = new Intent(mApplicationContext, ChatActivity.class);
thisIntent.putExtra("EXTRA_CONTACT_JID",contactJid);
PendingIntent contentIntent = PendingIntent.getActivity(mApplicationContext, 0, thisIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder b = new NotificationCompat.Builder(mApplicationContext);
b.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.fab_bg_mini)
.setTicker("Hearty365")
.setContentTitle("New message")
.setContentText(" You received a new message from " + contactJid)
.setContentIntent(contentIntent)
.setContentInfo("Info");
if(!ChatActivity.active){
b.setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND);
}
NotificationManager notificationManager = (NotificationManager) mApplicationContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, b.build());
And as you can see I put an extra contactJid which is important to me. I need to set it in such a way that if a user clicks one notification its contactJid will be this and if another its contactJid will be another.
notificationManager.notify(1, b.build()); is your problem - you need to supply a unique identifier for this notification, as per the documentation:
If a notification with the same id has already been posted by your application and has not yet been canceled, it will be replaced by the updated information.
You are supplying the constant 1 for each notification, instead of a unique ID. I'd suggest using a hash of the contact JID (which I assume is a string):
notificationManager.notify(contactJid.hashCode(), b.build());
My icons are having names icon_1, icon_2, icon_3 and so on. And I want to change the icon in the notification dynamically according to the input. The input is a number ranging from 1 to 100.
if the input is 1 then icon_1 should be shown, if input is 2 then icon_2 will be shown and so on. Is it possible to set the icon in one line or we are forced to use switch case statement? The example with the code I am pasting here to better understand. Switch case statement definitely will help out but only want to know if it is possible to write in one line to save 100 lines of code.
The following lines of code may not work. But just for understanding the things, I have used.
Input is a number in the variable name of num.
Intent intent = new Intent(this, NotificationReceiver.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification n = new Notification.Builder(this)
.setContentText("Subject")
.setSmallIcon(R.drawable.icon_+"num") //Here is the doubt..How can we modify this line to work
.setContentIntent(pIntent)
.setAutoCancel(true)
.build();
NotificationManager notificationManager=NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, n);
Have a look at this
//create a array of your notification icons
int[] not_icon={R.drawable.icon_1,R.drawable.icon_2,R.drawable.icon_3.......so on};
//pass the array accordingly to your input or payload
.setSmallIcon(not_icon[3]); //3 is the number you received in your payload.
After reading multiple posts I have tried everything to generate a new notification for every alert using unique id. However this is not working. Following is my code:-
//Generate random id for notification
Random r=new Random();
int id=r.nextInt(9999);
PendingIntent intent =PendingIntent.getActivity(getApplicationContext(), id, notificationIntent, 0);
Builder notice2=new Notification.Builder(getApplicationContext())
.setContentTitle(call.getName())
.setAutoCancel(true)
.setContentIntent(intent)
.setContentText("Context")
.setSmallIcon(com.project.calltracker.R.drawable.ic_alert)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), com.project.calltracker.R.drawable.ic_logo));
startForeground(id, notice2.getNotification());
As you can see I am using a random integer as Id each time I generate a notification. But still I only get a single notification no matter how many times I startForeground is called??
Please help!
Thanks!
That's because there's always just ONE foreground service running. Services are singletons by nature, there can't be 2 instances of the same service running.
If you want to just send multiple notifications you should use the notification manager, notify() method
You dont have to generate a random id, using a sequence of ids will be more safe and ensure that all the ids are unique
PendingIntent intent = null;
Builder notice2=null;
int N = 1000; //set this value
for(int id=1;id<N; id++){
intent =PendingIntent.getActivity(getApplicationContext(), id, notificationIntent, 0);
notice2=new Notification.Builder(getApplicationContext())
.setContentTitle(call.getName())
.setAutoCancel(true)
.setContentIntent(intent)
.setContentText("Context")
.setSmallIcon(com.project.calltracker.R.drawable.ic_alert)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), com.project.calltracker.R.drawable.ic_logo));
startForeground(id, notice2.getNotification());
}
I hope it helps
I am using NotificationCompat.Builder to make a simple notification, which is then added to the notification bar. On an AVD running API 17, the notification can be seen in the notification pull-down as expected. But on an AVD running API 13, the title/text/info of the notification cannot be seen (possibly due to black text against the black background).
In searching for a solution to my problem, I found the following: Custom notification colors and text colors But this seems to apply only in the case of using a custom layout for the notification.
Here is my code:
private static int notificationId = 1;
private void displayNotification() {
// create an Intent to launch the Show Notification activity
// (when user selects the notification on the status bar)
Intent i = new Intent(this, ShowNotificationActivity.class);
// pass it some value
i.putExtra(ShowNotificationActivity.NOTIF_ID, notificationId);
// wrap it in a PendingIntent
PendingIntent pendingIntent = PendingIntent.getActivity(this, notificationId, i, 0);
// create the notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle("This is the Title #" + notificationId);
builder.setContentText("This is the Text");
builder.setContentInfo("This is the Info");
builder.setContentIntent(pendingIntent);
builder.setSmallIcon(android.R.drawable.ic_notification_overlay);
builder.setAutoCancel(true);
Notification notif = builder.build();
// display the notification
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
nm.notify(notificationId, notif);
notificationId++;
}
I had hoped to post some screen shots showing the notification pull-down under API 17 and under API 13, but apparently I don't have sufficient 'reputation points' to post images. So I guess a written description will have to suffice:
Under API 17, the notification pull-down shows the notification's icon (a red dot), title, and text. The background color is black, and the text color is white.
Under API 13, only the icon (red dot) is visible. The background color is black, I suspect the text color may be black as well.
I suspect that I am missing something obvious, but I am new to this and would appreciate any pointers.
When I copy-pasted your code into my empty activity I got several errors(it seems your code is incomplete). I added these lines to make it work:
int notificationId = 1;
Intent intent = new Intent(this, MyActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
//Here goes your code.
I just tested your code on a 3.2 emulator and this is what the notification looks like:
Which, I believe, is what it should look like.
Coult you test with my code and let me know if it works?
I have integrated notifications in my app, and I have handle 2 cases :
Pre Jelly bean notifications with NotificationCompat.Builder, and,
Post Jelly bean notification with builder.
This makes me able to manage big text, and actions in post jelly bean version, and works for 2 or 3 time, but in a strange way, today I got the same result in JB and below.
Code for Pre JB :
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pint = PendingIntent.getActivity(context, 0, notificationIntent, 0);
NotificationCompat.Builder notif = new NotificationCompat.Builder(context)
.setContentTitle(nMessage.getTitle())
.setContentText(nMessage.getMessage())
.setTicker(nMessage.getTitle())
.setWhen(when)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setSmallIcon(R.drawable.ic_ttd_petales)
.setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
.setAutoCancel(true)
.setContentIntent(pint);
notification= notif.build();
notificationManager.notify(0, notification);
Code for JB and above :
Builder bigTextNotification = new Notification.Builder(context)
.setContentTitle(nMessage.getTitle())
.setTicker(nMessage.getTitle())
.setContentText(nMessage.getMessage())
.setWhen(when)
.setPriority(Notification.PRIORITY_HIGH)
.setSmallIcon(R.drawable.ic_ttd_petales)
.setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.setContentIntent(pint);
if(nMessage.getHasPhone()){
Intent iCall = new Intent(Intent.ACTION_CALL,Uri.parse(nMessage.getPhone()));
PendingIntent pintCall = PendingIntent.getActivity(context, 0, iCall, Intent.FLAG_ACTIVITY_NEW_TASK);
bigTextNotification.addAction(R.drawable.ic_menu_call, context.getResources().getString(R.string.call_ttd), pintCall);
}
String[] recipients = new String[]{context.getResources().getString(R.string.default_email)};
String subject = context.getResources().getString(R.string.about_offer);
Intent iEmail = new Intent(android.content.Intent.ACTION_SEND);
iEmail.setType("text/html");
iEmail.putExtra(android.content.Intent.EXTRA_EMAIL, recipients);
iEmail.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
PendingIntent pintEmail = PendingIntent.getActivity(context, 1, iEmail, Intent.FLAG_ACTIVITY_NEW_TASK);
bigTextNotification.addAction(R.drawable.ic_menu_compose, context.getResources().getString(R.string.call_ttd), pintEmail);
Notification notif = new Notification.BigTextStyle(bigTextNotification)
.bigText(nMessage.getMessage())
.build();
notificationManager.notify(1000, notif);
Have you ever encountered such behaviour or am I missing something?
I had a similar experience: I would send the notification and it would show the content text instead of the big text. Then when I unplugged the USB, it would show the big text expanded.
Now notice that when you plug in to USB, you get an ongoing notification: "Connected as a Media Device". Take a look and see if you get the same behavior with some other ongoing notification, like "Searching using GPS". I do. (My test device is a Samsung Galaxy Rush, and its vertical space is severely limited.)
So while the Android Notifications documentation says that only the top notification shows as expanded, it looks like there's another rule as well:
If the notifications list is running out of room, it will collapse even the top notification.
You can expand the collapsed notification by double- or single-finger dragging, depending on the OS version.
I'm sorry for this dummy question, I'm not using emulator to test this app, but real phone, and the strange thing is that when usb debugging cable is plugged, there's no actions in the notifications (JB) and when unplugged, we get our buttons working :) this solves my issue, but I have no idea about this behaviour.