swift – Flutter & OneSignal: Updating UI with background push notifications (iOS)


I have a flutter app that has push notifications functionality. For managing push notifications I’m using one signal package. So far it works pretty well, but I found myself strugling with handling notifications when I recieve them in the background. I need it because I want to update list of notifications that user see in the app each time new notification received. One signal doesn’t have it built in and I tried to add it myself.

I’ve tried to adjust swift code for this, and because of the lack of experience of working with swift it’s a bit challenging for me. I’ve tried to add this function – application(_:didReceiveRemoteNotification:fetchCompletionHandler:) to the AppDelegate because according to the apple documentation it should be triggered when notification is received either in foreground or background mode. I’ve tried to debug it and put some break points and prints but it was never triggered. I also tried to use NotificationServiceExtension to make it work, and it looks like didReceive(_:withContentHandler:) function here is trigerred as it should, but the problem is that because it’s outside of the AppDelegate I’m not sure how I can pass infromation about notification from the swift to flutter because I don’t know how to get binaryMessenger here

So I’d like to clarify a few things:

  1. Is it possible to somehow send data from NotificationServiceExtension to AppDelegate? Because I’ve seen some articles and I’m not sure about it because it seems they’re running in different containers
  2. Maybe it would be possible to send data directly from NotificationServiceExtension directly to the flutter side?
  3. Is there other approaches how I could catch notifications and send them directly to the flutter side?

Below the swift code that I’ve used:

import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
    var flutterChannel: FlutterMethodChannel!
    
    override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        let binaryMessenger = (window.rootViewController as! FlutterViewController).binaryMessenger
        flutterChannel = FlutterMethodChannel(name: "my.notification", binaryMessenger: binaryMessenger)

        GeneratedPluginRegistrant.register(with: self)
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }
    
    override func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        self.flutterChannel.invokeMethod("getNotification", arguments: "Notification from push")
    }
}

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img