Full developers guide available on: https://www.getresponse.com/help/mobile-apps-technical-documentation-for-flutter.html
Add the following to your pubspec.yaml file:
dependencies:
getresponsemobilesdk_flutter:
git:
url: https://github.com/GetResponse/MobileSDK-Flutter.git-
Go to app.getresponse.com > Web Push Notifications > Mobile apps to get the Application ID, Secret, and Entrypoint
-
Add initialization of GetResponseSDK and Firebase Messaging in main method:
Future<void> initGetResponse() async {
await GetResponseSDK().configure(
applicationId: /*applicationId*/,
secret: /*secret*/,
entrypoint: /*entrypoint*/,
settings: const GetResponseSDKSettings(enablePushNotifications: true, enableWebEvents: true) /* optional */ );
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
await initGetResponse();
runApp(const MyApp());
}- 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) { await await GetResponseSDK().notifications.consent( lang: /*language code*/, externalId: /*custom external id*/, email: /*email (optional)*/, fcmToken: fcmToken, ); }); } }); } });
- Send consent on every token refresh:
FirebaseMessaging.instance.onTokenRefresh.listen((fcmToken) async { await await GetResponseSDK().notifications.consent( lang: /*language code*/, externalId: /*custom external id*/, email: /*email (optional)*/, fcmToken: fcmToken, ); }).onError((err) { /* Handle error */ });
- Send consent on every token refresh:
- Remove consent to stop sending notifications (e.g logout):
await await GetResponseSDK().notifications.removeConsent()
-
Foreground notifications:
FirebaseMessaging.onMessage.listen((RemoteMessage message) async { final notificationHandler = await GetResponseSDK().notifications.handleIncomingNotification(message.data, EventType.showed); // show notification });
-
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 GetResponseSDK().notifications.handleIncomingNotification(message.data, EventType.showed); }
- 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) } } ... }
-
Tapping background notification (not terminated):
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) async { final notificationHandler = await GetResponseSDK().notifications.handleIncomingNotification(message.data, EventType.clicked); });
-
Tapping background notification (terminated):
FirebaseMessaging.instance.getInitialMessage().then((message) async { if (message != null) { final notificationHandler = await GetResponseSDK().notifications.handleIncomingNotification(message.data, EventType.clicked); } });
-
Handle depending how local notifications are shown for example user flutter_local_notifications