Skip to content

Commit 4c0170c

Browse files
authored
feat(android): Add lights and lightColor to PushNotificationChannel (#2618)
1 parent c381202 commit 4c0170c

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

android/capacitor/src/main/java/com/getcapacitor/plugin/PushNotifications.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.content.ContentResolver;
77
import android.content.Context;
88
import android.content.Intent;
9+
import android.graphics.Color;
910
import android.media.AudioAttributes;
1011
import android.os.Build;
1112
import android.os.Bundle;
@@ -47,6 +48,8 @@ public class PushNotifications extends Plugin {
4748
public static String CHANNEL_IMPORTANCE = "importance";
4849
public static String CHANNEL_VISIBILITY = "visibility";
4950
public static String CHANNEL_SOUND = "sound";
51+
public static String CHANNEL_USE_LIGHTS = "lights";
52+
public static String CHANNEL_LIGHT_COLOR = "lightColor";
5053

5154
public static Bridge staticBridge = null;
5255
public static RemoteMessage lastMessage = null;
@@ -192,6 +195,8 @@ public void createChannel(PluginCall call) {
192195
channel.put(CHANNEL_VISIBILITY, call.getInt(CHANNEL_VISIBILITY, NotificationCompat.VISIBILITY_PUBLIC));
193196
channel.put(CHANNEL_IMPORTANCE, call.getInt(CHANNEL_IMPORTANCE));
194197
channel.put(CHANNEL_SOUND, call.getString(CHANNEL_SOUND, null));
198+
channel.put(CHANNEL_USE_LIGHTS, call.getBoolean(CHANNEL_USE_LIGHTS, false));
199+
channel.put(CHANNEL_LIGHT_COLOR, call.getString(CHANNEL_LIGHT_COLOR, null));
195200
createChannel(channel);
196201
call.success();
197202
} else {
@@ -223,6 +228,8 @@ public void listChannels(PluginCall call) {
223228
channel.put(CHANNEL_IMPORTANCE, notificationChannel.getImportance());
224229
channel.put(CHANNEL_VISIBILITY, notificationChannel.getLockscreenVisibility());
225230
channel.put(CHANNEL_SOUND, notificationChannel.getSound());
231+
channel.put(CHANNEL_USE_LIGHTS, notificationChannel.shouldShowLights());
232+
channel.put(CHANNEL_LIGHT_COLOR, String.format("#%06X", (0xFFFFFF & notificationChannel.getLightColor())));
226233
Log.d(getLogTag(), "visibility " + notificationChannel.getLockscreenVisibility());
227234
Log.d(getLogTag(), "importance " + notificationChannel.getImportance());
228235
channels.put(channel);
@@ -238,8 +245,17 @@ public void listChannels(PluginCall call) {
238245
private void createChannel(JSObject channel) {
239246
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
240247
NotificationChannel notificationChannelChannel = new NotificationChannel(channel.getString(CHANNEL_ID), channel.getString(CHANNEL_NAME), channel.getInteger(CHANNEL_IMPORTANCE));
241-
notificationChannelChannel.setDescription(channel.getString(CHANNEL_DESCRIPTION, ""));
242-
notificationChannelChannel.setLockscreenVisibility(channel.getInteger(CHANNEL_VISIBILITY, 0));
248+
notificationChannelChannel.setDescription(channel.getString(CHANNEL_DESCRIPTION));
249+
notificationChannelChannel.setLockscreenVisibility(channel.getInteger(CHANNEL_VISIBILITY));
250+
notificationChannelChannel.enableLights(channel.getBool(CHANNEL_USE_LIGHTS));
251+
String lightColor = channel.getString(CHANNEL_LIGHT_COLOR);
252+
if (lightColor != null) {
253+
try {
254+
notificationChannelChannel.setLightColor(Color.parseColor(lightColor));
255+
} catch (IllegalArgumentException ex) {
256+
Log.e(getLogTag(), "Invalid color provided for light color.");
257+
}
258+
}
243259
String sound = channel.getString(CHANNEL_SOUND, null);
244260
if (sound != null && !sound.isEmpty()) {
245261
AudioAttributes audioAttributes = new AudioAttributes.Builder()

core/src/core-plugin-definitions.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,10 +1530,12 @@ export interface PushNotificationDeliveredList {
15301530
export interface PushNotificationChannel {
15311531
id: string;
15321532
name: string;
1533-
description: string;
1534-
sound: string;
1533+
description?: string;
1534+
sound?: string;
15351535
importance: 1 | 2 | 3 | 4 | 5;
15361536
visibility?: -1 | 0 | 1 ;
1537+
lights?: boolean;
1538+
lightColor?: string;
15371539
}
15381540

15391541
export interface PushNotificationChannelList {

0 commit comments

Comments
 (0)