Skip to content

Commit

Permalink
Merge pull request #915 from XavierBesnault/issue804
Browse files Browse the repository at this point in the history
Fix issue with Android not getting Led ON for notifications on Android O
  • Loading branch information
briantq authored Nov 5, 2018
2 parents 98953f4 + 2e0eac4 commit db3b661
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/android/FirebasePluginMessagingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

import java.util.List;
import java.util.Map;
import java.util.Random;

Expand Down Expand Up @@ -152,11 +153,12 @@ private void sendNotification(String id, String title, String messageBody, Map<S
Log.d(TAG, "Sound was null ");
}

int lightArgb = 0;
if (lights != null) {
try {
String[] lightsComponents = lights.replaceAll("\\s", "").split(",");
if (lightsComponents.length == 3) {
int lightArgb = Color.parseColor(lightsComponents[0]);
lightArgb = Color.parseColor(lightsComponents[0]);
int lightOnMs = Integer.parseInt(lightsComponents[1]);
int lightOffMs = Integer.parseInt(lightsComponents[2]);

Expand All @@ -169,7 +171,6 @@ private void sendNotification(String id, String title, String messageBody, Map<S
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
int accentID = getResources().getIdentifier("accent", "color", getPackageName());
notificationBuilder.setColor(getResources().getColor(accentID, null));

}

Notification notification = notificationBuilder.build();
Expand All @@ -184,8 +185,25 @@ private void sendNotification(String id, String title, String messageBody, Map<S

// Since android Oreo notification channel is needed.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(channel);
List<NotificationChannel> channels = notificationManager.getNotificationChannels();

boolean channelExists = false;
for (int i = 0; i < channels.size(); i++) {
if (channelId.equals(channels.get(i).getId())) {
channelExists = true;
}
}

if (!channelExists) {
NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
channel.enableLights(true);
channel.enableVibration(true);
channel.setShowBadge(true);
if (lights != null) {
channel.setLightColor(lightArgb);
}
notificationManager.createNotificationChannel(channel);
}
}

notificationManager.notify(id.hashCode(), notification);
Expand Down

0 comments on commit db3b661

Please sign in to comment.