0

I’m new at react-native. Now I need to make an app icon with a notification badge counter (as in the photo). PushNotification.getApplicationIconBadgeNumber solved this issue for ios, but for android, it does not work. I tried to make it through the android and java, native modules, but the solution is not working. Are there any other options, or you might want to point out an error in the Java code, as it was all done blindly enter image description here

app\BadgesModule.java
package name;

import android.app.Notification;
import android.content.Context;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import androidx.core.app.NotificationCompat;

public class BadgesModule extends ReactContextBaseJavaModule {
  private final ReactApplicationContext context;
  public BadgesModule(ReactApplicationContext context) {
    super(context);
    this.context = context;
  }

  @Override
  public String getName() {
    return "Badges";
  }

  @ReactMethod
  public void setNotificationBadge(int count) {
    String NOTIFICATION_CHANNEL_ID = "01";
    Notification notification = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
      .setNumber(count)
      .build();
  }
}

BadgesPackage.java
package name;

import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;

import java.util.Collections;
import java.util.List;
import java.util.ArrayList;

public class BadgesPackage implements ReactPackage {
    @Override
    public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
        return Collections.emptyList();
    }

    @Override
    public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
        List<NativeModule> modules = new ArrayList<>();
        modules.add(new BadgesModule(reactContext));
        return modules;
    }
}

// usage in js
const BadgeModule = NativeModules.Badges;

export const useBadgeNumbers = () => {
  return BadgeModule.setNotificationBadge(2);
};

vasilek_
  • 31
  • 1
  • 2

1 Answers1

0

You can check this package react-native-app-badge from here

import ShortcutBadge from 'react-native-app-badge';

ShortcutBadge.getCount((count) => {
    ShortcutBadge.setCount(count + 1);
});

Or you can check this article about how to achieve it using Native from here

enter image description here

Louay Sleman
  • 1,794
  • 2
  • 15
  • 39