Huawei integration in Flutter SDK
Enable integration in the Synerise platform
Before you start integrating Huawei services in your app, you must configure the integration in Synerise platform. For instructions, see “Huawei integration”.
Configuration
In order to integrate Huawei Mobile Services (HMS) with Synerise, you must add .setMesaggingServiceType(MessagingServiceType.hms)
to your Synerise.initializer
.
We recommend passing MessagingServiceType.hms
as an argument when you build the app for AppGallery.
Implementing Huawei notifications in applications
- Add the Huawei push library as a dependency and integrate it: https://pub.dev/packages/huawei_push.
- Add an
onTokenEvent
callback to receive the HMS token. In the callback, send the token to Synerise by usingSynerise.notifications.registerForNotifications
method:
void _onTokenEvent(String event) { // Requested tokens can be obtained here setState(() { _token = event; }); if (event != null && event.isNotEmpty) { Synerise.notifications.registerForNotifications( event, mobileAgreement: true, onSuccess: () {}, onError: (error) {}, ); print("TokenEvent: " + _token); } }
- Add a listener to trigger
getToken()
from the Huawei push library whenever theonRegistrationRequired
callback is called:
Synerise.notifications.listener((listener) { listener.onRegistrationRequired = () { getToken(); }; });
- Add the
onMessageReceived
callback from the Huawei push library and pass the data to Synerise SDK:
Future<void> _onMessageReceived(RemoteMessage remoteMessage) async { // Called when a data message is received Map<String, String>? data = remoteMessage.getDataOfMap; if (data != null) { bool isSyneriseNotification = await Synerise.notifications.isSyneriseNotification(data); if (isSyneriseNotification == true) { Synerise.notifications.handleNotification(data); bool isSyneriseNotificationEncrypted = await Synerise.notifications .isNotificationEncrypted(data); if (isSyneriseNotificationEncrypted) { Map decryptedPayload = await Synerise.notifications.decryptNotification(data); developer.log(decryptedPayload.toString()); } } } }
- To make sure your callbacks work, use streams:
Future<void> initTokenStream() async { if (!mounted) return; Push.getTokenStream.listen(_onTokenEvent, onError: _onTokenError); } void getToken() { // Call this method to request for a token Push.getToken(""); } Future<void> initMessageStream() async { if (!mounted) return; Push.onMessageReceivedStream .listen(_onMessageReceived, onError: _onMessageReceiveError); }
Links and Deep Links
In order to implement links and deep links, refer to this instruction.
Configuring notification encryption
Instructions for encrypting push notifications are available here.