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:
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());
}
- 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); }); } }); } });
- 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 */ });
- Remove consent to stop sending notifications (e.g logout):
await GetResponsePushNotificationService().removeConsent()
-
Foreground notifications:
FirebaseMessaging.onMessage.listen((RemoteMessage message) async { final notificationHandler = await GetResponsePushNotificationService().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 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) } } ... }
-
Tapping background notification (not terminated):
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) async { final notificationHandler = await GetResponsePushNotificationService().handleIncomingNotification(message.data, EventType.clicked); });
-
Tapping background notification (terminated):
FirebaseMessaging.instance.getInitialMessage().then((message) async { if (message != null) { final notificationHandler = await GetResponsePushNotificationService().handleIncomingNotification(message.data, EventType.clicked); } });
-
Handle depending how local notifications are shown for example user flutter_local_notifications