Flutter SDK Integration
Last updated
Last updated
Quick Steps Guidelines
Step 1: Install Flutter
Add netmera_flutter_sdk
to your pubspec.yaml
and run flutter pub get
to install the package.
Step 2: Setup Android
Initialize Netmera in MainActivity.java
and configure required settings in AndroidManifest.xml
.
Step 3: Setup iOS
Install CocoaPods with pod install
and configure AppDelegate.swift
for push notifications and Netmera setup.
Step 4: Calling Dart Methods (for onPrem)
Set the base URL for onPrem setups if needed.
Please find the Onboarding Checklist for Swift and Flutter below. Follow the titles in the checklist to ensure you have completed each essential step in your onboarding process with Netmera.
To use the Netmera Flutter SDK in your project, follow these steps:
Add the following lines to your pubspec.yaml
file under the dependencies section:
Install the package using the command:
No additional Netmera SDK libraries are required for Android or iOS.
In Netmera Panel:
Navigate to Developers > Netmera Onboarding.
Select Android and click Start to proceed.
Netmera uses Firebase Cloud Messaging (FCM) for delivering push notifications.
Go to the Firebase Developers Console and create a Firebase project.
Generate a new Private Key (JSON file) for your project.
Upload this file in Netmera Panel > Developers > Netmera Onboarding > Android > Step 2: Create A Firebase Configuration > FCM Service Account Key.
Choose the Flutter based on your application framework.
Important Notes:
Do not use the API key from a test panel in production.
Each panel has a unique API key, and using the wrong one can result in data misdirection or errors.
To obtain your SDK API Key:
Go to the Netmera Panel.
Navigate to Developer > API > SDK API Key.
Copy your SDK API Key from this section.
Project’s build.gradle
File
Add the following to your project-level build.gradle
file:
App’s build.gradle
File
Add the required dependency and plugins to your app-level build.gradle
file:
At the top of your app’s build.gradle
, include:
Important Note for Obfuscation:
If your code is obfuscated, there is no need to add any special rules for Netmera. Its functionality is unaffected by code obfuscation.
Create the Application Class (Java)
Create an application class that extends FlutterApplication
.
Replace <YOUR GCM SENDER ID>
, <YOUR HMS SENDER ID>
, and <YOUR NETMERA API KEY>
with your actual values.
Set Up Event Handlers in Dart
Inside your Dart class, add the following code to set up event handlers for push notifications.
Custom Firebase Messaging Integration
If you are using Firebase Messaging in your app, use these code snippets to handle Netmera push notifications:
Get Firebase Token:
Handle Incoming Firebase Messages:
If you're building your Flutter project with the flutter run --release
command and need to handle background messages:
Add the @pragma('vm:entry-point')
annotation to the _firebaseMessagingBackgroundHandler
method.
This ensures that the method is correctly recognized as an entry point when building the project in release mode, which is crucial for handling background messages. By adding this annotation, you ensure that Flutter will properly recognize and invoke the background handler during release builds.
Here’s how to modify your code:
Custom Huawei Messaging Integration
For Huawei messaging, use the following code to manage push notifications:
Get Huawei Token:
Handle Incoming Huawei Messages:
Android 13 introduced significant changes to how push notification permissions are handled.
Increased User Control: Apps targeting API Level 33 and above now have more control over when and how they request push notification permissions. This allows for a more user-friendly and context-aware permission request experience.
Automatic Permission Requests for Older Apps: For apps targeting API Level 32 and below running on Android 13, the system automatically prompts the user for push notification permission when the app creates its first notification channel.
Impact on Netmera SDK Users:
The Netmera SDK initializes and creates notification channels during the app's startup. Consequently:
On Android 13: For apps targeting API Level 32 and below, this initialization process will trigger the automatic system prompt for push notification permission.
On Android 12 and below: The system generally assumes push notification permission has already been granted.
If you initialize the Netmera SDK at app startup (which is recommended), the system will automatically request push notification permissions on Android 13 devices for apps targeting API Level 32 and below. For devices running Android 12 or earlier, the system assumes permission is already granted.
For Android apps targeting API Level 33, the Netmera SDK provides two key methods to handle notification permissions:
Check Notification Permission Status:
This method checks if the app has the necessary permissions to send push notifications. It returns true
if the permission is granted, otherwise false
.
Request Notification Permissions:
Use this method to request notification permissions from the user. Call this method when you need to prompt the user for permissions at any time.
Push Enable/Disable User Flow:
Granting Permission:
The user triggers requestNotificationPermissions()
, and if they grant permission, a push enable request is sent.
Handling Denial:
If the user denies permission, avoid resending the request immediately (as recommended by Google). Instead, the SDK opens the app's notification settings. The user can grant permission from the settings and return to the app, where a push enable request is sent.
Denying Permission:
If the user triggers requestNotificationPermissions()
and denies permission, a push disable request is sent.
Reattempt After Denial:
If denied, avoid immediate re-request. The SDK will open the app's notification settings. If the user cancels (presses the back button), a push disable request is sent.
Log in to developer.apple.com with your Apple Developer account.
Generate an Apple Push Notification Certificate for your app.
Export the certificate using Keychain Access on your Mac.
Upload the exported certificate to the Netmera panel by navigating to Developer > Push Backends > iOS in the left menu.
Bundle IDs should match on your project, certificate and panel:
Ensure that the certificate you upload to the panel matches the bundle ID of your project. Additionally, set your project's bundle ID in the panel to ensure consistency. The bundle ID of your project, the certificate, and the one specified in the panel must all align.
Certificate types:
For apps downloaded from the App Store or tested via TestFlight, the certificate type should be set to "prod".
For apps built directly from Xcode, the certificate type must be set to "dev".
If you have problems sending push notifications when you build from Xcode, verify the certificate type on the Developer > Push Backends > iOS page in Panel.
Install Pods
Navigate to the iOS folder in your terminal and run the following command to install the required pods:
Swift Configuration
If you're using Swift, add the following imports in your Runner-Bridging-Header.h
file:
To enable message sending from iOS to Dart, configure your AppDelegate
class as follows:
To obtain your SDK API Key:
Go to the Netmera Panel.
Navigate to Developer > API > SDK API Key.
Copy your SDK API Key from this section.
Use this key to replace the ("<YOUR-NETMERA-KEY>")
placeholder in the sample code.
When you trigger FNetmeraService.handleWork(ON_PUSH_RECEIVE, dict: ["userInfo": userInfo])
in AppDelegate
, the following Dart method will be triggered:
This ensures that push notifications from iOS are correctly handled and passed to your Dart code.
In Flutter applications targeting iOS, it is necessary to request notification permissions. To request notification permissions on iOS, add the following code to your Flutter app:
For a practical example, check out the Netmera Flutter Example Project on GitHub, which shows how to integrate the Flutter SDK and request notification authorization on iOS.
Open your project in Xcode.
Navigate to Signing & Capabilities.
Click + Capability and select Push Notifications.
Use the following methods to manage email subscription preferences in the Netmera SDK:
Check if email subscription is allowed:
Set email subscription preference:
For detailed instructions on using iOS 10 Media Push, refer to the specific documentation > Former iOS Push.
onPrem Configuration
If you're using an onPrem setup, you need to set the base URL for Netmera. You can do this using the following method. This will configure the base URL for your on-premise Netmera setup.
For further details, refer to the documentation for more information.
Flutter SDK integration has been successfully completed, and your devices are now ready to receive the following types of push notifications sent via the Netmera Dashboard:
Standard Push Notifications
Interactive Push Notifications
Widgets
Push Notifications with Deeplinks