Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing default Notification Channel metadata in AndroidManifest in Flutter

I am using firebase_messaging: ^5.0.1 package to achieve push notifications, everything is working fine in IOS whereas coming to the android when my mobile application running background I am receiving a notification but it is not navigating to the respective screens, it just opens the default screen. How to achieve navigation to that particular screen.

PS: I implemented click_action functionality that's the reason it's working fine in iOS but Android it shows the below message

W/FirebaseMessaging( 8260): Missing Default Notification Channel metadata in AndroidManifest. Default value will be used.

Here is my AndroidManifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.check">

    <!-- io.flutter.app.FlutterApplication is an android.app.Application that
         calls FlutterMain.startInitialization(this); in its onCreate method.
         In most cases you can leave this as-is, but you if you want to provide
         additional functionality it is fine to subclass or reimplement
         FlutterApplication and put your custom class here. -->
    <application
        android:name="io.flutter.app.FlutterApplication"
        android:label="Cargill FC"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:allowBackup="false"
            android:fullBackupContent="false"
            android:windowSoftInputMode="adjustResize">
            <!-- This keeps the window background of the activity showing
                 until Flutter renders its first frame. It can be removed if
                 there is no splash screen (such as the default splash screen
                 defined in @style/LaunchTheme). -->
            <meta-data
                android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
                android:value="true" />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            <intent-filter>
                <action android:name="FLUTTER_NOTIFICATION_CLICK" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Push notificatio code:

@override
  void initState() {
    super.initState();
    tabController = new TabController(length: 2, vsync: this);

    _firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        onFirebaseMessage(message);
      },
      onLaunch: (Map<String, dynamic> message) async {
        print("onLaunch: $message");
      },
      onResume: (Map<String, dynamic> message) async {
        print("onResume: $message");
      },
    );

    _firebaseMessaging.requestNotificationPermissions(const IosNotificationSettings(sound: true, badge: true, alert: true));
    _firebaseMessaging.onIosSettingsRegistered.listen((IosNotificationSettings settings) {
      print("Settings registered: $settings");
    });

    _firebaseMessaging.getToken().then(registerFirebaseTokenForUser);
  }

Here onMessage is the only thing working perfectly in Android. I want to achieve the same when it is running background.

like image 489
Harsha Vardhan Avatar asked Jun 03 '19 13:06

Harsha Vardhan


Video Answer


6 Answers

1- At first, add this meta code after </activity> tag in AndroidManifest.xml which located in path <flutter project path>/android/app/src/main/AndroidManifest.xml

<meta-data
   android:name="com.google.firebase.messaging.default_notification_channel_id"
   android:value="@string/notification_channel_id" />

Note: If you set this meta inside <activity> the code will not work.

2- Modify file (or create new file if not exists) in this path <flutter project path>/android/app/src/main/res/values/string.xml to be like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="default_notification_channel_id" translatable="false">fcm_default_channel</string>
</resources>

This will solve the problem Missing Default Notification Channel metadata in AndroidManifest. Default value will be used.

But after that, you need to create this channel in Android, to do that go to file <flutter project path>//android/app/src/main/kotlin/com/examble/project_name/Application.kt and add this function:

private fun createChannel(){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        // Create the NotificationChannel
        val name = getString(R.string.default_notification_channel_id)
        val channel = NotificationChannel(name, "default", NotificationManager.IMPORTANCE_HIGH)
        val notificationManager: NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.createNotificationChannel(channel)
    }
}

Then call it from onCreate() function:

override fun onCreate() {
    super.onCreate()
    createChannel()
    .........
}
like image 178
AnasSafi Avatar answered Oct 17 '22 09:10

AnasSafi


If your flutter version is greater than 1.12 you don't need to create any file like Application.java or Application.kt just add the below meta value to you AndroidManifest file

<meta-data
     android:name="com.google.firebase.messaging.default_notification_channel_id"
       android:value="high_importance_channel" />

Reference: https://firebase.flutter.dev/docs/messaging/overview/

like image 39
Saddan Avatar answered Oct 07 '22 03:10

Saddan


Adding FLUTTER_NOTIFICATION_CLICK is required to be sent, for onResume and onLunch to be executed.

{ 
    "notification": {...},
    "click_action": "FLUTTER_NOTIFICATION_CLICK"
} 

For my golang server, this meant adding the AndroidConfig

message := &messaging.Message{
    Topic: topic,
    Notification: &messaging.Notification{/* */}
    Data: data,
    APNS: &messaging.APNSConfig{/* */}
    Android: &messaging.AndroidConfig{
        Notification: &messaging.AndroidNotification{
            ClickAction: "FLUTTER_NOTIFICATION_CLICK",
        },
    },
}
like image 38
Juancki Avatar answered Oct 17 '22 10:10

Juancki


For those who are not able to find "string.xml", you can find it under: android>app>src>main>res>values. It is not the same as styles.xml. If you do not have one yet, you can create one:

  1. Right click "values" folder,
  2. Click New/Values Resource File
  3. Copy, and paste the following text:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="default_notification_channel_id" translatable="false">fcm_default_channel</string>
</resources>
like image 16
Amer Avatar answered Oct 17 '22 11:10

Amer


Maksim has a pretty solid answer here including links to the official docs. You need to add a the following meta-data tag in you Manifest:

<meta-data
    android:name="com.google.firebase.messaging.default_notification_channel_id"
    android:value="@string/default_notification_channel_id"/>

And in string.xml you can declare default_notification_channel_id in the following way: <string name=“default_notification_channel_id”>Channel ID</string>

Then you must provide an attribute with that specific id when sending push notifications.

EDIT It is possible to have multiple meta-data tags in your AndroidManifest.xml:

            <meta-data
                android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
                android:value="true" />
            <meta-data
                android:name="com.google.firebase.messaging.default_notification_channel_id"
                android:value="@string/default_notification_channel_id"/>
like image 11
flarkmarup Avatar answered Oct 17 '22 09:10

flarkmarup


Adding 'click_action': 'FLUTTER_NOTIFICATION_CLICK' to my notification's data solved this for me

like image 1
Marcel Plummer Avatar answered Oct 17 '22 09:10

Marcel Plummer