Android: Managing Multiple Notifications - java

I am trying to create multiple notifications in my application. To identify each notification uniquely, i have given them an unique identificationId. Following is my code:
private void updateNotification(int notificationId, int clockStatusID, CharSequence text) {
//notificationManager.cancel(notificationId);
// throws up an ongoing notification that the timer is running
Log.i("TIMERCOUNT", "Notification id: " + notificationId);
Notification not = new Notification(clockStatusID, // the
// icon
// for
// the
// status
// bar
text, // the text to display in the ticker
System.currentTimeMillis() // the timestamp for the
// notification to appear
);
Intent intent = new Intent();
intent.putExtra("notificationID", notificationId);
intent.setAction("actionstring" + System.currentTimeMillis());
intent.setClassName("com.chander.time.android.activities",
"com.chander.time.android.activities.Tabs");
not.setLatestEventInfo(self,
getText(R.string.timer_notification_title),
getText(R.string.timer_on_notification_text), PendingIntent
.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT));
not.flags += Notification.FLAG_ONGOING_EVENT;
not.flags += Notification.FLAG_NO_CLEAR;
notificationManager.notify(notificationId, not);
}
Problem:
When a notification is selected, Tabs activity is called passing the intent. I want to access the unique notificationId of the notification that was selected in Tabs. I tried intent.putExtra() to save the notificationId in the intent. But, for multiple notifications its overwriting the notificationId and returns the latest one. I dont understand as to why this is happening and how can i avoid this overwriting of notificationId.
Thanks,
Chander

I got the answer. The intents were getting cached in. To, make a new intent, just add the following piece of code:
saveCallIntent.setData((Uri.parse("custom://"+System.currentTimeMillis())));
This makes the intent unique.
Also, someone suggested me the following piece of code to make the intent unique:
saveCallIntent.setAction("actionstring" + System.currentTimeMillis());
It didnt help me, but might be of help to someone else.
--Chander

just place the notificationId instead of 0 in pendingIntent.
PendingIntent.getActivity(this, notificationId, intent,PendingIntent.FLAG_UPDATE_CURRENT));
The user must also update the following code together with the above one to get it working.
mNotificationManager.notify(notificationId, mBuilder.build());

Related

Actions not showing up in Remote Notification on Android

I can't seem to figure out why I can'y get my actions to show up on a remote notification. I have seen several tutorials on making it happen, and followed them, still no actions show up. The end goal is to have a "Reply" action where you can type in a response and send a reply through the notification, much like the standard text message notification.
I couldn't get that to show up, so I decided to add a second action, that is just a basic action, neither are showing up. I'll post up my sendNotification method, hopefully someone can see what I am doing wrong...
private void sendNotification(String body, String id) {
Intent i;
PendingIntent pi;
if(user.role.equals(Strings.roleMember) { //This is just to separate logic
i = new Intent(this, NotificationReceiver.class);
pi = PendingIntent.getBroadcast(this, 0, i, 0);
} else {
i = new Intent(this, AgentChatActivity.class);
pi = PendingIntent.getActivity(this, 0, i, 0);
}
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra(Strings.chatID, id);
RemoteInput input = new RemoteInput.Builder(Strings.textReply).setLabel("Reply").build();
NotificationCompat.Action replyAction = new NotificationCompat.Action.Builder(android.R.drawable.ic_dialog_info, "Reply", pi)
.addRemoteInput(input).build();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, Strings.channelID)
.setSmallIcon(R.drawable.ic_stat_notify)
.setContentTitle("New Message")
.setContentText(body)
.setAutoCancel(true)
.setCategory(NotificationCompat.CATEGORY_MESSAGE) //I have tried without this too
.setPriority(NotificationCompat.PRIORITY_MAX)
.addAction(replyAction)
.addAction(android.R.drawable.ic_dialog_alert, "reply", pi); //this is the second I added just to test
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(Strings.channelID, "General Messages", NotificationManager.IMPORTANCE_HIGH);
manager.createNotificationChannel(channel);
}
manager.notify(0, builder.build());
}
According to everything I have seen online, that should have actions attached to the notification, however I run it in the emulator (Pixel 6 API 27) and the notification shows up, but no actions are on it. I have tried without the second action, I have tried with only the second action, nothing shows up, just the basic notification. My build settings are set to minSdk 21, compileSdk and targetSdk 33. I would be more than happy to post any more code or anything else anyone needs, I can't figure it out. Thank you.

Canceling Notififcations Android Java

I started working with Notifications and to improve user expirirence I added button to notification "MARK AS READ", so when it is pressed - message will hide;
I had a few problems, in database each message has "read" field which is boolean, in method onDataChange() I loop and check if any of messages have this field as false if they have I call makeNotification(), looks fine, where is problem??? - e.x. I have 2 notifications, I cancel one(the app goes to database and change field to true, so now onDataChange() will be called), but another isn't cancelled and because onDataChange() was called now I have same notification appeared twice.....
I tried to fix this by building array of messages that are read and already displayed, not the best solution I think, but even previous problem was somehow solved this is a wierd one.
When notitifcation appears in notification list it has button "MARK AS READ", when I press it notififcation hides, but only if I start from the highest to the lower ones, so if I press second notification first foldes, if I press third first folds..
In onCreate() counter is set to 1;
public void makeNotification()
{
Intent activityIntent = new Intent(this, Notifications.class);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, activityIntent, 0);
Intent broadcastIntent = new Intent(getApplicationContext(), NotifUpdater.class);
broadcastIntent.putExtra("seconds", message.getSeconds());
broadcastIntent.putExtra("id", String.valueOf(counter));
PendingIntent actionIntent = PendingIntent.getBroadcast(this, 0,
broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(this, App.CHANNEL_ID)
.setVibrate(new long[]{100, 0, 100})
.setSmallIcon(R.drawable.ic_shield)
.setContentTitle(message.getTime() + " | " + message.getClient())
.setContentText(message.getMessage())
.setContentIntent(contentIntent)
.setAutoCancel(true)
.setOnlyAlertOnce(true)
.addAction(0, "MARK AS READ", actionIntent)
.build();
notificationManager.notify(counter, notification);
counter += 1;
}
// Broadcast which is triggered by pressing button
public class NotifUpdater extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
FirebaseDatabase database = FirebaseDatabase.getInstance();
String seconds = intent.getStringExtra("seconds");
int id = Integer.parseInt(intent.getStringExtra("id"));
database.getReference("latest").child(seconds).child("read").setValue(true);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
notificationManager.cancel(id);
}
}
You should have : FLAG_CANCEL_CURRENT
like :
PendingIntent actionIntent = PendingIntent.getBroadcast(this, NOTIFICATION_ID, buttonIntent, PendingIntent.FLAG_CANCEL_CURRENT);
not FLAG_UPDATE_CURRENT
Regarding your first issue, I'd just set an indicator for each notification, "wasShown" or something of the sort that is only set to true once you have showed the notification and when ever you are iterating your notifications, only show those who has "wasShown" set to false.
Regarding your 2nd problem, I'm not familiar with FirebaseDatabase but it sounds like its an ID problem. Did you make sure the ID you are getting from the intent is the right one?

Notification passes old Intent Extras when the Activity that read the extras values is on top

Notification is giving old values.
I read the stackoverflow link but still not working for me:
Notification passes old Intent Extras
I have a Activity A.
When I am on the activity B and touch the Notification, the Extra parameter is given correctly and shows the Activity A with the correctc values read with getExtras(..);
Then the Activity A is still on the top - showing on the screen:
I click on the second notification with new values of putExtras(newValue) to create a new activity A but with new values.
The problem:
The intent.getExtras()` is reading old values of the first notification clicked instead new values given by the second notification.
I made a lot of combinations of Flags of Pending Intent and the combinations of the link on top, but the aplication is still taking the old values(the values of the first touched notification) for the second Notification. I tried the flag: PendingIntent.FLAG_UPDATE_CURRENT to update the values instead create a new one Activity and some others Flags.
How can I make the second notification give the correct values for the activity A when the Activity A is still shown on the screen?
The snippet of the code creating the notification.
public void notificationCreateGu(String newMessageUserUidOfSender) {
Intent it = new Intent(this,ActivityA.class);
it.putExtra(USER_UID_READER,newMessageUserUidOfSender);
StoreValuesClass.count=StoreValuesClass.count+2;
PendingIntent pi = PendingIntent.getActivity(this, StoreValuesClass.count,it, 0);
Notification notification = new NotificationCompat.Builder(this)
.setTicker(newMessageUserUidOfSender )
.setSmallIcon(android.R.mipmap.sym_def_app_icon)
.setContentTitle("Title Message ")
.setContentText(String.valueOf(newMessageUserUidOfSender))
.setContentIntent(pi)
.setAutoCancel(true)
.build();
int m;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
m= StoreValuesClass.count=StoreValuesClass.count+2;
notificationManager.notify((m), notification);
}
//StoreValueClass.count is a static values that can be read by the activity to give an unique id for the notification.
the snippet of code reading the values.
userUidReader = getIntent().getExtras().getString(USER_UID_READER)
I tried to reload the values into onResume() but into onResume() the still taking the old values of the first time the getExtras() is read.
i understood that the operational system Android are not creating a new Activity but only giving it to the top.
using the answser of CommonsWare that helped with the override onNewIntetn and the link:
http://www.helloandroid.com/tutorials/communicating-between-running-activities
1 into xmlFile: put th android:launchMode="singleTask"
for the activity will receive the extra parameters with getExtras.
<activity android:name=".ActivityWillReceiveWithGetExtras"
android:launchMode="singleTask"
android:taskAffinity=""
android:excludeFromRecents="true">
</activity>
2.into the activity you that will receive the values with get_extras(...) override a method called onNewIntent:
2.1 Observation: put the line:
setIntent(intent);
to set the identifier of the intent.
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);//must store the new intent unless getIntent() will return the old one
getExtraParameterActual();
}
2.2 get the Extra parameters into a function that will have inside the command getExtras(...
getExtraParameterActual();
Write the function of the top getExtraParameterActual();
private void getExtraParameterActual() {
Intent intent = getIntent();//take back the value set with //setintenT of pass 2.1
user = getIntent().getExtras().getString(USER);//
}
5.
into OnCreate() call the e getExtraParameterActual();
and if necessary reload your views with a method for example reloadMyViews()
into onResume() reload your views again with the same function of the pass 5 reloadMyViews()
7 the notificatio code I used take care with the FLAGS
public void notificationCreateGu(String User) {
Log.d(TAG,nameOfTheService+"BUG createnotification for received CHAT messages useruidOfTheFriendNear="+newMessageUserUidOfSender);
Intent it = new Intent(this,ActivityWillReceiveWithGetExtras.class);
it.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
it.putExtra(USER,user);
StoreValuesClass.count=StoreValuesClass.count+2;
PendingIntent pi = PendingIntent.getActivity(this, StoreValuesClass.count,it, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(this)
.setTicker(newMessageUserUidOfSender )
.setSmallIcon(android.R.mipmap.sym_def_app_icon)
.setContentTitle("Title Message ")
.setContentText(String.valueOf(newMessageUserUidOfSender))
.setContentIntent(pi)
.setAutoCancel(true)
.build();
int m;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
m= StoreValuesClass.count=StoreValuesClass.count+2;
notificationManager.notify((m), notification);
}
Override onNewIntent() in your activity.
getIntent() returns the Intent that was used to initially create the activity. If an existing activity instance is brought back to the foreground via a startActivity() call, onNewIntent() is called to deliver to you the Intent used for that most recent startActivity() call.

How to show android GCM notification having single notification Id?

I want to show news GCm notification.
After receiving multiple message i want to show like 3 unread msg,4 unread msg.
Please help how can i do
(sorry for bad english)
Just count how many unread messages you have, build a notification and
use the [a NotificationManager.notify](http://developer.android.com/reference/android/app/NotificationManager.html#notify(int, android.app.Notification)) there are two versions one that accepts a notification id and the other accepts a tag and an id,
for both methods if there is already a notification with the same id or with the (id,tag) any previous notification is replaced by the new one.
Take a look into a this,
to see the available styles and patterns and use the one appropriated to your case, here's an example of how to do a Big Text or Inbox Style notification based on the number of messages
public static void updateOrSendNotification(Context context, String[] messages) {
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
int count = messages.length;
if (messages.length == 0) {
notificationManager.cancel(NOTIFICATION_ID);
return;
}
//Intent to be launched on notification click
Intent intent = new Intent(Intent.ACTION_VIEW,
null,
context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
int requestID = (int) System.currentTimeMillis();
PendingIntent contentIntent = PendingIntent.getActivity(context, requestID,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
String ticker = context.getString(R.string.new_notification_ticker);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context);
String contentTitle = context.getString(R.string.notification_text_style_title, messages.length);
mBuilder.setSmallIcon(R.drawable.ic_stat_notification)
.setTicker(ticker) // the thicker is the message that appears on the status bar when the notification first appears
.setDefaults(Notification.DEFAULT_ALL) // use defaults for various notification settings
.setContentIntent(contentIntent) // intent used on click
.setAutoCancel(true) // if you want the notification to be dismissed when clicked
.setOnlyAlertOnce(true); // don't play any sound or flash light if since we're updating
NotificationCompat.Style style;
if (count > 1) {
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
style = inboxStyle;
mBuilder.setContentTitle(contentTitle);
for (String r : messages) {
inboxStyle.addLine(r);
}
} else {
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
style = bigTextStyle;
bigTextStyle.setBigContentTitle(messages[0].substring(0, 10).concat(" ..."));
bigTextStyle.bigText(messages[0]);
}
mBuilder.setStyle(style);
notificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
This method assumes you call it every time with all the new messages your app has received,
since the NOTIFICATION_ID is always the same any previous notification will be replaced by the new one,
You can test it like this
String[] messages = {
"hi john how are you ?",
"john you never told me if you'r doing ok !",
"john lets go out"
};
updateOrSendNotification(this, messages);
From your question I assume you are trying to show stacked notifications (updating the current notifications)
Here is a detailed way of managing and updating notifications: http://developer.android.com/guide/topics/ui/notifiers/notifications.html#Managing
Multiple notifications:
How to display multiple notifications in android

Notification Intents not working for Android application

My app is running a service that collects feeds. When it find these feeds it create notations (unsuccessfully). I use a method call like this:
doNotification(date,"New Article",title,link,content,description,false);
for articles and this:
doNotification(date,"New Video",title,link,"","",true);
for videos. The method is this:
public void doNotification(Date date,String title,String subtext,String url,String body,String dateString,boolean video){
long time = date.getTime();
if(time > feedGetter.lastFeed){
//New feed, do notification
NotificationManager mNotificationManager = (NotificationManager) feedGetter.service.getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.notification_icon;
Notification notification = new Notification(icon, title + ": " + subtext, time);
Intent notificationIntent = new Intent(feedGetter.service, NotificationActivity.class);
notificationIntent.putExtra("url",url);
notificationIntent.putExtra("video",video);
if(!video){
notificationIntent.putExtra("body",body);
notificationIntent.putExtra("date",dateString);
notificationIntent.putExtra("title",subtext);
}
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(feedGetter.service, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
notification.setLatestEventInfo(feedGetter.service.getApplicationContext(), title, subtext, contentIntent);
mNotificationManager.cancel(video? 1 : 0);
mNotificationManager.notify(video? 1 : 0, notification);
//Update new time if necessary.
if(time > feedGetter.newTime){
feedGetter.newTime = time; //New time will be the time for this feed as it is the latest so far
}
}
}
As you see I add some data to the intent so I can handle the notifications correctly. The notifications are assigned to an ID for videos or an ID for articles and should replace the previous notifications. Here is the NotificationActivity that handles the notifications:
public class NotificationActivity extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Debug.out("Notification Activity");
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if(getIntent().getBooleanExtra("video", true)){
//Handle video notification
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(getIntent().getStringExtra("url")));
startActivity(browserIntent);
mNotificationManager.cancel(1);
}else{
//Start application UI and move to article
Intent intent = new Intent(this,TheLibertyPortalActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("url", getIntent().getStringExtra("url"));
intent.putExtra("body", getIntent().getStringExtra("body"));
intent.putExtra("date", getIntent().getStringExtra("date"));
intent.putExtra("title", getIntent().getStringExtra("title"));
startActivity(intent);
mNotificationManager.cancel(0);
}
finish();
}
}
So it's supposed to activate a URL for the videos and restart the application for articles with some data for handling the articles so the article is displayed to the user.
Seems simple enough but it doesn't work. The notifications display and they replace each other on the notification menu, showing the latest notifications for videos and articles but when I click on them they go wrong. I try to click on the article notification and it thinks it is a video and loads one of the videos. I go back onto the notification menu and the video notification has disappeared even when I clicked on the article notification. I try clicking on the article notification and nothing happens. It literally closes the menu and doesn't nothing and the notification remains in the menu doing nothing.
Thank you for any help with this. I am targeting the Google APIs level 14 API, with a min SDK version of level 8, trying with a 2.2.1 Android tablet.
The problem is with the Pending intent. Even though the docs say the requestCode is not used, it is. You must pass a unique integer for each PendingIntent. That worked!

Categories