Ray Kytto
Ray Kytto

Reputation: 45

Android - LED Notification doesn't blink

I'm desperately trying to add a notification feature to my application making the notification LED blink. After all my attempts nothing works ...

Here's my code :

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
            .setLights(Color.BLUE, 200, 200)
            .setContentTitle(remoteMessage.getData().get("title"))
            .setContentText(remoteMessage.getData().get("body"))
            .setColor(getColor(R.color.buttonBlueInactive))
            .setSmallIcon(R.mipmap.ic_launcher);
    NotificationManagerCompat manager = NotificationManagerCompat.from(this);
    manager.notify(1, notification.build());
}

If anyone could help, it would be really appreciated.

SOLUTION

For those who have the same problem that I had, there is the solution :

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
            .setLights(Color.BLUE, 200, 200)
            // Add the line bellow
            .setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND | Notification.FLAG_SHOW_LIGHTS)
            .setContentTitle(remoteMessage.getData().get("title"))
            .setContentText(remoteMessage.getData().get("body"))
            .setColor(getColor(R.color.buttonBlueInactive))
            .setSmallIcon(R.mipmap.ic_launcher);
    NotificationManagerCompat manager = NotificationManagerCompat.from(this);
    manager.notify(1, notification.build());
}

Upvotes: 0

Views: 1129

Answers (1)

guipivoto
guipivoto

Reputation: 18677

Option 1

If I'm not wrong, you are missing setDefaults()

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
            .setLights(Color.BLUE, 200, 200)
            .setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND | Notification.FLAG_SHOW_LIGHTS) // Add this line
            .setContentTitle(remoteMessage.getData().get("title"))
            .setContentText(remoteMessage.getData().get("body"))
            .setColor(getColor(R.color.buttonBlueInactive))
            .setSmallIcon(R.mipmap.ic_launcher);
    NotificationManagerCompat manager = NotificationManagerCompat.from(this);
    manager.notify(1, notification.build());
}

In the example above, I added following options:

Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND | Notification.FLAG_SHOW_LIGHTS

You may want to change according to your need. More info:

Android DOC

Option 2

If example above does not work, try the code below:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
            .setLights(Color.BLUE, 200, 200)
            .setContentTitle(remoteMessage.getData().get("title"))
            .setContentText(remoteMessage.getData().get("body"))
            .setColor(getColor(R.color.buttonBlueInactive))
            .setSmallIcon(R.mipmap.ic_launcher);

    Notification builtNotification = notification.build();
    builtNotification.flags |= Notification.FLAG_SHOW_LIGHTS;

    NotificationManagerCompat manager = NotificationManagerCompat.from(this);
    manager.notify(1, builtNotification);
}

Upvotes: 1

Related Questions