A wrapper package for FCM REST API inspired by PyFCM
Add Flutter FCM Wrapper to your pubspec.yaml
:
dependencies:
flutter_fcm_wrapper: <latest_version>
Get your API key from your Firebase Console
- Go to your Firebase Project
- Go to project setting
- Go to cloud messaging
- Obtain your server key
If you are sending message to Apple client app you will need to add your APNS key to your Firebase. Read more at Set up a Firebase Cloud Messaging client app on Apple platforms
import 'package:flutter_fcm_wrapper/flutter_fcm_wrapper.dart';
FlutterFCMWrapper flutterFCMWrapper = const FlutterFCMWrapper(
apiKey: "Your Own API Key",
enableLog: true,
enableServerRespondLog: true,
);
String result = await flutterFCMWrapper.sendTopicMessage(
topicName: "example",
title: "Example",
body: "Topic message send using Flutter FCM Wrapper",
androidChannelID: "example",
clickAction: "FLUTTER_NOTIFICATION_CLICK"
);
Map<String, dynamic> result = await
flutterFCMWrapper.sendMessageByTokenID(userRegistrationTokens: [user's token],
title: "Example",
body: "Token message send using Flutter FCM Wrapper",
androidChannelID: "example",
clickAction: "FLUTTER_NOTIFICATION_CLICK"
);
try {
String result = await flutterFCMWrapper.sendTopicMessage(
topicName: "example",
title: "Example",
body: "Topic message send using Flutter FCM Wrapper",
androidChannelID: "example",
clickAction: "FLUTTER_NOTIFICATION_CLICK"
);
} on FlutterFCMWrapperInvalidDataException catch (e) {
//TODO:: Handle the error here
} on FlutterFCMWrapperRespondException catch (e) {
//TODO:: Handle the error here
}
Example app includes other packages such as flutter_local_notifications and firebase_messaging, but it's not required as its being used to show the notification on the device and also to obtain user's registration token.
User is also required to set up their firebase for their project and insert their own FCM API in order for the example app to run expectedly
Type | Default Value | Nullable | Description | |
---|---|---|---|---|
apiKey | String |
- | β | API Key obtained from firebase console to authenticate sender's identity |
enableLog | bool |
false |
β | Enable to log error thrown and message delivery status |
enableServerRespondLog | bool |
false |
β | Enable to log raw header and body respond from the server |
Type | Default Value | Nullable | Description | |
---|---|---|---|---|
topicName | String |
- | β | The topic you wants to send this message to |
condition | String |
- | β | Logic expression that determine which topic you want to send this message to |
- [FlutterFCMWrapperInvalidDataException] will be thrown if [topicName] and [condition] is being provided at the same time
- [FlutterFCMWrapperInvalidDataException] will be thrown if [topicName] and [condition] is not being provided at the same time
Type | Default Value | Nullable | Description | |
---|---|---|---|---|
userRegistrationTokens | List<String> |
- | β | List of user's registration tokens that you want to send this message to |
- [FlutterFCMWrapperInvalidDataException] will be thrown if [userRegistrationTokens] is empty
- [FlutterFCMWrapperInvalidDataException] will be thrown if [userRegistrationTokens] length is more than 1000
Type | Default Value | Nullable | Description | |
---|---|---|---|---|
title | String |
- | β | Title of the notification |
body | String |
- | β | Body of the notification |
titleLocKey | String |
- | β | Key used to localize the title |
titleLocArgs | List<Map<String, String>> |
- | β | Used as format specifiers for the titleLocKey |
bodyLocKey | String |
- | β | Key used to localize the body |
bodyLocArgs | List<Map<String, String>> |
- | β | Used as format specifiers for the bodyLocKey |
subtitle | String |
- | β | Subtitle of the notification |
collapseKey | String |
- | β | Used to group the notification |
isHighPriority | bool |
true |
β | Priority of the notification |
contentAvailable | bool |
- | β | Application will be woken if set to true |
mutableContent | Map<String,bool> |
- | β | Ability to modify the notification's content before displaying it (Apple Platform only) |
timeToLive | int |
- | β | How long the message should be kept in the FCM storage when device is offline |
restrictedPackageName | String |
- | β | Register tokens must match with the provided package name in order to receive the notification(Android Only) |
isDryRun | bool |
false |
β | Used to send a test request without actually sending the notification |
data | Map<String,String> |
- | β | Payload of the message |
sound | String |
- | β | The sound to be play when the notification is received |
icon | String |
- | β | Icon of the notification |
tag | String |
- | β | Use to replace the existing notification shown, if not specified new notification will be created for each request |
color | String |
- | β | The notification's icon color |
imageUrl | String |
- | β | An Url which will be download and displayed on the notification |
badge | int |
- | β | Badge count for launchers |
clickAction | String |
- | β | The action that happen when user click on the notification |
androidChannelID | String |
- | β | Android notification's channel id |
isDataMessage | bool |
false |
β | If set to true, the notification send won't be shown or have any sound |
- [FlutterFCMWrapperInvalidDataException] will be thrown if [title] and [titleLocKey] is provided at the same time
- [FlutterFCMWrapperInvalidDataException] will be thrown if [body] and [bodyLocKey] is provided at the same time
- [FlutterFCMWrapperInvalidDataException] will be thrown if other parameters is provided and [isDataMessage] is set to true at the same time
Reason | Example Message | |
---|---|---|
FlutterFCMWrapperInvalidDataException | This error will be thrown if invalid parameters is provided | At least 1 registration token should be provided |
FlutterFCMWrapperRespondException | This error will be thrown if server responds error | Authentication error, invalid key might be provided |
For more detail explanation refer to Parameters Documentation
Feel free to open new pull request for contributions.
If it is a major or breaking changes it is recommended to open it as an issue to discuss it.
If any bugs has been found feel free to open an issue to discuss it.