Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase messaging-handle background message in kotlin

I'm using firebase_messaging in my flutter application. To handle background messages with firebase messaging in pub they suggested to create new Application.java file and replace java file name in AndroidManifest file.

In my application i'm using kotlin and i already implemented some native code in MainActivity.kt

So how to write this code in kotlin.

package io.flutter.plugins.firebasemessagingexample;

import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService;

public class Application extends FlutterApplication implements PluginRegistrantCallback {
 @Override
public void onCreate() {
 super.onCreate();
 FlutterFirebaseMessagingService.setPluginRegistrant(this);
 }

@Override
public void registerWith(PluginRegistry registry) {
 GeneratedPluginRegistrant.registerWith(registry);
}
}

it is mandatory to replace MainActivity to Application in AndroidManifest file?

like image 230
MSARKrish Avatar asked Jan 30 '20 10:01

MSARKrish


People also ask

How do I handle the Firebase notification when an app is in foreground?

Firebase notifications behave differently depending on the foreground/background state of the receiving app. If you want foregrounded apps to receive notification messages or data messages, you'll need to write code to handle the onMessageReceived callback.

How do I handle background notifications on Android?

To get notification data when your app in the background, you should add click_action inside notification payload. "Put that intent-filter on your manifest, inside application tag." you can't put intent-filter inside application tag.

Can FCM notification on Android overwrite previous one?

If specified and a notification with the same tag is already being shown, the new notification replaces the existing one in the notification drawer.


2 Answers

Here is the Kotlin code for the new firebase cloud messaging version:

package id.your.app
    
import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.firebase.messaging.FlutterFirebaseMessagingBackgroundService
// import io.flutter.plugins.firebase.messaging.FlutterFirebaseMessagingPlugin

class Application : FlutterApplication(), PluginRegistrantCallback {
  override fun onCreate() {
    super.onCreate()
    FlutterFirebaseMessagingBackgroundService.setPluginRegistrant(this)
  }

  override fun registerWith(registry: PluginRegistry?) {
    // FlutterFirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebase.messaging.FlutterFirebaseMessagingPlugin"))
  }
}
like image 98
icaksama Avatar answered Sep 28 '22 03:09

icaksama


Here is the working background notification kotlin code:

package com.example.yourapp

import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.GeneratedPluginRegistrant
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService

class Application : FlutterApplication(), PluginRegistrantCallback {

    override fun onCreate() {
        super.onCreate()
        FlutterFirebaseMessagingService.setPluginRegistrant(this);
    }

    override fun registerWith(registry: PluginRegistry?) {
        io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
    }
}
like image 35
Daniel Walter Avatar answered Sep 28 '22 05:09

Daniel Walter