0

We are developing an application in which we are using Google cloud messaging system. We have implemented GCM successfully. Now we have to implement badge counter asynchronously on our application's dashboard. We are getting notifications as per device ID. How can I implement badge counter increment when notifications receives from my application. Can I use notification receives for counter increment?

If someone has achieved using same scenario then please share sample code.So we can implement.

Thanks,

Prashanth

Prashant
  • 1,351
  • 3
  • 20
  • 32
  • simply when you send the notification to that user, in your code make database table or pref to store a single number whenever a device receives the new notification increase it by 1? is it that simple? – Kosh Dec 18 '14 at 15:13

1 Answers1

2

Have a look at can we show badge number on android app icon like iphone? for implementing the badge on the app icon. There is an open source library mentioned there that shows the badges on various devices.

The actual library is https://github.com/leolin310148/ShortcutBadger

With regards to getting an accurate counter, it's a bit more complex since some notifications can go missing.

  • If you don't care about that (notifications missing), it's simple enough. Every time there's a notification, in your service, increment some counter using shared preferences/database row (or whatever else where you can persist a number) and use that number to show the badge. Every time the user opens the app, clear that value and clear the badge.
  • If you do however care about missing notifications, you need to implement the mechanism on your server. Basically, you want to send an acknowledgement request every time the content is seen and when a new notification arrives, ask the server to tell you how many "unseen" contents there are (you call the server in the same service your GCM is using) and use that number as a badge.

The second solution assumes that (a) you have a server that's serving contents and (b) the server can determine when something is seen/new. If you don't have such a service, option 1 is probably the only way you can go about doing this.

Community
  • 1
  • 1
kha
  • 19,123
  • 9
  • 34
  • 67