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

changing notification button texts for running service #183

Open
dorjeduck opened this issue Nov 21, 2023 · 10 comments
Open

changing notification button texts for running service #183

dorjeduck opened this issue Nov 21, 2023 · 10 comments

Comments

@dorjeduck
Copy link

dorjeduck commented Nov 21, 2023

Thanks for this great package and all the community information here.

Is it possible to change the text of the notification buttons in Android while a foreground service is running? I can't see it in the FlutterForegroundTask.updateService method. What i want is something similar to the timer notification of the standard Android Timer app, for which one button text changes between Pause and Resume when the button is pressed. Thx

@deakjahn
Copy link

deakjahn commented Dec 24, 2023

@dorjeduck It can be done—at least on Android, I don't know iOS. If you have a platform side plugin yourself, just add the following function:

private fun serverNotification(call: MethodCall, result: MethodChannel.Result) {
  val newTitle: String = call.argument<String?>("title")!!
  val newMessage: String = call.argument<String?>("message")!!
  val newButtonId: String = call.argument<String?>("buttonId")!!
  val newButtonText: String = call.argument<String?>("buttonText")!!

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    val manager = getSystemService(NotificationManager::class.java)
    val builder = Notification.Builder(this, "your_channel_identifier")
    builder.setSmallIcon(your_package_identifier.R.drawable.ic_notification)
    if (newTitle.isNotEmpty())
      builder.setContentTitle(newTitle)
    if (newMessage.isNotEmpty())
      builder.setContentText(newTitle)
    if (newButtonId.isNotEmpty() && newButtonText.isNotEmpty()) {
      val buttonIntent = Intent("onNotificationButtonPressed").apply {
        putExtra("data", newButtonId)
      }
      val buttonAction = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        val buttonPendingIntent = PendingIntent.getBroadcast(this, 1, buttonIntent, PendingIntent.FLAG_IMMUTABLE)
        Notification.Action.Builder(null, newButtonText, buttonPendingIntent).build()
      } else {
        val buttonPendingIntent = PendingIntent.getBroadcast(this, 1, buttonIntent, 0)
        Notification.Action.Builder(0, newButtonText, buttonPendingIntent).build()
      }
      builder.setActions(buttonAction)
    }
    manager.notify(119025, builder.build())
  }

  result.success(null)
}

Of course, you might want to modify parts. Here I only needed to send a replacement title, message or a single button but setActions() actually has a variable number of arguments. Also make sure you change the channel ID and the name of your icon as necessary. The 119025 in the last call is a completely arbitrary number, just pass it to the options when you set up your foreground task. As long as you use the same id, the system will replace the notification rather than create a new one.

FlutterForegroundTask.init(
  androidNotificationOptions: AndroidNotificationOptions(
    id: 119025,
    channelId: '...',

I contemplated creating a PR but as the current plugin works, I couldn't find an easy way to connect to the running instance and I didn't want to start a major refactoring. Our plugin author might decide whether and how it should be implemented best.

@deakjahn
Copy link

Oh yes, forgot to add, it only works from API 26 while Flutter is capable of running on earlier phones. Well, that's Oreo, probably not that many around any more.

deakjahn added a commit to deakjahn/flutter_foreground_task that referenced this issue Dec 24, 2023
@deakjahn deakjahn mentioned this issue Dec 24, 2023
@deakjahn
Copy link

I did the PR, after all. :-)

@dorjeduck
Copy link
Author

thanks a lot - I can't try it right now as my laptop broke but hopefully can give you feedback once I get it fixed

@dorjeduck
Copy link
Author

Hi. Back to flutter and the project for which i asked the question here. Thanks again for your reply deakjahn . Sorry a bit lost right now, can you help me to understand to which file/class i should add the function "serverNotification". Thanks

@deakjahn
Copy link

deakjahn commented Mar 3, 2024

@dorjeduck I added several changes to the plugin (see #195) and I've been using the modified version for quite some time now. I don't know if the plugin is still actively maintained or not and whether the PR will be accepted or I will need to keep my own local copy but if you want to use it, you can refer to my github repo directly: https://github.com/deakjahn/flutter_foreground_task

Basically, now that I looked back, it has three additions:

  • a notification function
  • a send message function
  • support for a callback when the engine starts, to make it possible to use platform calls from the front code

@dorjeduck
Copy link
Author

dorjeduck commented Mar 4, 2024

thx again - i wont have the time right now to digg into your PR unfortunately which is a bit challenging for me without an example (no complain just saying) So no feedback from me for now it seems, but hopefully this functionality will be included into this project in the future. All the best

@deakjahn
Copy link

deakjahn commented Mar 4, 2024

It's up to you :-) but there's really not much need for an example. You get a new FlutterForegroundTask.notification() call that you can pass much of the same arguments as to the original call: title, text, icon, buttons.

@dorjeduck
Copy link
Author

Thx for your patience :-) Gave it another try and finally I got it, works like a charm, and indeed pretty straight forward to use. The one thing which confused me is that id for AndroidNotificationOptions is an optional parameter which i never used and by that missed to understand what notificationId is about. But it should have been clear from your comments above ...
Thx again for your help

@deakjahn
Copy link

deakjahn commented Mar 4, 2024

OK, nice if it works for you, too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants