From 8f04bea26deb54cfea0dedddf6dfc5cd92337a8f Mon Sep 17 00:00:00 2001 From: "kirill.kulakov" Date: Wed, 13 Feb 2019 14:52:59 -0600 Subject: [PATCH] use a default channel if no channel is specified --- .../react/fcm/SendNotificationTask.java | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/android/src/main/java/com/evollu/react/fcm/SendNotificationTask.java b/android/src/main/java/com/evollu/react/fcm/SendNotificationTask.java index e04ec12a..eac25f8a 100644 --- a/android/src/main/java/com/evollu/react/fcm/SendNotificationTask.java +++ b/android/src/main/java/com/evollu/react/fcm/SendNotificationTask.java @@ -1,6 +1,8 @@ package com.evollu.react.fcm; import android.app.Notification; +import android.app.NotificationChannel; +import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; @@ -32,6 +34,7 @@ import java.net.URL; import java.net.URLDecoder; +import static android.content.Context.NOTIFICATION_SERVICE; import static com.facebook.react.common.ReactConstants.TAG; public class SendNotificationTask extends AsyncTask { @@ -78,7 +81,20 @@ protected Void doInBackground(Void... params) { String subText = bundle.getString("sub_text"); if (subText != null) subText = URLDecoder.decode( subText, "UTF-8" ); - NotificationCompat.Builder notification = new NotificationCompat.Builder(mContext, bundle.getString("channel")) + String defaultChannelId = "default"; + + String channelId = bundle.getString("channel", defaultChannelId); + + NotificationManager mngr = (NotificationManager) mContext.getSystemService(NOTIFICATION_SERVICE); + if (mngr != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + if (mngr.getNotificationChannel(channelId) == null) { + channelId = defaultChannelId; + NotificationChannel channel = new NotificationChannel(channelId, "Default", NotificationManager.IMPORTANCE_DEFAULT); + mngr.createNotificationChannel(channel); + } + } + + NotificationCompat.Builder notification = new NotificationCompat.Builder(mContext, channelId) .setContentTitle(title) .setContentText(body) .setTicker(ticker) @@ -186,7 +202,7 @@ protected Void doInBackground(Void... params) { } } // setBigContentTitle and setSummaryText overrides current title with body and subtext - // that cause to display duplicated body in subtext when picture has specified + // that cause to display duplicated body in subtext when picture has specified notification.setStyle(bigPicture); }