ryanheise / audio_service

Flutter plugin to play audio in the background while the screen is off.
797 stars 477 forks source link

AudioService.notificationClicked not working in iOS #1024

Open VaibhavNeosoft opened 1 year ago

VaibhavNeosoft commented 1 year ago

(edit: Documentation request)

Documented behaviour

notificationClicked property A stream that broadcasts the status of the notificationClick event.

Actual behaviour

when the app is open from notification, the listener notificationClick is not receiving any update in iOS.

Minimal reproduction project

Official example: main.dart

Reproduction steps

  1. Run an example app on iOS
  2. Add a notificationClick listener to listen to the on-click updates
  3. Play an audio
  4. Click on the notification while the app is in the foreground/background
  5. notificationClick listener does not receive the update.

Output of flutter doctor


Checking Android licenses is taking an unexpectedly long time...[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 14.2)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.3)
[✓] VS Code (version 1.78.2)
[✓] Connected device (4 available)
[✓] HTTP Host Availability```
### Devices exhibiting the bug
iPhone SE, ios 16.4.1(a)
ryanheise commented 1 year ago

This is an Android-only feature, but the documentation was not clear on that.

I have converted this into a documentation request rather than a bug report so that I can remember to correct the documentation.

VaibhavNeosoft commented 1 year ago

Is there anything that can be done to listen to the notification click on iOS?

ryanheise commented 1 year ago

I am not aware of any, but if you are able to research and find a corresponding API on iOS, I could implement it for you.

VaibhavNeosoft commented 1 year ago

To determine whether a user has clicked on a notification in an iOS app, it is common to register a notification delegate, UNUserNotificationCenterDelegate, in the AppDelegate.swift file. By doing so, when a user interacts with a notification, the delegate receives an update and can trigger the necessary methods to notify the Flutter code about the notification click. This approach is typically used for regular notifications, but I am uncertain about its compatibility with Audio notification.

Upon reviewing your implementation in AudioServicePlugin.m, which contains native iOS code, I did not find a straightforward way to implement the above solution. I would like to know if there is a possibility of implementing the above solution within your existing implementation.

@ryanheise

vishal-singh-22 commented 1 year ago

I am facing the similar issue, any update on this @ryanheise.

ryanheise commented 1 year ago

Upon reviewing your implementation in AudioServicePlugin.m, which contains native iOS code, I did not find a straightforward way to implement the above solution. I would like to know if there is a possibility of implementing the above solution within your existing implementation.

Yes, I think so.

The AppDelegate.switch file or the AppDelegate.m file is not part of the plugin, it is part of your app. So it would just require adding iOS setup instructions to the README guiding app developers on how to configure this file.

Here is another iOS plugin that takes a similar approach:

https://pub.dev/packages/background_downloader

zhanglijie5997 commented 4 months ago

Hi, When playing in the background, I click the playback control in the notification bar to enter the foreground. Which event can be monitored? Thank you.