firebase / flutterfire

šŸ”„ A collection of Firebase plugins for Flutter apps.
https://firebase.google.com/docs/flutter/setup
BSD 3-Clause "New" or "Revised" License
8.46k stars 3.91k forks source link

[firebase_messaging] set notification channel id (notification in background Not working) #88

Closed MahmoodBakhshayesh closed 3 years ago

MahmoodBakhshayesh commented 4 years ago

Describe the bug the function onResume and onLunch not working on android platform, while they work fine on IOS, i receive the following message on console instead of printed strings in those functions:

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

onMessage function works fine, the problem is when the app is in background

To Reproduce try to print something in onResume and onLunch functions

Expected behavior the functions be called on work fine as they do on IOS

Additional context my guess is that it has something to do with android notification channel id which should be added in android manifest

when i add that to manifest the message change to : "Notification Channel set in AndroidManifest.xml has not been created by the app. Default value will be used."

kroikie commented 4 years ago

@MahmoodBakhshayesh Thanks for flagging this, looks like we don't currently have support for message channels. We should add this.

Feel free to submit a PR if you have a solution.

iapicca commented 4 years ago

https://github.com/FirebaseExtended/flutterfire/issues/47 https://github.com/FirebaseExtended/flutterfire/pull/53 https://github.com/FirebaseExtended/flutterfire/issues/116 https://github.com/FirebaseExtended/flutterfire/issues/125

KingWu commented 4 years ago

Any clear documents to setup firebase messaging? I follow 5.1.6 instruction. Facing the above issue

Masaokb commented 4 years ago

Any updates?

digarahayu commented 4 years ago

Any update for this issue..? or it will be fix when stable version updated???

hiuryanderson commented 4 years ago

Any update for this issue..?

JemZubiri-Projects commented 4 years ago

Any updates for this?

Navil commented 4 years ago

Is there any update for this issue?

bhanuka96 commented 4 years ago

any update?

EzequielMoroni commented 4 years ago

Did someone find a workaround for this issue? Thanks

faldunate commented 4 years ago

Does anyone have a solution?

fl-oe commented 4 years ago

I had the problem that the method registered with "onBackgroundMessage" was not called. Here I found that if the message sent from my Nodejs backend had a "notification" entity, the "onBackgroundMessage" was not called. Only the "data" entity may be contained here.

Wrong way:

const message = {
  token: 'myToken',
  notification:{
    title: 'My Title',
    body: 'The body data',
  },
  data: {
    time: 'My data',
    value: '2',
  }
}

Right way:

const message = {
  token: 'myToken',
  data: {
    time: 'My data',
    value: '2',
  }
}

If I send the "right way" message, background function will be called.

It could be the solution for #1269

ajnaf commented 4 years ago

adding this to my sent data worked for me 'click_action': 'FLUTTER_NOTIFICATION_CLICK'

iapicca commented 4 years ago

Hi @MahmoodBakhshayesh did @ajnaf solution worked for you? Thank you

ayush221b commented 4 years ago

Hey, I am also facing the same issue. I can confirm that:

adding this to my sent data worked for me 'click_action': 'FLUTTER_NOTIFICATION_CLICK'

does not solve this issue.

Ehesp commented 4 years ago

Hello, just to give some insight on this.

That message is a information message only. If no channel is specified via your Android manifest then FCM creates & uses it's own notification channel (since they're required in later Android versions). The channel is only used to impact how the notification is shown - from memory this is a default priority notification with sound enabled.

If you created a custom channel (via https://pub.dev/packages/flutter_local_notifications for example), you could specify that channel ID in the manifest and the notification would use your custom one.

If you provide a channel ID to the notification and it doesn't exist on the device, FCM falls back to it's own channel (and you see that message).

Our plan is to make this more user-friendly to work with, however for now you can safely ignore this message. I'll leave open to track though.

ayush221b commented 4 years ago

@Ehesp As you mentioned if no channel is specified then FCM should create and use its own notification channel, hence while sending the notification to the app, if we do not include an Android Notification Channel param then the it should be attributed to the default channel that FCM has created, but that that is not the case. When I do not specify any custom notification channel in my project and send a notification without the Android Notification Channel param even then I get this error when the app is not in foreground.

Ehesp commented 4 years ago

@ayush221b it's not an error, it's just a message from FCM telling you it's using it's own channel. You can get around this for now by creating your own channel and assigning the channel ID to the message body when you send it from the server.

azheennn commented 4 years ago

I had the problem that the method registered with "onBackgroundMessage" was not called. Here I found that if the message sent from my Nodejs backend had a "notification" entity, the "onBackgroundMessage" was not called. Only the "data" entity may be contained here.

Wrong way:

const message = {
  token: 'myToken',
  notification:{
    title: 'My Title',
    body: 'The body data',
  },
  data: {
    time: 'My data',
    value: '2',
  }
}

Right way:

const message = {
  token: 'myToken',
  data: {
    time: 'My data',
    value: '2',
  }
}

If I send the "right way" message, background function will be called.

It could be the solution for #1269

hello, i've deleted the the notification and it still doesnt read my onbackgroundmessagehandler, what do i do

QuentinSc commented 3 years ago

Hello,

I've reading a lot of comments since 6 months and still cannot figure out how to make onBackgroundMessage working. OnLaunch, OnResume and OnMessage works perfectly. If onBackgroundMessage still not working I will have to change framework :(

Someone successed to deal with it ? A sample app ?

Thanks you

Gperez88 commented 3 years ago

still happen! onBackgroundMessage still not working

yourshinsuke commented 3 years ago
Notification Channel set in AndroidManifest.xml has not been created by the app. Default value will be used.

what is this??

Should I add there???

android/app/src/main/AndroidManifest.xml android/app/src/main/res/values/strings.xml

android/app/build.gradle implementation 'com.google.firebase:firebase-messaging:20.1.5'

android/app/src/main/java/io/flutter/plugins/GeneratedPluginRegistrant.java

document is useless

yourshinsuke commented 3 years ago

default_notification_channel_id is needed? what ID???

yourshinsuke commented 3 years ago

https://pub.dev/packages/firebase_messaging

deep01code commented 3 years ago

@yourshinsuke please if you found the solution share it with me,,, I am not Android developer and I followed the documentation and unfortunately, I am struggling with very badly with the code..... PLEASE IF ANYONE CAN SHARE THE SOLUTION I WILL BE SO THANKFUL

NoeLopez commented 3 years ago

My English is not perfect, but I solved it by adding a channel natively and then in the manifest where it says channel id I put the id of my channel created natively

deep01code commented 3 years ago

@NoeLopez thanks for your replay, can you please share your code ... thanks again

NoeLopez commented 3 years ago

Yes of course,

In the Application file that flutter recommends you add a globar variable that will be the name of your id channel, for example

public static final String CHANNEL_HIGH = "channel1";

where channel1 is the id channel, then you create a method like the following

private void createNotificationChannels() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channelHigh = new NotificationChannel(
                    CHANNEL_HIGH,
                    "NotificaciĆ³n Importante",
                    NotificationManager.IMPORTANCE_HIGH
            );

            channelHigh.setDescription("Canal Alto");

            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(channelHigh);

        }
    }

where Notificacion Importante is the name that will appear in the notification settings on the cell phone, then add I'm in the AndroidManifest

<application
        android:name=".Application"
        android:icon="@mipmap/ic_launcher"
        android:label="NotificacionPush">

        <activity
            android:name=".MainActivity" ...
           .......

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

</application>

Note that in android:value it must be the name of the channel ID, which you created in your public variable in the application class.

and with that the notifications will come out with bubble

deep01code commented 3 years ago

@NoeLopez thank you so much I spent 2 days trying to solve this issue and I will try to use your solution, I really appreciate your kind replay and wish all the best.

bhanuka96 commented 3 years ago

I am facing the same issue and also this issue https://stackoverflow.com/q/63271091/8822337 (please check this too)

NoeLopez commented 3 years ago

@bhanuka96 If you want to submit a group, you need to subscribe to a topic, in the documentation that mentions it, you would no longer send a single token, but would instead subscribe to a topic and submit it to that topic.

bhanuka96 commented 3 years ago

@NoeLopez are you sure? is it only support for topic? okay, but how group notification works on like whatsapp/gmail? I think you talk about the device group.

NoeLopez commented 3 years ago

@bhanuka96 Do you mean that notifications are grouped when they arrive?

deep01code commented 3 years ago

@NoeLopez I still get this error :( W/FirebaseMessaging( 6367): Notification Channel set in AndroidManifest.xml has not been created by the app. Default value will be used.

bhanuka96 commented 3 years ago

@bhanuka96 Do you mean that notifications are grouped when they arrive?

yes like this https://developer.android.com/training/notify-user/group . if "A" user sent multiple messages to the "B" user. currently, there are lots of notifications came to in my notification panel, I want to group notification for that. (it's mean like above image like gmail/whatsapp). if "C" user sent multiple message to the "B user". I want to 2 groups

NoeLopez commented 3 years ago

@bhanuka96 Those types of implementations would have to be done with Local Notification

NoeLopez commented 3 years ago

@deep01code It would be nice to be able to see your code to see where it goes wrong, remember to add it in Java class and in Android Manifest

bhanuka96 commented 3 years ago

@bhanuka96 Those types of implementations would have to be done with Local Notification

I tried, not working on foreground or background... I saw this react native answer about same issue. I think this part of the firebase_messaging plugin right? https://github.com/invertase/react-native-firebase/issues/449#issuecomment-404701979

NoeLopez commented 3 years ago

@bhanuka96 Apparently it seems like if it's a Firebase issue I'm really sorry I can't help you

braysonjohn148 commented 3 years ago

I followed exactly the tutorials on youtube on how to setup these notification, they don't do these staffs but they get notifications when the app is teriminated. I don't know what wrong with my code.

firebase_messaging: ^6.0.16

_backgroundMessageHandler i even tried calling FlutterLocalNotificationsPlugin, still don't show notifications when the app is terminated.

`<meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@mipmap/ic_launcher" /> <meta-data android:name="com.google.firebase.messaging.android.channel_id" android:value="mmne" />

        <meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="@string/default_notification_channel_id"/>`
NoeLopez commented 3 years ago

@braysonjohn148 If want to be notified in your app (via onResume and onLaunch, see below) when the user clicks on a notification in the system tray include the following intent-filter within the <activity> tag of your android/app/src/main/AndroidManifest.xml:

<intent-filter>
      <action android:name="FLUTTER_NOTIFICATION_CLICK" />
      <category android:name="android.intent.category.DEFAULT" />
  </intent-filter>

Optionally handle background messages

Background message handling is intended to be performed quickly. Do not perform long running tasks as they may not be allowed to finish by the Android system

By default background messaging is not enabled. To handle messages in the background:

1) Add the com.google.firebase:firebase-messaging dependency in your app-level build.gradle file that is typically located at <app-name>/android/app/build.gradle.

dependencies {
  // ...

  implementation 'com.google.firebase:firebase-messaging:20.2.4'
}

you can find out what the latest version of the plugin is here

2) Add an Application.java class to your app in the same directory as your MainActivity.java. This is typically found in <app-name>/android/app/src/main/java/<app-organization-path>/.

package com.domain.myapplication;

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) {
 FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
  }
}

3) In Application.java, make sure to change package com.domain.myapplication; to your package's identifier. Your package's identifier should be something like com.domain.myapplication for your application package

4) Set name property of application in AndroidManifest.xml. This is typically found in <app-name>/android/app/src/main/.

<application android:name=".Application" ...>

and the json that sends from Postman or from firebase in the data part has to go this, very important

"click_action": "FLUTTER_NOTIFICATION_CLICK"

AquĆ­ hay un ejemplo del cuerpo de un JSON que debe enviarse desde Postman

{
  "notification": {
    "body": "here you put the body of the message",
    "title": "here you put some title"
  },
  "priority": "high",
  "data": {
    "click_action": "FLUTTER_NOTIFICATION_CLICK",
    "other_data": "any message",
    .....
  },
  "to": "your token ......"
}

Hope this answer can help you solve your problem. Regards

braysonjohn148 commented 3 years ago

what i want is when an app is completely terminated. Meaning the phone is either in the pocket or user on other apps. When a new notification arrives the user to get a push notification even when the app is terminated. Currently when i close the app and send the notification. I cannot see the notification in the system tray. I even tried using the firebase console FCM to fire test messages on my app

NoeLopez commented 3 years ago

@braysonjohn148 that is done in the onLaunch method (it is when the app has closed completely) and onResume is when the app is in the background, you just have to implement there the actions you want it to do

Hakim-Allaoui commented 3 years ago

@braysonjohn148 If want to be notified in your app (via onResume and onLaunch, see below) when the user clicks on a notification in the system tray include the following intent-filter within the <activity> tag of your android/app/src/main/AndroidManifest.xml:

<intent-filter>
     <action android:name="FLUTTER_NOTIFICATION_CLICK" />
     <category android:name="android.intent.category.DEFAULT" />
 </intent-filter>

Optionally handle background messages

Background message handling is intended to be performed quickly. Do not perform long running tasks as they may not be allowed to finish by the Android system

By default background messaging is not enabled. To handle messages in the background:

  1. Add the com.google.firebase:firebase-messaging dependency in your app-level build.gradle file that is typically located at <app-name>/android/app/build.gradle.
dependencies {
  // ...

  implementation 'com.google.firebase:firebase-messaging:20.2.4'
}

you can find out what the latest version of the plugin is here

  1. Add an Application.java class to your app in the same directory as your MainActivity.java. This is typically found in <app-name>/android/app/src/main/java/<app-organization-path>/.
package com.domain.myapplication;

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) {
 FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
  }
}
  1. In Application.java, make sure to change package com.domain.myapplication; to your package's identifier. Your package's identifier should be something like com.domain.myapplication for your application package
  2. Set name property of application in AndroidManifest.xml. This is typically found in <app-name>/android/app/src/main/.

<application android:name=".Application" ...>

and the json that sends from Postman or from firebase in the data part has to go this, very important

"click_action": "FLUTTER_NOTIFICATION_CLICK"

AquĆ­ hay un ejemplo del cuerpo de un JSON que debe enviarse desde Postman

{
  "notification": {
    "body": "here you put the body of the message",
    "title": "here you put some title"
  },
  "priority": "high",
  "data": {
    "click_action": "FLUTTER_NOTIFICATION_CLICK",
    "other_data": "any message",
    .....
  },
  "to": "your token ......"
}

Hope this answer can help you solve your problem. Regards

Thank you save my day šŸ˜šŸ˜

yourshinsuke commented 3 years ago

Where is default_notification_channel_id? It is FLUTTER. Why don't you describe about it?

android/app/src/main/res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="default_notification_channel_id" translatable="false">fcm_default_channel</string>
</resources>
yourshinsuke commented 3 years ago
E/FlutterFcmService(18015): Fatal: failed to find callback
fazil-augr commented 3 years ago

same issue is facing from me also

danilomna commented 3 years ago

Yes of course,

In the Application file that flutter recommends you add a globar variable that will be the name of your id channel, for example

public static final String CHANNEL_HIGH = "channel1";

where channel1 is the id channel, then you create a method like the following

private void createNotificationChannels() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channelHigh = new NotificationChannel(
                    CHANNEL_HIGH,
                    "NotificaciĆ³n Importante",
                    NotificationManager.IMPORTANCE_HIGH
            );

            channelHigh.setDescription("Canal Alto");

            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(channelHigh);

        }
    }

where Notificacion Importante is the name that will appear in the notification settings on the cell phone, then add I'm in the AndroidManifest

<application
        android:name=".Application"
        android:icon="@mipmap/ic_launcher"
        android:label="NotificacionPush">

        <activity
            android:name=".MainActivity" ...
           .......

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

</application>

Note that in android:value it must be the name of the channel ID, which you created in your public variable in the application class.

and with that the notifications will come out with bubble

Hi @NoeLopez , does this solve the onBackgroundMessage callback issue? Could you help with a full project example on github?

danilomna commented 3 years ago

still happen! onBackgroundMessage still not working

hi @Gperez88 have you found a fix for this issue? It is not working for me too.

danilomna commented 3 years ago

Does anybody found a workaround or fix to make onBackgroundMessage work?