Skip to content
This repository was archived by the owner on Jul 11, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package fr.g123k.flutterappbadger;

import android.content.Context;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.MethodCall;
import me.leolin.shortcutbadger.ShortcutBadger;
import android.annotation.TargetApi;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;


/**
* FlutterAppBadgerPlugin
Expand All @@ -17,6 +26,10 @@ public class FlutterAppBadgerPlugin implements MethodCallHandler, FlutterPlugin
private Context applicationContext;
private MethodChannel channel;
private static final String CHANNEL_NAME = "g123k/flutter_app_badger";
private static final String NOTIFICATION_CHANNEL = "g123k/flutter_app_badger";

private NotificationManager mNotificationManager;
private int notificationId = 0;

/**
* Plugin registration.
Expand All @@ -27,6 +40,8 @@ public void onAttachedToEngine(FlutterPluginBinding flutterPluginBinding) {
channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), CHANNEL_NAME);
channel.setMethodCallHandler(this);
applicationContext = flutterPluginBinding.getApplicationContext();

mNotificationManager = (NotificationManager) applicationContext.getSystemService(applicationContext.NOTIFICATION_SERVICE);
}

@Override
Expand All @@ -38,7 +53,30 @@ public void onDetachedFromEngine(FlutterPluginBinding flutterPluginBinding) {
@Override
public void onMethodCall(MethodCall call, Result result) {
if (call.method.equals("updateBadgeCount")) {
if (Build.MANUFACTURER.equalsIgnoreCase("Xiaomi")) {
Notification.Builder builder = new Notification.Builder(applicationContext)
.setContentTitle(call.argument("title").toString())
.setContentText(call.argument("description").toString())
.setSmallIcon(applicationContext.getApplicationInfo().icon);

mNotificationManager.cancel(notificationId);
notificationId++;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
setupNotificationChannel();

builder.setChannelId(NOTIFICATION_CHANNEL);
}
Notification notification = builder.build();
ShortcutBadger.applyNotification(applicationContext, notification, Integer.valueOf(call.argument("count").toString()));
mNotificationManager.notify(notificationId, notification);
}
else
{
Log.d("App Badger: ", "Other Model detected");

ShortcutBadger.applyCount(applicationContext, Integer.valueOf(call.argument("count").toString()));
}
result.success(null);
} else if (call.method.equals("removeBadge")) {
ShortcutBadger.removeCount(applicationContext);
Expand All @@ -49,4 +87,13 @@ public void onMethodCall(MethodCall call, Result result) {
result.notImplemented();
}
}

@TargetApi(Build.VERSION_CODES.O)
private void setupNotificationChannel() {
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL, "ShortcutBadger Sample",
NotificationManager.IMPORTANCE_DEFAULT);

mNotificationManager.createNotificationChannel(channel);
}

}
4 changes: 2 additions & 2 deletions lib/flutter_app_badger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ class FlutterAppBadger {
static const MethodChannel _channel =
const MethodChannel('g123k/flutter_app_badger');

static void updateBadgeCount(int count) {
_channel.invokeMethod('updateBadgeCount', {"count": count});
static void updateBadgeCount(int count,{String title = 'Missed notification',String description=''}) {
_channel.invokeMethod('updateBadgeCount', {"count": count,"title": title,"description": description});
}

static void removeBadge() {
Expand Down