781flyingdutchman / background_downloader

Flutter plugin for file downloads and uploads
Other
165 stars 76 forks source link

iOS Notification not visible in notification center #312

Closed orevial closed 6 months ago

orevial commented 6 months ago

iOS notifications are not visible in the notification center.

When I run my app on an iPhone 15 simulator (with iOS 17.4) I can't get to make the notifications visible in the notification center.

I've tried by copying all background_downloader example widgets in my app and all my permissions seem to be correct, yet I don't have the notifications. I do have correct notifications when I run background_downloader example app directly on the same simulator.

Context:

<key>NSPhotoLibraryUsageDescription</key>
<string>We need to access your library for publications and profile picture</string>
<key>NSPhotoLibraryAddUsageDescription</key>
 <string>We need to access your library to move downloaded images and videos there</string>

as well as the background mode for fetching:

<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
<string>remote-notification</string>
</array>
import UIKit
import Flutter
import flutter_sharing_intent

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    UNUserNotificationCenter.current().delegate = self as UNUserNotificationCenterDelegate
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

simulator_screenshot_281CB19F-F36A-4E94-9EE1-6DE4C686E583

Am I missing something here for notifications to work on iOS ?

Env

I'm using background_downloader: ^8.4.3 and Flutter 3.19.5

781flyingdutchman commented 6 months ago

Notification center does not work properly on a simulator. Apparently a known issue. I spent hours looking into this too - please test that specific feature on a real device.

orevial commented 6 months ago

I have the exact same problem on a real device (iPhone X with iOS 16.3.1). It really seem to be specific to my app because when I test on a newly created app I have no problem having the notifications in notification center.

Are you maybe aware of some conflict with other plugins (e.g. Firebase ?) or some config that might mess with your plugin ?

781flyingdutchman commented 6 months ago

Hi, I'm sorry this is not working for you. It is definitely possible that another plugin interferes with the notification functionality, as some degree of cooperation is required. The AppDelegate code you included adds the flutter app as a delegate for notifications, and the flutter framework then calls each FlutterPlugin that implements UNUserNotificationCenterDelegate when delegate action is required. Here is that code for the background_downloader:

    @MainActor
    public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification) async -> UNNotificationPresentationOptions
    {
        if ourCategories.contains(notification.request.content.categoryIdentifier) {
            if #available(iOS 14.0, *) {
                return UNNotificationPresentationOptions.list
            } else {
                return UNNotificationPresentationOptions.alert
            }
        }
        return []
    }

As you can see, it checks if the notification.request.content.categoryIdentifier is "one of ours" and if not, returns []. If another plugin that implements UNUserNotificationCenterDelegate does not check and does not return [] when the notification category is not "theirs" then they effectively block notifications for other plugins that have been registered after that plugin.

There have been issues with this (e.g. see here for Firebase messaging, but I believe most have been resolved. Are you using other plugins that may do something with notifications and implement UNUserNotificationCenterDelegate?

orevial commented 6 months ago

Oh wow, that could explain a lot, I didn't know there was some cooperation between plugins involved in the process. I'm using Firebase messaging for push notifs so that could potentially be it but from what I understand it should not conflict with your plugin now, right ?

Is there a way to maybe change the order in which the different plugins and their respective AppDelegate are loaded, at least to confirm/infirm that there is a conflict with another plugin ?

orevial commented 6 months ago

Just disabled firebase_messaging entirely in my app for the sake of the test, and it indeed fixes my issue, I can now see background_downloader notifications in iOS notif center (even on simulator).

I will probably try and check what in Firebase messaging plugin is causing the issue and will probably a ticket there, do you know any workaround in the meantime ?

781flyingdutchman commented 6 months ago

The order of plugin registration in this file is usually determined by the order in which the plugins are listed in the pubspec.yaml file. When you add a plugin to pubspec.yaml and run flutter pub get, the build system generates the GeneratedPluginRegistrant.java file and lists the plugins in the order they appear in pubspec.yaml.

You could try to put he Firebase entry after the background downloader entry. Let me know if that works or not.

mattia-venturini commented 3 months ago

You could try to put he Firebase entry after the background downloader entry. Let me know if that works or not.

I tried it but the problems remains

@orevial did you manage to solve the problem?