I’ve recently been coding up the notifications part of an in house app we run here and I forgot just how integral to any mobile application these are. Android should really be credited as creating the notification as we know it today and it’s also particularly nice and easy to create really great notifications for your apps so I thought i’d write a quick start guide to get you going!
Firstly we need to understand that there are any different versions of Android right now, all of which handle notifications slightly differently, add to that your device might also handle things a little differently too. I’m writing based on 4.3 to 5.x versions here and the core of this code will set you up, you just might have to explore the Android API to get things working 100% on your device.
So let’s get started with a brief explanation of what a notification is. Notifications are used within any app on your device to let you know when something from that app might need your attention, especially when it might not be running in the foreground with your full attention. Email is a great example, you check your email, however you don’t have it open constantly on your smartphone, rather you leave it running in the background or even switch it off and it will tell you if there’s any new mail. This is all done via the notifications classes of Android.
Notifications come in all shapes an sizes. Firstly there’s the classic full notification that you see on your notifications drawer or that flashes up for the top toolbar. This is viewed for the lock screen of the device if yours is set up to view them that way and also on the notification tray that you pull down for the top of the device.
Secondly there’s the ticker notification. This runs at the top of the devices screen when the notification fires and can be used to give you a little more info about the app.
Lastly there’s the notification button that you see on the icon of the app. However this is not really a notification as it’s handled differently and is not in the scope of this article.
Making the notification is pretty easy and you can use it anywhere in your app. Below is the basic code to get you started that I wrote within an activity on create().
Intent notificationIntent = new Intent(context, MainActivity.class); notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); Notification notification = new NotificationCompat.Builder(context) .setTicker(rsTicker) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle(rsTitle) .setContentText(rsText) .setContentIntent(pendingIntent) .setAutoCancel(true) .build(); NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(mnNotificationID, notification); mnNotificationID++;
So to explain the above, we firstly create a new intent, this looks a lot like the intent you call when changing Activity in an app and in this case it allows us to add some flags to the intent. These are flags that tell the notification system a few details about the notification we’re creating and how the resultant behaviour of it will be. In this case:
FLAG_ACTIVITY_CLEAR_TOP, FLAG_ACTIVITY_SINGLE_TOP & FLAG_ACTIVITY_NEW_TASK all handle instances of the app already running or not and how you want the resultant activity to behave. Go and check out all the different options you have from here.
Next we need a PendingIntent Type and this is to handle if we want to be able to click straight into our app from the notification. You can see we use the pendingItnent variable in the .setContentIntent parameter of the notification below. You don’t need this part if you don;t want the user to be able to click form the notification straight into your app.
We then create a Notification object and this is where start to string together all the main attributes for notification. You can see that setTicker is set here, the icon, title and main text of the notification and when we’;re happy with all that we can call the .build method of this object to set it.
Lastly we bring it all together by using a NotificationManager object to register the notification into the Notification Service. THe last thing to remember here is that every notification needs a unique ID and this is where the mnNotificationID is used. This is just an integer that I clock round and seems to work for me. You can use a random number generator or a more structured way to do this, as long as it’s unique to the notifications already live.
Remember if you want to move this to a class or fragment then you need to find the right Context but that’s all.
So there we have it, a quick guide to get you started with Notifications.