Push notifications
With this plugin you can also manage the push notification section of MBurger, this is a wrapper around MPush, the underlying platform, so you should refer to the MPush documentation to understand the concepts and to start the push integration. In order to use
MBMessages
instead of MPush
you have to do the following changes:Set the push token like this:
MBPush.pushToken = "YOUR_PUSH_TOKEN";
Configure the callbacks and Android native interface like this:
MBPush.configure(
onNotificationArrival: (notification) {
print("Notification arrived: $notification");
},
onNotificationTap: (notification) {
print("Notification tapped: $notification");
},
androidNotificationsSettings: MPAndroidNotificationsSettings(
channelId: 'messages_example',
channelName: 'mbmessages',
channelDescription: 'mbmessages',
icon: '@mipmap/icon_notif',
),
);
To configure the Android part you need to pass a
MPAndroidNotificationsSettings
to the configure sections, it has 2 parameters:channelId
: the id of the channelchannelName
: the name for the channelchannelDescription
: the description for the channelicon
: the default icon for the notification, in the example application the icon is in the res folder as a mipmap, so it's adressed as@mipmap/icon_notif
, iff the icon is a drawable use@drawable/icon_notif
.
To request a notification token you need to do the following things:
- 1.Set a callback that will be called once the token is received correctly from APNS/FCM
MBPush.onToken = (token) {
print("Token retrieved: $token");
}
- 1.Request the token using MPush:
MBPush.requestToken();
Once you have a notification token you can register this device to push notifications and register to topics:
MBPush.onToken = (token) async {
print("Token received $token");
await MBPush.registerDevice(token).catchError(
(error) => print(error),
);
await MBPush.registerToTopic(MPTopic(code: 'Topic')).catchError(
(error) => print(error),
);
print('Registered');
};
The topic are instances of the
MPTopic
class which has 3 properties:code
: the id of the topic- [Optional]
title
: the readable title of the topic that will be displayed in the dashboard, if this is not set it will be equal tocode
. - [Optional]
single
: if this topic represents a single device or a group of devices, by defaultfalse
.
MBurger has 2 default topics that you should use in order to guarantee the correct functionality of the engagement platform:
MBMessages.projectPushTopic()
: this topic represents all devices registred to push notifications for this projectMBMessages.devicePushTopic()
: this topic represents the current device
await MBPush.registerToTopics(
[
await MBMessages.projectPushTopic(),
await MBMessages.devicePushTopic(),
MPTopic(code: 'Topic'),
],
);
If the application was launched from a notification you can retrieve the data of the notification like this, this will be
null
if the application was launched normally:Map<String, dynamic> launchNotification = await MBPush.launchNotification();
print(launchNotification);
Last modified 2yr ago