简体   繁体   中英

Flutter push notifications app background : firebase_messaging

I want to display a push notification when my app is in the background.
I am using the flutter_local_notifications package and the firebase_messaging package.

Push notifications are working great with firebase_messaging and when my app is the background.
However, the below method:

 // The following handler is called, when App is in the background.
 FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);

is not called if a RemoteNotification object is passed through the RemoteMessage :

Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  print('message from background handler');
  print("Handling a background message: ${message.messageId}");
  
  // If this is null, then this handler method is called
  RemoteNotification remoteNotification = message.notification; 

  showNotificationFromData(message.data);
}

Therefore, i have to pass the message.data object which is a Map<String, dynamic> .

My problem is that i don't receive push notification anymore when this firebaseMessagingBackgroundHandler handler method is called.

So i have been trying to use the flutter_local_notification package to display a push notification but as it is said, it's "local" so it's working fine in the Foreground but apparently not in the background (the same code, with the same data is not showing in the background as a push notification).

Question:

I need to call the firebaseMessagingBackgroundHandler handler to handle events in my app. So i can do to still have push notifications when my app is in the background please?

Thanks

Okay i have found the solution. It was to set the content_available value to True in the server-side code.

To replace the firebase_messaging.Notification item, we need to override to the android and apns configs.

This looks like this in python:

apnsConfig = messaging.APNSConfig(
            payload=messaging.APNSPayload(
                aps=messaging.Aps(
                    content_available=True,
                )
            ),
     
        )

I am now receiving push notifications and call the background handler inside the dart code.

I know this bit too late but anyone trying to get custom notification using firebase then here is the solution

For Android (Flutter)

On Android, notification messages are sent to Notification Channels which are used to control how a notification is delivered. The default FCM channel used is hidden from users, however provides a "default" importance level. Heads up notifications require a "max" importance level.

This means that we need to first create a new channel with a maximum importance level & then assign incoming FCM notifications to this channel. we can take advantage of the flutter_local_notifications package

  1. Add the flutter_local_notifications package to your local project.
  2. Create a new AndroidNotificationChannel instance:
const AndroidNotificationChannel channel = AndroidNotificationChannel(
  'high_importance_channel', // id
  'High Importance Notifications', // title
  'This channel is used for important notifications.', // description
  importance: Importance.max,
);

(keep in mind this is example code but you can custimize notification through notification channel code)

  1. Create the channel on the device:
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
    FlutterLocalNotificationsPlugin();

    await flutterLocalNotificationsPlugin
      .resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
      ?.createNotificationChannel(channel);
  1. Once created, we can now update FCM to use our own channel rather than the default FCM one. To do this, open the android/app/src/main/AndroidManifest.xml file for your FlutterProject project. Add the following meta-data schema within the application component:
    <meta-data
      android:name="com.google.firebase.messaging.default_notification_channel_id"
      android:value="high_importance_channel" />

now whenever you send notification specify your channel id and your app automatically show your custom notification (through api or firebase console)

{
   "message":{
      "token":"token_1",
      "android_channel_id":"high_importance_channel" //specify channel id here
      "data":{},
      "notification":{
        "title":"FCM Message",
        "body":"This is an FCM notification message!",
      }
   }
}

as for ios they don't support notification channel i don't have necessary resources and info how things works if you guys know comment below will be much appriciated

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM