Animate Android Notification Icon

okuzawats
okuzawats

You can animate your Android app’s notification icon. To animate the notification icon, you have to define animation-list in drawable directory like this. As you know, each icon image should be alpha-only.

<?xml version="1.0" encoding="utf-8"?>
<animation-list
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item android:drawable="@drawable/awesome_notification_icon_1" android:duration="200" />
    <item android:drawable="@drawable/awesome_notification_icon_2" android:duration="200" />
    <item android:drawable="@drawable/awesome_notification_icon_3" android:duration="200" />
    <item android:drawable="@drawable/awesome_notification_icon_4" android:duration="200" />
</animation-list>

Once you created an animation-list file, you can set it as small icon of notifications. See the part of setSmallIcon(R.drawable.awesome_animation).

package com.example.app

import android.app.NotificationChannel
import android.app.NotificationManager
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationCompat
import androidx.core.content.getSystemService

private const val NOTIFICATION_CHANNEL_ID = "awesome channel id"
private const val NOTIFICATION_CHANNEL_NAME = "awesome channel name"
private const val NOTIFICATION_ID = 1001

class MainActivity : AppCompatActivity(R.layout.activity_main) {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // get notification manager with ktx
        val manager = requireNotNull(
            getSystemService<NotificationManager>()
        )

        // create notification channel
        val channel = NotificationChannel(
            NOTIFICATION_CHANNEL_ID,
            NOTIFICATION_CHANNEL_NAME,
            NotificationManager.IMPORTANCE_DEFAULT
        )
        manager.createNotificationChannel(channel)

        // create notification
        val notification = NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
            .setSmallIcon(R.drawable.awesome_animation)
            .setContentTitle("awesome notification title")
            .setContentText("awesome notification text")
            .build()
        manager.notify(NOTIFICATION_ID, notification)
    }
}

This would make your notification icon animated!