Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure custom channel ID #31

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
Expand Up @@ -69,27 +69,28 @@ public static void registerWith(Registrar registrar) {

@Override
public void onMethodCall(MethodCall call, Result result) {
switch (call.method) {
case "startForegroundService":
final Boolean holdWakeLock = call.argument("holdWakeLock");
final String icon = call.argument("icon");
final int color = call.argument("color");
final String title = call.argument("title");
final String content = call.argument("content");
final String subtext = call.argument("subtext");
final Boolean chronometer = call.argument("chronometer");
final Boolean stopAction = call.argument("stop_action");
final String stopIcon = call.argument("stop_icon");
final String stopText = call.argument("stop_text");

launchForegroundService(icon, color, title, content, subtext, chronometer, stopAction, stopIcon, stopText);
result.success("startForegroundService");
break;
case "stopForegroundService":
stopForegroundService();
result.success("stopForegroundService");
break;
case "setServiceMethodInterval":
switch (call.method) {
case "startForegroundService":
final Boolean holdWakeLock = call.argument("holdWakeLock");
final String icon = call.argument("icon");
final int color = call.argument("color");
final String title = call.argument("title");
final String content = call.argument("content");
final String subtext = call.argument("subtext");
final Boolean chronometer = call.argument("chronometer");
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,channelId);
result.success("startForegroundService");
break;
case "stopForegroundService":
stopForegroundService();
result.success("stopForegroundService");
break;
case "setServiceMethodInterval":
if (call.argument("seconds") == null) {
result.notImplemented();
break;
Expand Down Expand Up @@ -118,7 +119,8 @@ 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(context, FlutterForegroundService.class);
intent.setAction(START_FOREGROUND_ACTION);
intent.putExtra("icon", icon);
Expand All @@ -130,6 +132,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);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,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))
Expand Down
2 changes: 1 addition & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_foreground_plugin/flutter_foreground_plugin.dart';

Expand Down Expand Up @@ -69,6 +68,7 @@ void startForegroundService() async {
title: "Flutter Foreground Service",
content: "This is Content",
iconName: "ic_stat_hot_tub",
channelId: "MY_CHANNEL_ID",
);
}

Expand Down
2 changes: 2 additions & 0 deletions lib/flutter_foreground_plugin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class FlutterForegroundPlugin {
bool stopAction = false,
String? stopIcon,
String stopText = 'Close',
String channelId = 'flutter_foreground_notification_channel',
}) async {
if (onStarted != null) {
onStartedMethod = onStarted;
Expand All @@ -48,6 +49,7 @@ class FlutterForegroundPlugin {
'stop_action': stopAction,
'stop_icon': stopIcon,
'stop_text': stopText,
'channel_id': channelId,
});
}

Expand Down