14

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Harsha Vardhan
  • 1,048
  • 6
  • 19
  • 34

6 Answers6

18

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>
Amer
  • 319
  • 3
  • 7
12

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"/>
flarkmarup
  • 5,129
  • 3
  • 24
  • 25
  • But sir, I have something specified in the metadata `````` and if I add that in the metadata it will throw an error right?? – Harsha Vardhan Jun 04 '19 at 04:56
  • `* What went wrong: Execution failed for task ':app:processDebugResources'. > Android resource linking failed Output: /Users/xxx/Workspace/xxx/build/app/intermediates/merged_manifests/debug/processDebugManifest/merged/AndroidManifest.xml:53: error: resource string/default_notification_channel_id (aka com.iotrl.cargill_fc:string/default_notification_channel_id) not found. error: failed processing manifest.` This is what I got and I couldn't find String.xml in the flutter project – Harsha Vardhan Jun 10 '19 at 10:19
  • @HarshaVardhan you should have defined string with id `default_notification_channel_id` somewhere.. :O – zeroDivider Mar 31 '20 at 23:49
8

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",
        },
    },
}
Juancki
  • 1,793
  • 1
  • 14
  • 21
7

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/default_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()
    .........
}
AnasSafi
  • 5,353
  • 1
  • 35
  • 38
2

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/

Saddan
  • 1,546
  • 16
  • 19
1

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

marcelpl
  • 21
  • 2