Repository: zo0r/react-native-push-notification (language: java)

Issue:
Add setShowBadge support for Android channel
# Feature Request

Add optional param for channel that will contribute to `setShowBadge` method on native side. PR is pending.

## Why it is needed

Main reason for this is because on some devices badge is incremented automatically and can not be removed even with `setApplicationIconBadgeNumber`. We wouldn't want to annoy user with badges increasing when it is not really needed so option to disable badge per channel will become handy.

## Possible implementation

Simply extend `RNPushNotificationHelper.checkOrCreateChannel` with extra param and use `channel.setShowBadge(value);`

### Code sample

`PushNotification.createChannel({channelId: "no_badge_channel", channelName: "Housekeeping notifications", showBadge: false});`



Discussion / hints:

Fix patch:
diff --git a/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java b/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java
index 248ff0825..9bb85a50c 100644
--- a/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java
+++ b/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java
@@ -269,13 +269,13 @@ public void sendToNotificationCentreWithPicture(Bundle bundle, Bitmap largeIconB
                         visibility = NotificationCompat.VISIBILITY_PRIVATE;
                 }
             }
-            
+
             String channel_id = bundle.getString("channelId");
 
             if(channel_id == null) {
                 channel_id = this.config.getNotificationDefaultChannelId();
             }
-            
+
             NotificationCompat.Builder notification = new NotificationCompat.Builder(context, channel_id)
                     .setContentTitle(title)
                     .setTicker(bundle.getString("ticker"))
@@ -283,7 +283,7 @@ public void sendToNotificationCentreWithPicture(Bundle bundle, Bitmap largeIconB
                     .setPriority(priority)
                     .setAutoCancel(bundle.getBoolean("autoCancel", true))
                     .setOnlyAlertOnce(bundle.getBoolean("onlyAlertOnce", false));
-            
+
             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { // API 24 and higher
                 // Restore showing timestamp on Android 7+
                 // Source: https://developer.android.com/reference/android/app/Notification.Builder.html#setShowWhen(boolean)
@@ -296,7 +296,7 @@ public void sendToNotificationCentreWithPicture(Bundle bundle, Bitmap largeIconB
                 // Changing Default mode of notification
                 notification.setDefaults(Notification.DEFAULT_LIGHTS);
             }
-      
+
             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) { // API 20 and higher
               String group = bundle.getString("group");
 
@@ -359,7 +359,7 @@ public void sendToNotificationCentreWithPicture(Bundle bundle, Bitmap largeIconB
                     largeIconBitmap = BitmapFactory.decodeResource(res, largeIconResId);
                 }
             }
-            
+
             if (largeIconBitmap != null){
               notification.setLargeIcon(largeIconBitmap);
             }
@@ -469,32 +469,32 @@ public void sendToNotificationCentreWithPicture(Bundle bundle, Bitmap largeIconB
 
                 vibratePattern = new long[]{0, vibration};
 
-                notification.setVibrate(vibratePattern); 
+                notification.setVibrate(vibratePattern);
             }
 
-            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 
+            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
               // Define the shortcutId
               String shortcutId = bundle.getString("shortcutId");
-              
+
               if (shortcutId != null) {
                 notification.setShortcutId(shortcutId);
               }
- 
+
               Long timeoutAfter = (long) bundle.getDouble("timeoutAfter");
-  
+
               if (timeoutAfter != null && timeoutAfter >= 0) {
                 notification.setTimeoutAfter(timeoutAfter);
               }
             }
 
             Long when = (long) bundle.getDouble("when");
-  
+
             if (when != null && when >= 0) {
               notification.setWhen(when);
             }
 
             notification.setUsesChronometer(bundle.getBoolean("usesChronometer", false));
-                
+
             notification.setChannelId(channel_id);
             notification.setContentIntent(pendingIntent);
 
@@ -717,7 +717,7 @@ public void clearDeliveredNotifications(ReadableArray identifiers) {
     @RequiresApi(api = Build.VERSION_CODES.M)
     public WritableArray getDeliveredNotifications() {
       WritableArray result = Arguments.createArray();
-  
+
       if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
         return result;
       }
@@ -819,10 +819,10 @@ private NotificationManager notificationManager() {
 
     public List<String> listChannels() {
       List<String> channels = new ArrayList<>();
-      
+
       if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
           return channels;
-      
+
       NotificationManager manager = notificationManager();
 
       if (manager == null)
@@ -840,7 +840,7 @@ public List<String> listChannels() {
     public boolean channelBlocked(String channel_id) {
       if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
           return false;
-      
+
       NotificationManager manager = notificationManager();
 
       if (manager == null)
@@ -857,7 +857,7 @@ public boolean channelBlocked(String channel_id) {
     public boolean channelExists(String channel_id) {
       if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
           return false;
-      
+
       NotificationManager manager = notificationManager();
 
       if (manager == null)
@@ -871,7 +871,7 @@ public boolean channelExists(String channel_id) {
     public void deleteChannel(String channel_id) {
         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
             return;
-        
+
         NotificationManager manager = notificationManager();
 
         if (manager == null)
@@ -880,7 +880,7 @@ public void deleteChannel(String channel_id) {
         manager.deleteNotificationChannel(channel_id);
     }
 
-    private boolean checkOrCreateChannel(NotificationManager manager, String channel_id, String channel_name, String channel_description, Uri soundUri, int importance, long[] vibratePattern) {
+    private boolean checkOrCreateChannel(NotificationManager manager, String channel_id, String channel_name, String channel_description, Uri soundUri, int importance, long[] vibratePattern, boolean showBadge) {
         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
             return false;
         if (manager == null)
@@ -904,6 +904,7 @@ private boolean checkOrCreateChannel(NotificationManager manager, String channel
             channel.enableLights(true);
             channel.enableVibration(vibratePattern != null);
             channel.setVibrationPattern(vibratePattern);
+            channel.setShowBadge(showBadge);
 
             if (soundUri != null) {
                 AudioAttributes audioAttributes = new AudioAttributes.Builder()
@@ -936,14 +937,15 @@ public boolean createChannel(ReadableMap channelInfo) {
         int importance = channelInfo.hasKey("importance") ? channelInfo.getInt("importance") : 4;
         boolean vibrate = channelInfo.hasKey("vibrate") && channelInfo.getBoolean("vibrate");
         long[] vibratePattern = vibrate ? new long[] { 0, DEFAULT_VIBRATION } : null;
+        boolean showBadge = channelInfo.hasKey("showBadge") ? channelInfo.getBoolean("showBadge") : true;
 
         NotificationManager manager = notificationManager();
 
         Uri soundUri = playSound ? getSoundUri(soundName) : null;
 
-        return checkOrCreateChannel(manager, channelId, channelName, channelDescription, soundUri, importance, vibratePattern);
+        return checkOrCreateChannel(manager, channelId, channelName, channelDescription, soundUri, importance, vibratePattern, showBadge);
     }
-    
+
     public boolean isApplicationInForeground() {
         ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
         List<RunningAppProcessInfo> processInfos = activityManager.getRunningAppProcesses();


Test patch: