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.
Related
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
This may be off topic , but I couldn't found anything for it.
Is there any limit on the number of notifications android app can display?I am facing issue after 100 notifications. There is no documentation which states this clearly.
Note: This is not really a good idea to show 100 notifications but It is required for certain reasons.
According to #Nirel's answer.
1) I tried to run the code in 3 different devices.
Surprisingly notifications beyond 50 are not showing in notification area.
It gives following error.
W/NotificationManager﹕ notify: id corrupted: sent 51, got back 0
The same error comes for subsequent calls.
I saw the source of NotificationManager , it gives this error if incoming and out id is not same. See below code.
https://github.com/android/platform_frameworks_base/blob/master/core/java/android/app/NotificationManager.java#L233
2) After I tried to notify on intervals of 100 milliseconds.
It also Gives the same error. What I tried is removed 1 notification when code is executed.
Surprisingly , notification number 153 came in status bar.
So the conclusion is that , at most 50 notifications can be there. This may be default behaviour and may can change by manufacturer as said by #Sharp Edge.
Thnx.
In API23
package com.android.server.notification;
NotificationManagerService.java
static final int MAX_PACKAGE_NOTIFICATIONS = 50;
The limit for notifications and toasts is per app 50
this post has really helped me to do research on this topic. I have written an article on this like how can you modify your logic and keep posting notifications even if you have reached the maximum limit by compromising on the oldest notifications. https://medium.com/mindorks/the-notification-limit-per-app-in-android-94af69a6862c
The notification limit dropped from 50 to 24 per appin the Android 10 notification drawer.
Read more about it here.
run this:
// prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(this, NotificationReceiver.class);
// use System.currentTimeMillis() to have a unique ID for the pending intent
PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);
// build notification
// the addAction re-use the same intent to keep the example short
Notification n = new Notification.Builder(this)
.setContentTitle("New mail from " + "test#gmail.com")
.setContentText("Subject")
.setSmallIcon(R.drawable.icon)
.setContentIntent(pIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
for(int i = 0;i<1000;i++)
{
Log.d("Tag", "notification number" + i "just published")
notificationManager.notify(i, n);
}
when the application will crash you will see how much notification you have..
I'm trying to develop with Android Wear. I tried all the tutorial provided in the documentation, but now I want to try to do something smarter. I'm trying to get back the text that user says (with emulator written by computer keyboard), so I made it with this code:
protected void voiceNotification() {
// Crete intent for the response action
Intent replyIntent = new Intent(this, ReplyActivity.class);
// Adding intent to pending intent
PendingIntent replyPendingIntent = PendingIntent.getActivity(this, 0,
replyIntent, 0);
// Build the notification
NotificationCompat.Builder replyNotificationBuilder = new NotificationCompat.Builder(
this);
replyNotificationBuilder
.setSmallIcon(android.R.drawable.ic_btn_speak_now);
replyNotificationBuilder.setContentTitle("Messaggio");
replyNotificationBuilder.setContentText("Testo del messaggio");
replyNotificationBuilder.setContentIntent(replyPendingIntent);
replyNotificationBuilder.setNumber(++numMessages);
replyNotificationBuilder.setAutoCancel(true);
replyNotificationBuilder.setSound(RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
replyNotificationBuilder.setVibrate(new long[] { 1000, 1000 });
replyNotificationBuilder.setTicker("Hai una nuova notifica!");
// Create remote input
RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
.setLabel(getResources().getString(R.string.reply_label))
.build();
// Create the wearable notification
Notification replyNotification = new WearableNotifications.Builder(replyNotificationBuilder)
.addRemoteInputForContentIntent(remoteInput)
.build();
// Get the instance of NotificationManagerCompat and send my notification
NotificationManagerCompat.from(this).notify(0, replyNotification);
}
With this code on the emulator I'm getting 2 views: one with the text of my notification and a second one in which I can answer to notification with voice (keyboard with emulator). It's working all good, but I want to know if it's possible to get the text I said (wrote with emulator) to do something in my application (I saw on the emulator display that after I said/wrote somethings it appears 2 button "Edit" and "Send", so I think that with button "Send" I can get the text in my application to do something). I try to find out something in the documentation, but I don't find nothing. I hope you can help me to get this text.
You'll need to implement a Broadcast-receiver that listens to the pendingIntent you defined - the reply from the user will be passed in an extra string you defined in the RemoteInput - in your case this would be EXTRA_VOICE_REPLY.
You might want to have a look at these two files someone posted on GitHub in order to understand what is going on.
http://git.io/emKcrw
http://git.io/_PRW_w
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?