MathWellan
MathWellan

Reputation: 43

Add a custom notification led light

I have a phone under Android 8 and I begin Android programmation with Android Studio. Some apps like Snapchat or Facebook makes my phone light a led with their custom colors (yellow & blue) when a notification comes.

I want to do the same with my app, I searched a lot and nothing works, the notification appears but not the white light. I checked my phone settings and my app is allowed to light the led.

public class Notification
{
    public Notification(String channelId, String channelName, Context context, String title, String body, Intent intent)
    {
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

            int notificationId = 1;
            int importance = NotificationManager.IMPORTANCE_HIGH;

            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
            {
                 NotificationChannel mChannel = new 
                 NotificationChannel(channelId, channelName, importance);
                 notificationManager.createNotificationChannel(mChannel);
            }

            NotificationCompat.Builder nbuilder = new NotificationCompat.Builder(context, channelId)
                     .setLights(Color.WHITE, 500, 100) // doesn't work
                     .setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 }) // doesn't work
                     .setContentTitle(title)
                     .setContentText(body);

            TaskStackBuilder stackBuilder = 
            TaskStackBuilder.create(context);
            stackBuilder.addNextIntent(intent);
            PendingIntent resultPendingIntent = 
            stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
            nbuilder.setContentIntent(resultPendingIntent);

            notificationManager.notify(notificationId, nbuilder.build());
    }
}

I tried calling the setLights() and setVibrate() after setContentText() and I tried calling these voids just before the notify() but it didn't changed anything.

I instanciate my class in the onCreate() :

new Notification("channel-01", "MathWellan Notification", this, "Twitter", "Follow me on Twitter !", new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?screen_name=MathWellan")));

Sorry for my bad english, I'm french and I hope you can help me ! Thanks by advance :)

Upvotes: 2

Views: 886

Answers (1)

FlyingNades
FlyingNades

Reputation: 452

Use this line of code to change notification LED color: your_notification.ledARGB = Color.YOUR_COLOR;

Usage example:

NotificationManager notif = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notif.cancel(1); // clear previous notification 
    final Notification notification = new Notification();

    notification.ledARGB = Color.MAGENTA;

    notification.ledOnMS = 1000;
    notification.flags |= Notification.FLAG_SHOW_LIGHTS;
    notif.notify(1, notification);

Note : screen should be locked when you test it , because notification LED will be highlighted only when screen is off. Maybe that's your problem.

Upvotes: 1

Related Questions