Notifications only work on certain versions of android

121 Views Asked by At

My app uses a receiver to send the user notifications after a certain amount of time. The receiver works great as it runs a few functions, the notification however doesn't work so smoothly.

On the emulator (API29 and Android 10) it sends them correctly however when I install it on real devices it either doesn't work at all or works perfectly fine.

My phone had the notifications perfectly until when I updated it to android 12, from then on no notifications are fired. I also tested it on an older device (Android 7) and again it doesn't work.

I read into it and don't really understand how the channels work, so I think the issue might be there however I find it weird how it would then still work on some devices/emulators.

Here is my code:

class MyReceiver: BroadcastReceiver() {

    @RequiresApi(Build.VERSION_CODES.O)
    override fun onReceive(context: Context, intent: Intent) {

        val notificationChannel =
          NotificationChannel("My Channel", "New Quote", 
          NotificationManager.IMPORTANCE_DEFAULT).apply {
            description = "Alerts when A new daily quote is set!"
          }

        val titles = arrayOf(
          "Become inspired!",
          "Check out this quote!",
          "A new quote appeared!",
          "Daily quote available!"
        )
        val title = titles.random()

        val i = Intent(context, Qinperation::class.java)

        val builder = NotificationCompat.Builder(context, "My Channel")
          .setSmallIcon(R.drawable.ic_stat_name)
          .setContentTitle(title)
          .setContentText("A new daily quote is available for viewing")
          .setContentIntent(
            PendingIntent.getActivity(
              context,
              0,
              i,
              PendingIntent.FLAG_UPDATE_CURRENT
            )
          );

        with(NotificationManagerCompat.from(context)) {
          createNotificationChannel(notificationChannel)
          notify(1, builder.build())
        }
    }
}

All help is appreciated :)

2

There are 2 best solutions below

0
On BEST ANSWER

This was my bad. Turns out that on certain devices (like the one I was testing on), Doesn't support the pending intent I was using to fire the notification. If people have a similar issue, try changing Pending Intents to - PendingIntent.FLAG_IMMUTABLE as this works on more devices, Especially newer ones!

0
On

Channels are required for android version O and above. You need to create the channel(s) before you receive a notification. Channels basically are like a "switch" that can be turned off and on from the settings of the app and you configure your notifications, this means you can configure if you receive a notification if it should vibrate etc.

Before android version O you don't require a channel and the configuration of the notification is happening from the notificationBuilder.

So if you want to configure your notifications for above and bellow version O you have to do it on both places.

Now regarding your problem I think, you create to late the channel and call this method in your first activity or in the application class :

private fun createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    val name = getString(R.string.channel_name)
    val descriptionText = getString(R.string.channel_description)
    val importance = NotificationManager.IMPORTANCE_DEFAULT
    val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
        description = descriptionText
    }
    // Register the channel with the system
    val notificationManager: NotificationManager =
        getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    notificationManager.createNotificationChannel(channel)
   }
}

And then in your receiver you need only to create your notification and finally call this:

with(NotificationManagerCompat.from(this)) {
            // notificationId is a unique int for each notification that you must define
            notify(notificationId, builder.build())
        }

In case this doesn't work you might want to add a log to see if your receiver is indeed called.