How to give counter if more than one Notifications are there - android

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);

Related

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());

Only play sound once for duplicate Android notification

Is there a way to only play the notification sound once for a duplicate notification that comes through?
Sometimes our users accidentally send more then one of the same notification and on the second one would not like the annoying sound to occur for a duplicate notification in Android. I can't find a setting that handles this.
I don't think there is a function to handle this on his own but you could write it if you have unique ID for each notification(so that a duplicate would have the same id).
I'll explain better: when you receive a new message in your onMessageReceived() function you check and see if a notification with the same ID is present. If so the new one is a duplicate otherwise it is a new one for real.
Here a possible way of checking the notification Is it possible to check if a notification is visible or canceled?
Assuming you know to identify what is a "duplicate notification" this code should do the trick:
private Notification buildNotification(Context context, int icon, String title, String message, boolean isDuplicate) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(icon)
.setContentTitle(title)
.setContentText(message)
// Decide if you want a sound or not based on whether it is duplicate
.setSound(isDuplicate ? null : RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
return builder.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!!

Update Inbox style Notification Like gmail

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());

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.

Categories

Resources