setNotificationListener static method
- dynamic onReceived()?,
- dynamic onClicked()?,
- dynamic onDismissed()?,
- dynamic onButtonClicked()?,
- dynamic onCustomContentReceived(
- dynamic
- dynamic onBackgroundNotificationReceived(
- String,
- dynamic
Set callbacks for different types of events for notifications (in foreground or when app is open in the background)
onReceived
is called when notification was received.
onClicked
is called when notification was clicked.
onDismissed
is called when notification was swiped away.
onButtonClicked
is called when notification contains button and a button was clicked.
onCustomContentReceived
is called when notification includes custom json. It will a json in string format.
onBackgroundNotificationReceived
is the function that will be called when notification is received in the background,
this would be a TopLevel or Static function will be passed and saved for later usage.
Function will take a dynamic value which is a dictionary (aka Map), which contains 'type' (one of receive, click, button_click, custom_content, dismiss),
and the 'data' either a custom map (provided as customContent), the notification, or the notification and the button (in case of button click)
In addition, the Isolate of the top level function is different and it will not have access to any widget (if app is in foreground, this does not get called and instead
foreground function will take place). So be aware of the isolate difference.
Implementation
static Future<void> setNotificationListener({
Function(NotificationData?)? onReceived,
Function(NotificationData?)? onClicked,
Function(NotificationData?)? onDismissed,
Function(NotificationData?)? onButtonClicked,
Function(dynamic)? onCustomContentReceived,
Function(String, dynamic)? onBackgroundNotificationReceived,
}) async {
if(!Platform.isAndroid) return;
_receiveCallback = onReceived;
_clickCallback = onClicked;
_dismissCallback = onDismissed;
_buttonClickCallback = onButtonClicked;
_customContentCallback = onCustomContentReceived;
_channel.setMethodCallHandler(_handleMethod);
// In case that application was not overrode and background was not used, this will be helpful (sets the listener after app is started which will not trigger background)
_channel.invokeMethod("Pushe.initNotificationListenerManually");
if (onBackgroundNotificationReceived != null) {
// If background was set, get the handles to save
final CallbackHandle? backgroundSetupHandle =
PluginUtilities.getCallbackHandle(_pusheSetupBackgroundChannel);
final CallbackHandle? backgroundMessageHandle =
PluginUtilities.getCallbackHandle(onBackgroundNotificationReceived);
// Callback must be exactly a top level or static and should be dependent to any inner scopes
if (backgroundMessageHandle == null) {
throw ArgumentError(
'''Failed to setup background handle. `backgroundNotificationListener` must be a TOPLEVEL or a STATIC method.
Checkout Flutter FAQ at https://docs.pushe.co for more information.
''');
}
_channel.invokeMethod<bool>(
'Pushe.notificationListener',
<String, dynamic>{
'setupHandle': backgroundSetupHandle?.toRawHandle(),
'backgroundHandle': backgroundMessageHandle.toRawHandle()
},
);
}
return;
}