I am trying to implement the background notification handler in flutter for the one signal flutter sdk. What I am trying to do is call a method in flutter side when a background notification arrives like in the OneSignal docs suggests https://documentation.onesignal.com/docs/service-extensions#android-notification-extender-service . This is my MainActrivity.kt
import androidx.annotation.NonNull
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import com.onesignal.OneSignal.OSRemoteNotificationReceivedHandler;
var channel: MethodChannel? = null
class MainActivity: FlutterActivity() {
private val CHANNEL = "samples.flutter.dev/notification";
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine){
super.configureFlutterEngine(flutterEngine);
channel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL);
}
}
public class NotificationServiceExtension : OSRemoteNotificationReceivedHandler{
override fun remoteNotificationReceived(Context context, OSNotificationReceivedEvent notificationReceivedEvent) {
channel.invokeMethod("testNotification");
}
}
I have never written Kotlin code before and maybe I am doing something wrong here as this is resulting in build errors. Has anyone here done something similar before? Would really appreciate any help. Thanks in advance!
onesignal_flutter: 3.2.8
flutter doctor :
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.10.1, on Ubuntu 20.04.3 LTS 5.13.0-30-generic,
locale en_US.UTF-8)
[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.1)
[✓] Android Studio
[✓] VS Code
[✓] Connected device (2 available)
[✓] HTTP Host Availability