Skip to main content

awesome_notifications_chat 1.0.0-dev.1 copy "awesome_notifications_chat: ^1.0.0-dev.1" to clipboard
awesome_notifications_chat: ^1.0.0-dev.1 copied to clipboard

[pending analysis]

Chat notifications for awesome_notifications: Android conversations and iOS / macOS communication notifications, with a shared Person model.

πŸ’¬ awesome_notifications_chat #

Awesome Notifications

Chat notifications for awesome_notifications: Android conversations and iOS / macOS communication notifications, from one shared Person model.

pub package Full tests workflow codecov badge

Show messages the way messaging apps do: with the sender's photo and name, the group's name, the history of the conversation and a reply typed right in the notification. You describe the people of a conversation once, in Dart, and the package turns them into an androidx.core.app.Person on Android and an INPerson on iOS and macOS.


Key Features: #

  • Android conversations: MessagingStyle with each sender's name and avatar, a conversation shortcut that puts the notification in the Conversations section (Android 11+), and the history of the conversation in one notification.
  • iOS / macOS communication notifications: the sender's photo replaces the app icon, with the sender and group names; the messages of a conversation are grouped together in Notification Center.
  • Reply from the notification: the typed text reaches your app, and Android adds it to the conversation.
  • One-to-one and group conversations, with the Messaging and MessagingGroup layouts.
  • Messages by push with awesome_notifications_fcm: Android builds them even with the app killed, iOS through a Notification Service Extension, macOS while the app runs.
  • The same Person model everywhere, validated before it reaches the platform.


πŸ“™ Table of Contents #



πŸ“± Platform support #

Feature Android iOS macOS Windows Linux Web
Sender name and text βœ… βœ… βœ… ⏳ ⏳ ⏳
Sender avatar βœ… (Android 9+) βœ… βœ… ⏳ ⏳ ⏳
System conversation βœ… Conversations section (Android 11+) βœ… communication notification βœ… communication notification ❌ ❌ ❌
History in one notification βœ… (last 25 messages) ❌ (one notification per message, grouped by conversation) ❌ (grouped by conversation) ⏳ ⏳ ⏳
Group name βœ… βœ… βœ… ⏳ ⏳ ⏳
Group picture βœ… (conversation shortcut icon) βœ… βœ… ⏳ ⏳ ⏳
Reply from the notification βœ… βœ… βœ… ⏳ ⏳ ❌
Messages by push (awesome_notifications_fcm) βœ… βœ… (Notification Service Extension) βœ… while the app runs; plain text when closed ❌ ❌ ⏳
Bubbles ⏳ ❌ ❌ ❌ ❌ ❌

βœ… supported Β· πŸ§ͺ preview (implemented, still being validated) Β· ⏳ planned Β· ❌ not possible on this platform

  • iOS and macOS: communication notifications are mandatory. Every message of this package is a communication notification. There is no fallback to a plain notification: when the app is not set up for them, creating the notification fails with an error. Follow the setup below before sending the first message.
  • Minimum versions: iOS 15 and macOS 12. Android: the core's minimum (API 24); sender avatars need Android 9 and the Conversations section Android 11.
  • Use AwesomeNotificationsChat().capabilities() to adapt your UI to what the current platform can do (history, reply, avatar, system conversation).


πŸ›  Getting started #

  1. Add the core and this package to your pubspec.yaml:
dependencies:
  awesome_notifications: ^1.0.0-dev.1       # the core
  awesome_notifications_chat: ^1.0.0-dev.1  # this package (same version line as the core)
  awesome_notifications_fcm: ^1.0.0-dev.1   # only to receive messages by push
  1. Get the packages:
flutter pub get
  1. Configure each platform you ship, as described below. Android needs nothing beyond the core's setup; iOS and macOS need a capability and an Info.plist entry.

What changes when you add this package: it takes over the NotificationLayout.Messaging and NotificationLayout.MessagingGroup layouts. From then on, a notification with one of those layouts must carry a NotificationConversation (see Migrating from the 0.12 Messaging layouts). Without this package, the core keeps showing those layouts exactly as 0.12 did.


πŸ€– Configuring Android #

Nothing to add beyond the core's Android setup (minimum SDK, the POST_NOTIFICATIONS permission request on Android 13+).

Recommended:

  1. Create a dedicated channel for chat messages with NotificationImportance.High, so messages pop up on screen:
NotificationChannel(
  channelKey: 'chat',
  channelName: 'Chat',
  channelDescription: 'Chat messages',
  importance: NotificationImportance.High,
)
  1. Keep one notification id per conversation (see Sending chat messages): Android rebuilds the conversation's history from the notification that is already on screen.

How it works on Android: each message publishes (or updates) a long-lived conversation shortcut whose id is the conversation id. That shortcut is what lets Android 11+ show the notification in the Conversations section and let the user mark it as a priority conversation. When you remove a conversation for good (leaving a group, deleting a chat), call removeConversation, which also removes the shortcut. You can turn the shortcut off for a conversation with shortcut: false (the notification is then still a MessagingStyle, outside the Conversations section).


🍎 Configuring iOS (required) #

Communication notifications need iOS 15 and two things in your Xcode project: the Communication Notifications capability and the INSendMessageIntent user activity type. Do these steps once per app:

1. Raise the iOS deployment target to 15.0.

In Xcode, select the Runner project, then the Runner target, General tab, Minimum Deployments β†’ iOS 15.0. If your project uses CocoaPods, set it in ios/Podfile too:

platform :ios, '15.0'

2. Add the Communication Notifications capability.

  1. Open ios/Runner.xcworkspace in Xcode.
  2. Select the Runner target, then the Signing & Capabilities tab.
  3. Make sure Automatically manage signing is on and your Team is selected.
  4. Click + Capability and add Communication Notifications.

Xcode creates (or updates) ios/Runner/Runner.entitlements with:

<key>com.apple.developer.usernotifications.communication</key>
<true/>

With automatic signing, Xcode also enables the capability on your App ID in the Apple Developer portal. If you sign manually, enable Communication Notifications on the App ID (Certificates, Identifiers & Profiles β†’ Identifiers β†’ your app) and download a new provisioning profile.

3. Declare the INSendMessageIntent activity type.

Add to ios/Runner/Info.plist, inside the top-level <dict>:

<key>NSUserActivityTypes</key>
<array>
    <string>INSendMessageIntent</string>
</array>

If the key already exists, just add the INSendMessageIntent string to its array.

4. (Push only) Add a Notification Service Extension.

Local messages (created by your app) work with steps 1 to 3. A message that arrives by push (through awesome_notifications_fcm) becomes a communication notification only inside a Notification Service Extension: iOS runs it before showing the push, and the extension rebuilds the push through the same add-ons your app uses. The extension products are Swift packages, so this step needs Flutter's Swift Package Manager support (flutter config --enable-swift-package-manager).

  1. Generate Flutter's local packages once: flutter build ios --config-only.

  2. In Xcode (ios/Runner.xcworkspace), File β†’ New β†’ Target… β†’ Notification Service Extension. Name it NotificationService, language Swift, and answer Don't Activate when Xcode asks to activate its scheme.

  3. Select the NotificationService target:

    • General β†’ Minimum Deployments β†’ iOS 15.0.
    • Signing & Capabilities: the same Team as Runner. An extension must be signed with the app's team and certificate; its bundle id is the app's plus a suffix (e.g. com.example.app.NotificationService). Don't add the Communication Notifications capability here: it belongs to the app only.
  4. Add the packages to the extension. In the NotificationService target, General β†’ Frameworks and Libraries β†’ + β†’ Add Other… β†’ Add Package Dependency… β†’ Add Local…, and add these folders from ios/Flutter/ephemeral/Packages/.packages/:

    Folder Product to link
    awesome_notifications IosAwnCore
    awesome_notifications_fcm AwesomeNotificationsFcmExtension
    awesome_notifications_chat AwesomeNotificationsChatExtension
  5. Replace NotificationService/NotificationService.swift with:

    import AwesomeNotificationsChatExtension
    import AwesomeNotificationsFcmExtension
    import IosAwnCore
    
    class NotificationService: AwesomeServiceExtension {
        override func registerAddOns() {
            AwesomeFcmExtension.register()
            AwesomeChatExtension.register()
        }
    }
    
  6. Declare INSendMessageIntent in the extension's NotificationService/Info.plist too (the same NSUserActivityTypes block of step 3), inside the top-level <dict>.

Push messages must carry "mutable-content": 1 (see Sending messages by push). A push the extension doesn't recognize, or one it fails to rebuild, is shown as it arrived.

On iOS (and macOS), a group message shows the group's picture with the sender's name, like WhatsApp, as long as the group has more than one member besides the user (participants with two or more people). A "group" with a single other person reads as a one-to-one conversation and shows the sender's photo.


πŸ–₯ Configuring macOS (required) #

Communication notifications need macOS 12.

1. Raise the macOS deployment target to 12.0 (Runner target, General β†’ Minimum Deployments, and platform :osx, '12.0' in macos/Podfile if you use CocoaPods).

2. Add the capability and the network entitlement. In Xcode, open macos/Runner.xcworkspace, select the Runner target, Signing & Capabilities, choose your Team (macOS needs a development certificate for this capability, not "Sign to Run Locally"), then + Capability β†’ Communication Notifications. Add the same keys to both macos/Runner/DebugProfile.entitlements and macos/Runner/Release.entitlements:

<!-- Communication notifications -->
<key>com.apple.developer.usernotifications.communication</key>
<true/>
<!-- Download avatars from the network (the app is sandboxed) -->
<key>com.apple.security.network.client</key>
<true/>

3. Declare the INSendMessageIntent activity type in macos/Runner/Info.plist, exactly as on iOS (step 3 above).

4. (Push only) Enable push. Follow the macOS setup of awesome_notifications_fcm: the Push Notifications capability (in both entitlements files, com.apple.developer.aps-environment) and macos/Runner/GoogleService-Info.plist added to the Runner target. While the app runs, a chat push shows as a communication notification, once. macOS has no Notification Service Extension support in this package yet: with the app closed, the push shows its alert as plain text.

The group's picture works as on iOS (see the note at the end of Configuring iOS).


βœ… Checking the setup #

On iOS and macOS the package checks the setup before each message and fails the createNotification call when something is missing:

PlatformException(INSUFFICIENT_PERMISSIONS,
  Communication notifications are not set up: add the Communication Notifications
  capability and INSendMessageIntent to NSUserActivityTypes (see the package README),
  insufficientPermissions.communicationNotifications)

What it can check: the INSendMessageIntent entry in Info.plist (iOS and macOS) and the entitlement itself (macOS). iOS doesn't let an app read its own entitlements, so on iOS a missing capability is not detected at runtime: the message then shows as a plain notification, with the app icon instead of the sender's photo. If you see the app icon, go back to step 2.



πŸ“¨ Sending chat messages #

A chat message is a regular createNotification from the core with:

  • the Messaging layout (one-to-one) or the MessagingGroup layout (group), and
  • a NotificationConversation in extensions.

Each conversation should always use the same notification id: on Android the new message joins the history of the notification already on screen.


People #

const me = NotificationPerson(key: 'u-1', name: 'Rafael');

const ana = NotificationPerson(
  key: 'u-2',                                // a stable, unique id (an account id)
  name: 'Ana',
  avatar: 'https://example.com/ana.jpg',     // asset://, file://, https:// or resource://
  uri: 'tel:+5511999999999',                 // optional: tel:, mailto: or a contact URI
  isImportant: true,                         // Android: someone the user talks to often
);

me is the user of the device. Every person of a conversation needs a unique key.


A one-to-one conversation #

await AwesomeNotifications().createNotification(
  content: NotificationContent(
    id: 1,                                   // one id per conversation
    channelKey: 'chat',
    notificationLayout: NotificationLayout.Messaging,
  ),
  extensions: [
    NotificationConversation(
      id: 'conversation-ana',                // a stable conversation id
      me: me,
      participants: const [ana],
      message: const NotificationMessage(sender: ana, text: 'Are you coming?'),
    ),
  ],
);

The content's title and body can stay empty: the package fills them with the sender (or group) name and the message text.


A group conversation #

await AwesomeNotifications().createNotification(
  content: NotificationContent(
    id: 2,
    channelKey: 'chat',
    notificationLayout: NotificationLayout.MessagingGroup,
  ),
  extensions: [
    NotificationConversation(
      id: 'family',
      title: 'Family',                       // required for MessagingGroup
      image: 'https://example.com/family.jpg',
      me: me,
      participants: const [ana, bia],        // required for MessagingGroup
      message: const NotificationMessage(sender: bia, text: 'Dinner at 8?'),
    ),
  ],
);

Replying from the notification #

Add a button with requireInputText: true. A SilentAction keeps the app in the background while it handles the reply:

actionButtons: [
  NotificationActionButton(
    key: 'REPLY',
    label: 'Reply',
    requireInputText: true,
    actionType: ActionType.SilentAction,
  ),
],

In your action listener, send the message, then add it to the conversation:

@pragma('vm:entry-point')
static Future<void> onActionReceivedMethod(ReceivedAction action) async {
  final conversationId = AwesomeNotificationsChat.conversationIdOf(action);
  if (action.buttonKeyPressed == 'REPLY' && conversationId != null) {
    final text = action.buttonKeyInput;
    await myChatServer.send(conversationId, text);          // your app's logic
    await AwesomeNotificationsChat().addReply(
      conversationId,
      NotificationMessage(sender: me, text: text),
    );
  }
}

On Android, addReply adds the reply to the conversation and stops the "sending" indicator; skip it and Android keeps showing that indicator. On iOS and macOS it does nothing (the system never shows the user's own messages).

conversationIdOf reads the conversationId the package adds to the notification's payload (unless you set that key yourself).


Marking a conversation as read #

await AwesomeNotificationsChat().markAsRead('family');         // removes its notifications
await AwesomeNotificationsChat().removeConversation('family'); // also forgets the conversation
final onScreen = await AwesomeNotificationsChat().listActiveConversations();

removeConversation also removes the Android conversation shortcut and the iOS donated interactions. Use it when the conversation is gone for good, not just read.


Sending messages by push #

With awesome_notifications_fcm installed, a push carries the same conversation as createNotification, as JSON strings in the FCM data block: content (the notification, with the Messaging or MessagingGroup layout) and conversation (the NotificationConversation, with the model's keys). Android needs nothing else, even with the app killed; iOS needs the Notification Service Extension (step 4); macOS builds it while the app runs (Configuring macOS, step 4).

An FCM v1 message to a group:

{
  "message": {
    "token": "<the device's FCM token>",
    "data": {
      "content": "{\"id\": 21, \"channelKey\": \"chat\", \"notificationLayout\": \"MessagingGroup\"}",
      "conversation": "{\"id\": \"family\", \"title\": \"Family\", \"image\": \"https://example.com/family.jpg\", \"me\": {\"key\": \"me\", \"name\": \"Me\"}, \"participants\": [{\"key\": \"ana\", \"name\": \"Ana\", \"avatar\": \"https://example.com/ana.jpg\"}, {\"key\": \"bot\", \"name\": \"Awesome Bot\"}], \"message\": {\"sender\": {\"key\": \"ana\", \"name\": \"Ana\", \"avatar\": \"https://example.com/ana.jpg\"}, \"text\": \"On my way\"}}"
    },
    "apns": {
      "payload": {
        "aps": {
          "alert": { "title": "Family", "body": "On my way" },
          "mutable-content": 1,
          "sound": "default"
        }
      }
    }
  }
}
  • No top-level notification block (as in awesome_notifications_fcm): on Android the Firebase SDK would show it by itself, without the conversation. Android builds the message from data alone.
  • iOS needs aps.alert and "mutable-content": 1: iOS runs the extension only for a push with an alert, and shows that alert as it is when the extension can't run.
  • Put the message text in the alert's body, without the sender's name. The alert is the content's starting point, and a body already in the content wins over the message text. iOS already shows the sender as the title and the group's name as the subtitle, so "Ana: On my way" would repeat the name. The alert's title is replaced by the sender on iOS; use the group's name (or the sender's, one-to-one) for when the extension can't run.

πŸ— Model reference #

NotificationPerson

Field Type Required Android (Person) iOS / macOS (INPerson)
key String yes key handle (when no uri) and custom identifier
name String yes name display name
avatar String (media source) no icon, cropped round image
uri String (tel:, mailto:, contact) no uri phone or e-mail handle
isBot bool no bot β€”
isImportant bool no important β€”

NotificationMessage

Field Type Required Notes
sender NotificationPerson yes me for a message the user sent
text String yes plain text
timestamp DateTime no now when absent
image String (media source) no iOS: attached to the notification

NotificationConversation

Field Type Required Notes
id String yes Android shortcut id; iOS conversation identifier and thread
me NotificationPerson yes the user of the device
message NotificationMessage yes the new message
title String MessagingGroup the group's name
participants List<NotificationPerson> MessagingGroup the other people
image String (media source) no the group's picture; iOS and macOS show it when the group has two or more other members
history ConversationHistory no (append) Android: append to the notification on screen or replace it
shortcut bool no (true) Android: publish the conversation shortcut

Invalid models throw an AwesomeNotificationsException in Dart, before anything reaches the platform. Natively (for messages that don't come from Dart), the same rules answer INVALID_ARGUMENTS.



πŸ” Migrating from the 0.12 Messaging layouts #

In 0.12, the Messaging and MessagingGroup layouts used the content fields: title was the sender, body the message, largeIcon the avatar and summary the group name, and Android stacked the messages of a groupKey in one notification.

Without this package, 1.0 keeps exactly that behavior. With this package installed, those layouts require a NotificationConversation:

0.12 content field With this package
title (sender name) message.sender.name
body (message) message.text
largeIcon (sender avatar) message.sender.avatar
summary (group name) title of the conversation
groupKey (the conversation) id of the conversation

A Messaging / MessagingGroup notification without a conversation now fails with INVALID_ARGUMENTS instead of showing the 0.12 layout. Installing the package is your choice of the new behavior, so update every place that creates those layouts.



🩺 Troubleshooting #

INSUFFICIENT_PERMISSIONS / insufficientPermissions.communicationNotifications (iOS, macOS) The setup is incomplete: check the NSUserActivityTypes entry in Info.plist and, on macOS, the entitlement in both entitlements files. See Configuring iOS.

iOS shows the app icon instead of the sender's photo The Communication Notifications capability is missing or not in the provisioning profile (iOS can't detect it at runtime). Add it in Signing & Capabilities and rebuild.

macOS build error: "Provisioning profile ... doesn't include the Communication Notifications capability" or "No Accounts" Sign in to your Apple account in Xcode β€Ί Settings β€Ί Accounts, select your Team in Signing & Capabilities and build again (from the command line, add -allowProvisioningUpdates to xcodebuild).

INVALID_ARGUMENTS / arguments.invalid.notificationConversation A Messaging or MessagingGroup notification has no conversation, a conversation was sent with another layout, a group misses its title or participants, or a person key is repeated.

Android: the notification is not in the Conversations section It needs Android 11+ and the conversation shortcut (shortcut: true, the default). Some launchers limit the number of shortcuts; when publishing fails the message is still shown, outside that section.

Android keeps showing a spinner after a reply Call addReply after handling the typed text.

iOS: a push shows the alert's text as it is, not as a communication notification The extension didn't run or didn't recognize the push: check "mutable-content": 1 and an alert in aps, that the NotificationService extension is embedded in Runner, and that its Info.plist declares INSendMessageIntent. On the iOS Simulator, xcrun simctl push never runs the extension: test with a real push (FCM / APNs).

Xcode, extension target: "Entitlement com.apple.developer.usernotifications.communication not found and could not be included in profile" Remove the Communication Notifications capability (and its entitlements file) from the NotificationService target: it belongs to the Runner target only.

Xcode: "Embedded binary is not signed with the same certificate as the parent app" The extension's Team differs from Runner's. Select the same Team in both targets.

FCM answers 401 Invalid APNs credential (THIRD_PARTY_AUTH_ERROR) Firebase has no APNs key, or a key from another team. Upload the APNs authentication key (.p8) of the team that signs the app in Project settings β†’ Cloud Messaging, with its Key ID and Team ID.


See the example/ app for a runnable, single-file demo.

0
likes
0
points
--
downloads

Publisher

verified publishercarda.me

Weekly Downloads

Chat notifications for awesome_notifications: Android conversations and iOS / macOS communication notifications, with a shared Person model.

Homepage
Repository (GitHub)
View/report issues

License

(pending) (license)

Dependencies

awesome_notifications, flutter

More

Packages that depend on awesome_notifications_chat

Packages that implement awesome_notifications_chat