Skip to content

GetResponse/MobileSDK-Flutter

Repository files navigation

GetResponseMobileSDK-Flutter

Installation

Add the following to your pubspec.yaml file:

dependencies:
    getresponsemobilesdk_flutter: 
        git:
           url: https://github.com/GetResponse/MobileSDK-Flutter.git

Usage

Configure SDK

  1. Go to app.getresponse.com > Web Push Notifications > Mobile apps to get the Application ID, Secret, and Entrypoint

  2. Add initialization of GetResponseSDK and Firebase Messaging in main method:

void initGetResponse() {
  GetResponsePushNotificationService().configure(
      secret: /*secret*/,
      applicationId: /*applicationId*/,
      entrypoint: /*entrypoint*/);
}

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );
  initGetResponse();
  runApp(const MyApp());
}

Managing consent

  1. To start sending notifications Request system permission and send consent:
    FirebaseMessaging.instance.requestPermission().then((notificationSettings) async {
        if (notificationSettings.authorizationStatus == AuthorizationStatus.authorized) {
            FirebaseMessaging.instance.getAPNSToken().then((apnsToken) {
                if (apnsToken != null || Platform.isAndroid) {
                    FirebaseMessaging.instance.getToken().then((fcmToken) {
                        GetResponsePushNotificationService().consent(lang: /*languageCode*/, externalId: /*externalId*/, email: /*email (optional)*/, fcmToken: fcmToken);
                    });
                }
            });
        }
    });
  2. Send consent on every token refresh:
    FirebaseMessaging.instance.onTokenRefresh.listen((fcmToken) async {
        await GetResponsePushNotificationService().consent(lang: /*languageCode*/, externalId: /*externalId*/, email: /*email (optional)*/, fcmToken: fcmToken);
    }).onError((err) {
        /* Handle error */
    });
  3. Remove consent to stop sending notifications (e.g logout):
    await GetResponsePushNotificationService().removeConsent()

Handling notifications

  1. Foreground notifications:

    FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
        final notificationHandler = await GetResponsePushNotificationService().handleIncomingNotification(message.data, EventType.showed);
        // show notification 
    });
  2. Background messages:

    • Create background handler outside of any class. Important: this handler works in background isolate so GetResponseSDK has to be initialize again.
    @pragma('vm:entry-point')
    Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
        initGetResponse();
        final notificationHandler = await GetResponsePushNotificationService().handleIncomingNotification(message.data, EventType.showed);
        if (Platform.isAndroid) {
            //show local notification for android
        }
    }
    • Add handler to firebase configuration in main method.
    FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
    • Add notification service extension class in XCode for IOS:
    class NotificationService: UNNotificationServiceExtension {
    ...
       
        override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
            self.contentHandler = contentHandler
            bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
            if let bestAttemptContent = bestAttemptContent {
                Messaging.serviceExtension().populateNotificationContent(bestAttemptContent, withContentHandler: contentHandler)
            }
        }
           
        ...
    }
  3. Tapping background notification (not terminated):

    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) async {
        final notificationHandler = await GetResponsePushNotificationService().handleIncomingNotification(message.data, EventType.clicked);
    });
  4. Tapping background notification (terminated):

    FirebaseMessaging.instance.getInitialMessage().then((message) async {
        if (message != null) {
        final notificationHandler = await GetResponsePushNotificationService().handleIncomingNotification(message.data, EventType.clicked);
    }
    });
  5. Handle depending how local notifications are shown for example user flutter_local_notifications

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages