Firebase Messaging
Smart WebView integrates Firebase Cloud Messaging (FCM) to enable push notifications, allowing you to engage users even when the app isn’t active.
Setup
Refer back to the Getting Started guide for the initial step of adding the platform-specific Firebase configuration file (google-services.json
for Android, GoogleService-Info.plist
for iOS) to your project. This is mandatory for FCM.
How it Works
- Token Generation: The Firebase SDK automatically generates a unique registration token for the device instance.
The Firebase.java
service listens for new tokens (onNewToken
) and stores the latest token inSmartWebView.fcm_token
.Functions.fcm_token()
attempts to retrieve this and set it as a cookie (FCM_TOKEN=...
).The messaging(_:didReceiveRegistrationToken:)
delegate method in AppDelegate receives the token. It needs to be stored or sent to your server. - Receiving Messages:
* Foreground: Firebase.java
’sonMessageReceived
is triggered.sendMyNotification
manually displays the notification. * Background/Closed: The Firebase SDK automatically handles displaying notifications sent with anotification
payload.* Foreground: userNotificationCenter(_:willPresent:withCompletionHandler:)
(UNUserNotificationCenterDelegate) is called. * Background/Closed: System handles the notification display.userNotificationCenter(_:didReceive:withCompletionHandler:)
is called when the user taps the notification.application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
(AppDelegate) is also involved. - Handling Clicks: Notifications can include data payloads (and
click_action
on Android) to control behavior on tap.The default click_action
OPEN_URI
opens the specifieduri
(orASWV_URL
) inMainActivity
.Click handling is done within userNotificationCenter(_:didReceive:withCompletionHandler:)
by inspecting the notification payload.
Sending Notifications
Use the Firebase Console or programmatically via the FCM HTTP v1 API or legacy APIs. The payload structure is mostly platform-agnostic, but platform-specific overrides exist.
Example POST Request (Legacy HTTP API):
{
"to": "DEVICE_REGISTRATION_TOKEN", // <-- Get this from the device
"notification": { // Basic notification payload (handled automatically in background)
"title": "Your Notification Title",
"body": "This is the main message body.",
"click_action": "OPEN_URI" // Android specific click action
// "sound": "default" // iOS uses 'default' or filename
},
"data": { // Custom data payload (always delivered to app)
"uri": "https://your-website.com/specific-page",
"custom_key": "custom_value",
"nid": "unique_notification_id_123"
},
"priority": "high", // Android priority
"content_available": true // iOS flag for background updates
}
Headers:
Content-Type: application/json
Authorization: key=YOUR_SERVER_KEY
(Find Server Key in Firebase Console: Project Settings > Cloud Messaging)
Getting the Device Token:
You need to send the device token generated by the app to your server.
FCM_TOKEN
cookie set by Functions.fcm_token()
, use JavaScript to call a native function, or modify native code to send the token from onNewToken
to your backend.
messaging(_:didReceiveRegistrationToken:)
to your server via an API call.