66import android .content .Context ;
77import android .media .AudioAttributes ;
88import android .net .Uri ;
9- import android .os .Build ;
10- import android .provider .Settings ;
119import androidx .core .app .NotificationCompat ;
1210import com .getcapacitor .*;
1311import com .getcapacitor .util .WebColor ;
14- import java .util .Arrays ;
1512import java .util .List ;
1613
1714public class NotificationChannelManager {
1815
1916 private Context context ;
2017 private NotificationManager notificationManager ;
21- private PluginConfig config ;
2218
23- public NotificationChannelManager (Context context , NotificationManager manager , PluginConfig config ) {
19+ public NotificationChannelManager (Context context , NotificationManager manager ) {
2420 this .context = context ;
2521 this .notificationManager = manager ;
26- this .config = config ;
2722 }
2823
2924 private static String CHANNEL_ID = "id" ;
@@ -37,104 +32,90 @@ public NotificationChannelManager(Context context, NotificationManager manager,
3732 private static String CHANNEL_LIGHT_COLOR = "lightColor" ;
3833
3934 public void createChannel (PluginCall call ) {
40- if (android .os .Build .VERSION .SDK_INT >= android .os .Build .VERSION_CODES .O ) {
41- JSObject channel = new JSObject ();
42- if (call .getString (CHANNEL_ID ) != null ) {
43- channel .put (CHANNEL_ID , call .getString (CHANNEL_ID ));
44- } else {
45- call .reject ("Channel missing identifier" );
46- return ;
47- }
48- if (call .getString (CHANNEL_NAME ) != null ) {
49- channel .put (CHANNEL_NAME , call .getString (CHANNEL_NAME ));
50- } else {
51- call .reject ("Channel missing name" );
52- return ;
53- }
54-
55- channel .put (CHANNEL_IMPORTANCE , call .getInt (CHANNEL_IMPORTANCE , NotificationManager .IMPORTANCE_DEFAULT ));
56- channel .put (CHANNEL_DESCRIPTION , call .getString (CHANNEL_DESCRIPTION , "" ));
57- channel .put (CHANNEL_VISIBILITY , call .getInt (CHANNEL_VISIBILITY , NotificationCompat .VISIBILITY_PUBLIC ));
58- channel .put (CHANNEL_SOUND , call .getString (CHANNEL_SOUND , null ));
59- channel .put (CHANNEL_VIBRATE , call .getBoolean (CHANNEL_VIBRATE , false ));
60- channel .put (CHANNEL_USE_LIGHTS , call .getBoolean (CHANNEL_USE_LIGHTS , false ));
61- channel .put (CHANNEL_LIGHT_COLOR , call .getString (CHANNEL_LIGHT_COLOR , null ));
62- createChannel (channel );
63- call .resolve ();
35+ JSObject channel = new JSObject ();
36+ if (call .getString (CHANNEL_ID ) != null ) {
37+ channel .put (CHANNEL_ID , call .getString (CHANNEL_ID ));
6438 } else {
65- call .unavailable ();
39+ call .reject ("Channel missing identifier" );
40+ return ;
6641 }
42+ if (call .getString (CHANNEL_NAME ) != null ) {
43+ channel .put (CHANNEL_NAME , call .getString (CHANNEL_NAME ));
44+ } else {
45+ call .reject ("Channel missing name" );
46+ return ;
47+ }
48+
49+ channel .put (CHANNEL_IMPORTANCE , call .getInt (CHANNEL_IMPORTANCE , NotificationManager .IMPORTANCE_DEFAULT ));
50+ channel .put (CHANNEL_DESCRIPTION , call .getString (CHANNEL_DESCRIPTION , "" ));
51+ channel .put (CHANNEL_VISIBILITY , call .getInt (CHANNEL_VISIBILITY , NotificationCompat .VISIBILITY_PUBLIC ));
52+ channel .put (CHANNEL_SOUND , call .getString (CHANNEL_SOUND , null ));
53+ channel .put (CHANNEL_VIBRATE , call .getBoolean (CHANNEL_VIBRATE , false ));
54+ channel .put (CHANNEL_USE_LIGHTS , call .getBoolean (CHANNEL_USE_LIGHTS , false ));
55+ channel .put (CHANNEL_LIGHT_COLOR , call .getString (CHANNEL_LIGHT_COLOR , null ));
56+ createChannel (channel );
57+ call .resolve ();
6758 }
6859
6960 public void createChannel (JSObject channel ) {
70- if (android .os .Build .VERSION .SDK_INT >= android .os .Build .VERSION_CODES .O ) {
71- NotificationChannel notificationChannel = new NotificationChannel (
72- channel .getString (CHANNEL_ID ),
73- channel .getString (CHANNEL_NAME ),
74- channel .getInteger (CHANNEL_IMPORTANCE )
75- );
76- notificationChannel .setDescription (channel .getString (CHANNEL_DESCRIPTION ));
77- notificationChannel .setLockscreenVisibility (channel .getInteger (CHANNEL_VISIBILITY ));
78- notificationChannel .enableVibration (channel .getBool (CHANNEL_VIBRATE ));
79- notificationChannel .enableLights (channel .getBool (CHANNEL_USE_LIGHTS ));
80- String lightColor = channel .getString (CHANNEL_LIGHT_COLOR );
81- if (lightColor != null ) {
82- try {
83- notificationChannel .setLightColor (WebColor .parseColor (lightColor ));
84- } catch (IllegalArgumentException ex ) {
85- Logger .error (Logger .tags ("NotificationChannel" ), "Invalid color provided for light color." , null );
86- }
61+ NotificationChannel notificationChannel = new NotificationChannel (
62+ channel .getString (CHANNEL_ID ),
63+ channel .getString (CHANNEL_NAME ),
64+ channel .getInteger (CHANNEL_IMPORTANCE )
65+ );
66+ notificationChannel .setDescription (channel .getString (CHANNEL_DESCRIPTION ));
67+ notificationChannel .setLockscreenVisibility (channel .getInteger (CHANNEL_VISIBILITY ));
68+ notificationChannel .enableVibration (channel .getBool (CHANNEL_VIBRATE ));
69+ notificationChannel .enableLights (channel .getBool (CHANNEL_USE_LIGHTS ));
70+ String lightColor = channel .getString (CHANNEL_LIGHT_COLOR );
71+ if (lightColor != null ) {
72+ try {
73+ notificationChannel .setLightColor (WebColor .parseColor (lightColor ));
74+ } catch (IllegalArgumentException ex ) {
75+ Logger .error (Logger .tags ("NotificationChannel" ), "Invalid color provided for light color." , null );
8776 }
88- String sound = channel .getString (CHANNEL_SOUND , null );
89- if (sound != null && !sound .isEmpty ()) {
90- if (sound .contains ("." )) {
91- sound = sound .substring (0 , sound .lastIndexOf ('.' ));
92- }
93- AudioAttributes audioAttributes = new AudioAttributes .Builder ()
94- .setContentType (AudioAttributes .CONTENT_TYPE_SONIFICATION )
95- .setUsage (AudioAttributes .USAGE_NOTIFICATION )
96- .build ();
97- Uri soundUri = Uri .parse (ContentResolver .SCHEME_ANDROID_RESOURCE + "://" + context .getPackageName () + "/raw/" + sound );
98- notificationChannel .setSound (soundUri , audioAttributes );
77+ }
78+ String sound = channel .getString (CHANNEL_SOUND , null );
79+ if (sound != null && !sound .isEmpty ()) {
80+ if (sound .contains ("." )) {
81+ sound = sound .substring (0 , sound .lastIndexOf ('.' ));
9982 }
100- notificationManager .createNotificationChannel (notificationChannel );
83+ AudioAttributes audioAttributes = new AudioAttributes .Builder ()
84+ .setContentType (AudioAttributes .CONTENT_TYPE_SONIFICATION )
85+ .setUsage (AudioAttributes .USAGE_NOTIFICATION )
86+ .build ();
87+ Uri soundUri = Uri .parse (ContentResolver .SCHEME_ANDROID_RESOURCE + "://" + context .getPackageName () + "/raw/" + sound );
88+ notificationChannel .setSound (soundUri , audioAttributes );
10189 }
90+ notificationManager .createNotificationChannel (notificationChannel );
10291 }
10392
10493 public void deleteChannel (PluginCall call ) {
105- if (android .os .Build .VERSION .SDK_INT >= android .os .Build .VERSION_CODES .O ) {
106- String channelId = call .getString ("id" );
107- notificationManager .deleteNotificationChannel (channelId );
108- call .resolve ();
109- } else {
110- call .unavailable ();
111- }
94+ String channelId = call .getString ("id" );
95+ notificationManager .deleteNotificationChannel (channelId );
96+ call .resolve ();
11297 }
11398
11499 public void listChannels (PluginCall call ) {
115- if (android .os .Build .VERSION .SDK_INT >= android .os .Build .VERSION_CODES .O ) {
116- List <NotificationChannel > notificationChannels = notificationManager .getNotificationChannels ();
117- JSArray channels = new JSArray ();
118- for (NotificationChannel notificationChannel : notificationChannels ) {
119- JSObject channel = new JSObject ();
120- channel .put (CHANNEL_ID , notificationChannel .getId ());
121- channel .put (CHANNEL_NAME , notificationChannel .getName ());
122- channel .put (CHANNEL_DESCRIPTION , notificationChannel .getDescription ());
123- channel .put (CHANNEL_IMPORTANCE , notificationChannel .getImportance ());
124- channel .put (CHANNEL_VISIBILITY , notificationChannel .getLockscreenVisibility ());
125- channel .put (CHANNEL_SOUND , notificationChannel .getSound ());
126- channel .put (CHANNEL_VIBRATE , notificationChannel .shouldVibrate ());
127- channel .put (CHANNEL_USE_LIGHTS , notificationChannel .shouldShowLights ());
128- channel .put (CHANNEL_LIGHT_COLOR , String .format ("#%06X" , (0xFFFFFF & notificationChannel .getLightColor ())));
129- Logger .debug (Logger .tags ("NotificationChannel" ), "visibility " + notificationChannel .getLockscreenVisibility ());
130- Logger .debug (Logger .tags ("NotificationChannel" ), "importance " + notificationChannel .getImportance ());
131- channels .put (channel );
132- }
133- JSObject result = new JSObject ();
134- result .put ("channels" , channels );
135- call .resolve (result );
136- } else {
137- call .unavailable ();
100+ List <NotificationChannel > notificationChannels = notificationManager .getNotificationChannels ();
101+ JSArray channels = new JSArray ();
102+ for (NotificationChannel notificationChannel : notificationChannels ) {
103+ JSObject channel = new JSObject ();
104+ channel .put (CHANNEL_ID , notificationChannel .getId ());
105+ channel .put (CHANNEL_NAME , notificationChannel .getName ());
106+ channel .put (CHANNEL_DESCRIPTION , notificationChannel .getDescription ());
107+ channel .put (CHANNEL_IMPORTANCE , notificationChannel .getImportance ());
108+ channel .put (CHANNEL_VISIBILITY , notificationChannel .getLockscreenVisibility ());
109+ channel .put (CHANNEL_SOUND , notificationChannel .getSound ());
110+ channel .put (CHANNEL_VIBRATE , notificationChannel .shouldVibrate ());
111+ channel .put (CHANNEL_USE_LIGHTS , notificationChannel .shouldShowLights ());
112+ channel .put (CHANNEL_LIGHT_COLOR , String .format ("#%06X" , (0xFFFFFF & notificationChannel .getLightColor ())));
113+ Logger .debug (Logger .tags ("NotificationChannel" ), "visibility " + notificationChannel .getLockscreenVisibility ());
114+ Logger .debug (Logger .tags ("NotificationChannel" ), "importance " + notificationChannel .getImportance ());
115+ channels .put (channel );
138116 }
117+ JSObject result = new JSObject ();
118+ result .put ("channels" , channels );
119+ call .resolve (result );
139120 }
140121}
0 commit comments