9 C
London
Saturday, February 10, 2024

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


I’ve a flutter app that has push notifications performance. For managing push notifications I am utilizing one sign package deal. To date it really works fairly properly, however I discovered myself strugling with dealing with notifications once I recieve them within the background. I want it as a result of I need to replace record of notifications that consumer see within the app every time new notification acquired. One sign does not have it inbuilt and I attempted so as to add it myself.

I’ve tried to regulate swift code for this, and due to the dearth of expertise of working with swift it is a bit difficult for me. I’ve tried so as to add this perform – software(_:didReceiveRemoteNotification:fetchCompletionHandler:) to the AppDelegate as a result of in accordance with the apple documentation it must be triggered when notification is acquired both in foreground or background mode. I’ve tried to debug it and put some break factors and prints but it surely was by no means triggered. I additionally tried to make use of NotificationServiceExtension to make it work, and it seems like didReceive(_:withContentHandler:) perform right here is trigerred because it ought to, however the issue is that as a result of it is outdoors of the AppDelegate I am unsure how I can move infromation about notification from the swift to flutter as a result of I do not know methods to get binaryMessenger right here

So I might wish to make clear a number of issues:

  1. Is it doable to in some way ship knowledge from NotificationServiceExtension to AppDelegate? As a result of I’ve seen some articles and I am unsure about it as a result of it appears they’re working in several containers
  2. Possibly it will be doable to ship knowledge instantly from NotificationServiceExtension on to the flutter aspect?
  3. Is there different approaches how I may catch notifications and ship them on to the flutter aspect?

Beneath the swift code that I’ve used:

import UIKit
import Flutter

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

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

Latest news
Related news

LEAVE A REPLY

Please enter your comment!
Please enter your name here