From 8f0b6ff50d4c9c20ffb04a7b5452465bc359783c Mon Sep 17 00:00:00 2001 From: shashikantdurge Date: Wed, 12 May 2021 08:22:38 +0530 Subject: [PATCH 1/2] Configure custom channel ID --- .../flutter_foreground_plugin/FlutterForegroundPlugin.java | 6 ++++-- .../flutter_foreground_plugin/FlutterForegroundService.java | 2 +- example/lib/main.dart | 3 +-- lib/flutter_foreground_plugin.dart | 2 ++ 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/android/src/main/java/changjoopark/com/flutter_foreground_plugin/FlutterForegroundPlugin.java b/android/src/main/java/changjoopark/com/flutter_foreground_plugin/FlutterForegroundPlugin.java index 89072ab..49ea328 100644 --- a/android/src/main/java/changjoopark/com/flutter_foreground_plugin/FlutterForegroundPlugin.java +++ b/android/src/main/java/changjoopark/com/flutter_foreground_plugin/FlutterForegroundPlugin.java @@ -59,8 +59,9 @@ public void onMethodCall(MethodCall call, Result result) { final Boolean stopAction = call.argument("stop_action"); final String stopIcon = call.argument("stop_icon"); final String stopText = call.argument("stop_text"); + final String channelId = call.argument("channel_id"); - launchForegroundService(icon, color, title, content, subtext, chronometer, stopAction, stopIcon, stopText); + launchForegroundService(icon, color, title, content, subtext, chronometer, stopAction, stopIcon, stopText,channelId); result.success("startForegroundService"); break; case "stopForegroundService": @@ -96,7 +97,7 @@ public void onMethodCall(MethodCall call, Result result) { private void launchForegroundService(String icon, int color, String title, String content, String subtext, Boolean chronometer, Boolean stopAction, String stopIcon, - String stopText) { + String stopText,String channelId) { Intent intent = new Intent(activity, FlutterForegroundService.class); intent.setAction(START_FOREGROUND_ACTION); intent.putExtra("icon", icon); @@ -108,6 +109,7 @@ private void launchForegroundService(String icon, int color, String title, Strin intent.putExtra("stop_action", stopAction); intent.putExtra("stop_icon", stopIcon); intent.putExtra("stop_text", stopText); + intent.putExtra("channel_id", channelId); activity.startService(intent); serviceStarted = true; diff --git a/android/src/main/java/changjoopark/com/flutter_foreground_plugin/FlutterForegroundService.java b/android/src/main/java/changjoopark/com/flutter_foreground_plugin/FlutterForegroundService.java index ebd2bc3..99ae884 100644 --- a/android/src/main/java/changjoopark/com/flutter_foreground_plugin/FlutterForegroundService.java +++ b/android/src/main/java/changjoopark/com/flutter_foreground_plugin/FlutterForegroundService.java @@ -40,7 +40,7 @@ public int onStartCommand(Intent intent, int flags, int startId) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, - "flutter_foreground_service_channel", + bundle.getString("channel_id"), NotificationManager.IMPORTANCE_DEFAULT); ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)) diff --git a/example/lib/main.dart b/example/lib/main.dart index af42088..1129fe8 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,6 +1,4 @@ import 'package:flutter/material.dart'; -import 'dart:async'; - import 'package:flutter_foreground_plugin/flutter_foreground_plugin.dart'; void main() => runApp(MyApp()); @@ -63,6 +61,7 @@ void startForegroundService() async { title: "Flutter Foreground Service", content: "This is Content", iconName: "ic_stat_hot_tub", + channelId: "MY_CHANNEL_ID", ); } diff --git a/lib/flutter_foreground_plugin.dart b/lib/flutter_foreground_plugin.dart index 50057b6..fa45ee6 100644 --- a/lib/flutter_foreground_plugin.dart +++ b/lib/flutter_foreground_plugin.dart @@ -29,6 +29,7 @@ class FlutterForegroundPlugin { bool stopAction = false, String stopIcon, String stopText = 'Close', + String channelId = 'flutter_foreground_notification_channel', }) async { if (onStarted != null) { onStartedMethod = onStarted; @@ -49,6 +50,7 @@ class FlutterForegroundPlugin { 'stop_action': stopAction, 'stop_icon': stopIcon, 'stop_text': stopText, + 'channel_id': channelId, }); } From c9ade476cfbf4f435a6bbd56ae1f047ce67b6001 Mon Sep 17 00:00:00 2001 From: shashikantdurge Date: Wed, 26 May 2021 11:54:27 +0530 Subject: [PATCH 2/2] Null handling on intent object --- .../flutter_foreground_plugin/FlutterForegroundService.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/android/src/main/java/changjoopark/com/flutter_foreground_plugin/FlutterForegroundService.java b/android/src/main/java/changjoopark/com/flutter_foreground_plugin/FlutterForegroundService.java index 99ae884..6ce220a 100644 --- a/android/src/main/java/changjoopark/com/flutter_foreground_plugin/FlutterForegroundService.java +++ b/android/src/main/java/changjoopark/com/flutter_foreground_plugin/FlutterForegroundService.java @@ -23,7 +23,7 @@ public class FlutterForegroundService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { - if (intent.getAction() == null) { + if (intent == null || intent.getAction() == null) { return START_STICKY; } @@ -61,7 +61,7 @@ public int onStartCommand(Intent intent, int flags, int startId) { stopSelf.setAction(ACTION_STOP_SERVICE); PendingIntent pStopSelf = PendingIntent - .getService(this, 0, stopSelf ,PendingIntent.FLAG_CANCEL_CURRENT); + .getService(this, 0, stopSelf, PendingIntent.FLAG_CANCEL_CURRENT); builder.addAction(getNotificationIcon(bundle.getString("stop_icon")), bundle.getString("stop_text"), pStopSelf);