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.72k stars 3.97k forks source link

[firebase_messaging] How to bundle notifications? #1770

Closed esDotDev closed 4 years ago

esDotDev commented 4 years ago

I'd like to group notifications like Hangouts does, where multiple messages are grouped together in one notification but I can't figure out how. From what I've read, this is a client-side implementation detail.

Here's my example payload:

var response = await http.post(
      'https://fcm.googleapis.com/fcm/send',
      headers: <String, String>{
        'Content-Type': 'application/json',
        'Authorization': 'key=$serverToken',
      },
      body: jsonEncode(
        <String, dynamic>{
          'collapse_key': 'chats',
          'notification': <String, dynamic>{
            'body': 'this is a body',
            'title': 'this is a title',
            'tag': 'tag'
          },
          'priority': 'high',
          'data': <String, dynamic>{
            'click_action': 'FLUTTER_NOTIFICATION_CLICK',
            'id': '1',
            'status': 'done'
          },
          'to': "/topics/all",
        },
      ),
    );
esDotDev commented 4 years ago

For reference, this is what I'm after: https://javapapers.com/android/android-bundled-notifications/

p30arena commented 4 years ago

@iapicca This is really needed in a messaging app, as WhatsApp does.

TedRibeiro commented 4 years ago

Reading the docs, my best guess is to link the plugin to Android project and then handle background messages: image

Salakar commented 4 years ago

A basic version of this would be to specify threadId for APNs and collapseKey for Android when sending your FCM messages from your backend service, for example with the Firebase Admin SDK for Node.js you'd do something like:

await admin.messaging().send({
  token: 'YOUR_TOKEN_HERE',
  apns: {
    payload: {
      aps: {
        threadId: 'foo',
        alert: 'Hello world',
      },
    },
  },
  android: {
    collapseKey: 'foo',
    notification: {
      title: 'Hello world',
    },
  },
});

For the full style though on Android you need to create notifications using the Inbox Style notification style. For this I'd recommend sending data only messages through FCM and then using a local notifications plugin to build an Inbox Style notification from your FCM message data.