Update Inbox style Notification Like gmail - android

I want to use inbox style notification, and once notification is still showing in notification status bar then it should append to the existing notification like gmail.
But I don't know how to detect that notification is showing in the status bar, Is there any way to get the notification id
Is ther any way to know that notification generated by my application is already displayed and just update it with +1 more(Inbox style)
What I thought :-
I thought I can store the notification id in shared prefrences and I will pass the pending intent which will start a intent service which will clear the notification is stored in shared prefrences and during notification posting I will check the notification id in prefrences If it is not cleared then I will update it
Does any one have any better idea ?

Do not save it in the preferences. Just use a constant value as the Notification ID.
static final int MY_NOTIFICATION_ID = 1;
As the ID's are unique per application you can use the number you want. Then use it when notifying the NotifiactionManager. Use the same code to update your notifiaction.
NotificationManager.notify(MY_NOTIFICATION_ID , notification);

I think you should have everything on this link: http://developer.android.com/training/notify-user/managing.html
It like you said, you need to know the notification id in order to update it. Using Shared Preferences is an easy way to do it since it only a few lines of code to do everything,
Your idea good, clear the preferences file when the user clicks on the notification.

you can use the static variables to keep the track of notification ID and for appending the notification i mean for stacked notification also you can keep a number in static variables...
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(notificationTitle).setContentText(contentText);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle(notificationTitle + " Details");
// Moves events into the big view
for (int i = 0; i < extrasList.size(); i++) {
inboxStyle.addLine(extrasList.get(i).getString(mString));
}
if (number >= 8) {
inboxStyle.setSummaryText("+" + (number - 7) + " more reply(s)");
} else {
inboxStyle.setSummaryText(contentText);
}
mBuilder.setStyle(inboxStyle);
mBuilder.setNumber(number);
mBuilder.setContentIntent(contentIntent);
mBuilder.setAutoCancel(true);
mNotificationManager.notify(Integer.parseInt(type), mBuilder.build());

Related

Android notification append content on update

I have made an android app (Android Studio / Java) that checks a website for content and stores it in an sqlite DB. If new content is fetched that is not stored in DB, it shows a notification with the new content for user to notice.
That's working fine, although if user does not read/open/dismiss that notification, the next notification will update current one and replace its content with new data. This is wanted behavior, because I don't want the user to receive many notifications for the same thing, so I'm using the same notification id.
This introduces a problem though, if user checks the notification now, he will see the second fetched data, but won't be aware of the existence of the first fetched data.
So, what I'm trying to do is to append to the notification's content, so that both first and second fetched data are shown.
I tried the "inboxStyle" notifications that allow for new lines to be added, but it seems to be working only for setting many lines at the time notification is created and not for appending lines to an existing notifications.
I know that I can do that by storing what user has seen and what not, whether a notification was opened, etc, but this seems too much hassle for a simple thing, there must be an easier way to achieve it.
The expected behavior would be to either be able to append the message of existing notifications, or be able to fetch the message of an existing notification (by id) and then manually append to it and push the updated notification.
If that's not clear enough, the expected outcome is:
Issue the first notification with message "Test message 1"
Issue second notification using the same notification-id with message "Test message 2" that would NOT overwrite "Test message 1" but rather keep that message and append to it, so that the notification's message would now be "Test message 1 {newline-here} Test message 2" (or even better reversed so that the last message is shown on top).
Thank you in advance!
I was looking for a solution myself. I managed to do something that works but I'm quite sure there are other elegant solutions : I'm checking whether or not similar notification has been displayed. If so, I get the previous content and append it to the new notification content before publishing.
First, make sure that the notifications you want to assemble have the same uniqueID
This way, when you receive your 2nd, 3rd notification, you can easily match the one you are creating with the ones already displayed.
Note : This code works only on API 23 and higher.
String message = null;
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
StatusBarNotification[] notifications = mNotificationManager.getActiveNotifications();
for (StatusBarNotification notification: notifications) {
if (notification.getId() == uniqueID) {
// You have a match
Bundle extras = notification.getNotification().extras;
message = extras.getCharSequence(Notification.EXTRA_TEXT).toString();
break;
}
}
}
The easy part : Now that you have the previous notification text, you have to append it to the new notification text before publishing
String newMessage = message + remoteMessage.getData.get("text");
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(),
channelID)
.setStyle(new NotificationCompat.BigTextStyle().bigText(newMessage))
.setContentTitle("Title")
.setContentText(notification.getMessage())
.setAutoCancel(true);

How to handle Android app receive Multiple fcm data message?

Send data message for app notification via FCM.
I have various FCM data types, and each type has its own action separate way.
Everything works fine, if I send only one message. App could handle exactly what I want.
But send FCM more than twice, (for example, [FCM - data for action 1] then [FCM - data for action 2] ) something goes wrong.
First, I want to show it separately, but second one overlay the first one.
Second, set 'First question' aside, after I click the message that contains the second one, it works for [ action 1 ] that the first one aimed.
So... I want to solve these problems. Or at least one. ( if first one is solved, second solve naturally )
Thx in advance.
did you use NotificationManager for displaying the notifications?
Then try this
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify((int) System.currentTimeMillis(), notifBuilder.build());
see the
(int) System.currentTimeMillis()
that's how i make the unique id for each notifications.
Hope that helps, thanks
If you want notification seperate, you need define diffrent id for notificaton:
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(<Unique notification id here>, notifBuilder.build());

notifications not getting grouped

I an a noob in android, I am trying to show notification of the push notifications I receive. Every time I receive a push notification a new notification is created in the notification bar, even if an exisiting one is present. I want them to be grouped together.
This is what I am currently doing
private void generateNotification(Context context, String ticker, String title, String msg, int icon, Intent intent)
{
int notificationId = 1;
long when = System.currentTimeMillis();
int pendingNotificationsCount = AppName.getPendingNotificationsCount() + 1;
AppName.setPendingNotificationsCount(pendingNotificationsCount);
mNotifyBuilder = new NotificationCompat.Builder(this)
.setWhen(when)
.setContentTitle(title)
.setContentText(msg)
.setAutoCancel(true)
.setContentIntent(PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT))
.setNumber(pendingNotificationsCount);
//This prints the count correctly....
Log.d("Snehan", "Message built with Count "+pendingNotificationsCount);
Notification notif = mNotifyBuilder.build();
notif.defaults = Notification.DEFAULT_ALL;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationId, notif);
}
Am I doing something wrong here or missing something??
Seems Android updated the library since I last used it. But the logic is still the same. You need to save whatever the notification id was or at least give it a name you can track and check if it exists. More info can be found in the Android docs. Below is a snippet from what I mean.
To set up a notification so it can be updated, issue it with a notification ID by calling NotificationManager.notify(ID, notification). To update this notification once you've issued it, update or create a NotificationCompat.Builder object, build a Notification object from it, and issue the Notification with the same ID you used previously. If the previous notification is still visible, the system updates it from the contents of the Notification object. If the previous notification has been dismissed, a new notification is created instead.
The docs have everything you need so no need for me to write the code for you :) Hope that helped.
Edit:
Ok, so I recommend you adda a dummy icon just to see what that does. I also recommend instead of chaining all that stuff only chain the text stuff. This way you can debug a bit easier. Try to follow the doc a bit more closely. I don;t really see anything wrong with your code, but obviously something is causing the issue.
Edit 2
So it seems the icon was the problem. I've had this issue before, which is why I mentioned to add that explicitly. Hopefully when someone encounters issues with notifications please make sure you have an icon!!

Android push notification opening a custom page

my question for you is the following: I have a web app written in HTML5, wrapped as a native Android app in order to use Google Push Notifications. Because my app is using many notifications for different reasons, I want to be able to say each time a notification is received, which page to be open, like adding a 'href' in the notification intent. Is this possible?
If I wasn't clear enough please let me know.
Thanks
You can define your own notification message content. The Message builder from Google supports key value pairs to be set by the sender of the notification.
See http://developer.android.com/reference/com/google/android/gcm/server/Message.html
Example:
Message message = new Message.Builder()
.addData("link1", "http://mypage1.com")
.addData("link2", "http://mypage2.com")
.build();
When you create the notification, use setContentIntent() to attach an Intent that has been constructed to visit the right webpage:
// assuming <this> is an Activity or other Context
Intent urlIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(yourUrl));
PendingIntent urlPendingIntent = PendingIntent.getActivity(this, 0 urlIntent, 0);
Notification.Builder b = new Notification.Builder(this)
.setSmallIcon(...).setContentTitle(...).setContentText(...) // etc.
.setContentIntent(urlPendingIntent);
NotificationManager noMan
= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
noMan.notify(ID, b.build());
If you expect to have more than one of these in the notification panel at a time:
Reconsider. It's spammy to post more than one notification.
If you must, you'll need a separate ID (or separate tag) for each.

How to give counter if more than one Notifications are there

I have implemented PushNotification Using C2dm. I am getting notification from c2dm also. My problem is I want to give a counter when I get more than one notifications, I mean like "You have a Notification(count)". How can I implement this.
you can do to set the number value into the Notification object
Notification notifyDetails = new Notification(R.drawable.alarm,intent.getExtras().getString(KEY_TITLE),System.currentTimeMillis());
notifyDetails.number = 1; ////// here you can pass the counter value which will so you the number
here is the link
http://developer.android.com/reference/android/app/Notification.html#number
http://developer.android.com/guide/topics/ui/notifiers/notifications.html
Android Notification Bar Number
Are you looking for Notification#number?
NotificationManager notificationManager =(NotificationManager)context.getSystemService(NOTIFICATION_SERVICE);
notification.setNumber(1);
NotificationManager notificationManager=(NotificationManager)context.getSystemService(NOTIFICATION_SERVICE);
This creates the notificationManager class instance. Then you will have notification object with which you can do any adjustment. To set the number of the messages you have received, simply set this:
notification.setNumber(1);

Categories

Resources