diff --git a/android/src/main/java/fr/g123k/flutterappbadger/FlutterAppBadgerPlugin.java b/android/src/main/java/fr/g123k/flutterappbadger/FlutterAppBadgerPlugin.java index 18d1e47..710ea49 100644 --- a/android/src/main/java/fr/g123k/flutterappbadger/FlutterAppBadgerPlugin.java +++ b/android/src/main/java/fr/g123k/flutterappbadger/FlutterAppBadgerPlugin.java @@ -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 @@ -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. @@ -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 @@ -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); @@ -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); + } + } diff --git a/lib/flutter_app_badger.dart b/lib/flutter_app_badger.dart index 6378ce3..ccf04f0 100644 --- a/lib/flutter_app_badger.dart +++ b/lib/flutter_app_badger.dart @@ -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() {