1818import com .google .firebase .messaging .FirebaseMessagingService ;
1919import com .google .firebase .messaging .RemoteMessage ;
2020
21+ import java .util .List ;
2122import java .util .Map ;
2223import java .util .Random ;
2324
@@ -152,11 +153,12 @@ private void sendNotification(String id, String title, String messageBody, Map<S
152153 Log .d (TAG , "Sound was null " );
153154 }
154155
156+ int lightArgb = 0 ;
155157 if (lights != null ) {
156158 try {
157159 String [] lightsComponents = lights .replaceAll ("\\ s" , "" ).split ("," );
158160 if (lightsComponents .length == 3 ) {
159- int lightArgb = Color .parseColor (lightsComponents [0 ]);
161+ lightArgb = Color .parseColor (lightsComponents [0 ]);
160162 int lightOnMs = Integer .parseInt (lightsComponents [1 ]);
161163 int lightOffMs = Integer .parseInt (lightsComponents [2 ]);
162164
@@ -169,7 +171,6 @@ private void sendNotification(String id, String title, String messageBody, Map<S
169171 if (android .os .Build .VERSION .SDK_INT >= android .os .Build .VERSION_CODES .M ) {
170172 int accentID = getResources ().getIdentifier ("accent" , "color" , getPackageName ());
171173 notificationBuilder .setColor (getResources ().getColor (accentID , null ));
172-
173174 }
174175
175176 Notification notification = notificationBuilder .build ();
@@ -184,8 +185,25 @@ private void sendNotification(String id, String title, String messageBody, Map<S
184185
185186 // Since android Oreo notification channel is needed.
186187 if (android .os .Build .VERSION .SDK_INT >= android .os .Build .VERSION_CODES .O ) {
187- NotificationChannel channel = new NotificationChannel (channelId , channelName , NotificationManager .IMPORTANCE_HIGH );
188- notificationManager .createNotificationChannel (channel );
188+ List <NotificationChannel > channels = notificationManager .getNotificationChannels ();
189+
190+ boolean channelExists = false ;
191+ for (int i = 0 ; i < channels .size (); i ++) {
192+ if (channelId .equals (channels .get (i ).getId ())) {
193+ channelExists = true ;
194+ }
195+ }
196+
197+ if (!channelExists ) {
198+ NotificationChannel channel = new NotificationChannel (channelId , channelName , NotificationManager .IMPORTANCE_HIGH );
199+ channel .enableLights (true );
200+ channel .enableVibration (true );
201+ channel .setShowBadge (true );
202+ if (lights != null ) {
203+ channel .setLightColor (lightArgb );
204+ }
205+ notificationManager .createNotificationChannel (channel );
206+ }
189207 }
190208
191209 notificationManager .notify (id .hashCode (), notification );
0 commit comments