diff --git a/packages/firebase_messaging/CHANGELOG.md b/packages/firebase_messaging/CHANGELOG.md index 52419076ad94..d63ae0f801f2 100644 --- a/packages/firebase_messaging/CHANGELOG.md +++ b/packages/firebase_messaging/CHANGELOG.md @@ -1,3 +1,7 @@ +## 5.1.2 + +* Updates to README and example with explanations of differences in data format. + ## 5.1.1 * Update README with more detailed integration instructions. diff --git a/packages/firebase_messaging/README.md b/packages/firebase_messaging/README.md index cfb0b79b551a..51f78851efc3 100644 --- a/packages/firebase_messaging/README.md +++ b/packages/firebase_messaging/README.md @@ -93,6 +93,21 @@ Messages are sent to your Flutter app via the `onMessage`, `onLaunch`, and `onRe Additional reading: Firebase's [About FCM Messages](https://firebase.google.com/docs/cloud-messaging/concept-options). +## Notification messages with additional data +It is possible to include additional data in notification messages by adding them to the `"data"`-field of the message. + +On Android, the message contains an additional field `data` containing the data. On iOS, the data is directly appended to the message and the additional `data`-field is omitted. + +To receive the data on both platforms: + +````dart +Future _handleNotification (Map message, bool dialog) async { + var data = message['data'] ?? message; + String expectedAttribute = data['expectedAttribute']; + /// [...] +} +```` + ## Sending Messages Refer to the [Firebase documentation](https://firebase.google.com/docs/cloud-messaging/) about FCM for all the details about sending messages to your app. When sending a notification message to an Android device, you need to make sure to set the `click_action` property of the message to `FLUTTER_NOTIFICATION_CLICK`. Otherwise the plugin will be unable to deliver the notification to your app when the users clicks on it in the system tray. diff --git a/packages/firebase_messaging/example/lib/main.dart b/packages/firebase_messaging/example/lib/main.dart index cf8e64347bed..36de611f8bd3 100644 --- a/packages/firebase_messaging/example/lib/main.dart +++ b/packages/firebase_messaging/example/lib/main.dart @@ -9,9 +9,10 @@ import 'package:flutter/material.dart'; final Map _items = {}; Item _itemForMessage(Map message) { - final String itemId = message['data']['id']; + final dynamic data = message['data'] ?? message; + final String itemId = data['id']; final Item item = _items.putIfAbsent(itemId, () => Item(itemId: itemId)) - ..status = message['data']['status']; + ..status = data['status']; return item; } diff --git a/packages/firebase_messaging/pubspec.yaml b/packages/firebase_messaging/pubspec.yaml index 3772ba72e2b2..20efc36817d9 100644 --- a/packages/firebase_messaging/pubspec.yaml +++ b/packages/firebase_messaging/pubspec.yaml @@ -3,7 +3,8 @@ description: Flutter plugin for Firebase Cloud Messaging, a cross-platform messaging solution that lets you reliably deliver messages on Android and iOS. author: Flutter Team homepage: https://github.com/flutter/plugins/tree/master/packages/firebase_messaging -version: 5.1.1 + +version: 5.1.2 flutter: plugin: