Sunday, 29 January 2017

Why do my Android Notification only appear in the status bar?

I'm definitely getting back into Android development, I'm remembering that feeling of 'Surely this should be easier than this!'. All I wanted to do was to schedule a local notification which behaved similar to a push notification pop-up. That is, as well as showing the small icon in the status bar I wanted it to pop up on screen to notify the end user. All seems fairly easily, I found this code for how to schedule a notification. That all worked perfectly, apart from the notification would only appear in the status bar. Searching around I found loads of different answers / solutions, mostly all saying the same thing:

  • It only worked if you used 'NotificationCompat.Builder' in place of 'Notification.Builder', or
  • You had to set the priority to 'NotificationCompat.PRIORITY_HIGH'

As usually happens, none of these solutions worked for me until I added in the missing piece of the jigsaw:- '.setDefaults(Notification.DEFAULT_ALL)'. For me this fixed the problem, the scheduled notification began to pop up on the desktop as expected! Looking into the Android documentation for this method nothing seemed to make sense as to why it would change the behaviour of the notification. As the documentation made reference to default sounds I replaced the call ".setDefaults(...)" with ".setSound(...)" and tried again. Again, the notification behaved as desired and appeared as a pop-up. Not making calls to either of these methods resulted in the notification reverted back the undesired behaviour of only appearing in the status bar. Adding either or both of these calls back in caused the desired behaviour. I'm still not completely sure why at the moment, but adding a sound to the scheduled notification sorted the problem out for me.

These 'pop-up' notifications are called “Heads-up” notifications and were introduced with Lollipop (v5.0).

No comments:

Post a Comment