Push notifications
Silent push
The sync push Herald sends so content cards and in-app messages arrive without waiting for the next session, and how to wire it on iOS and Android.
A silent push wakes your app in the background without showing a notification. Herald sends one kind, the sync push, so new content cards and in-app messages reach the device without waiting for the next session.
What a sync push is
A sync push is a data-only push with the key herald_sync set to "1". On iOS it is an APNs background push (content-available: 1, no alert, sound or badge). On Android it is an FCM data message. When the SDK receives one, it fetches cards and in-app messages with the usual ETag check, so an unchanged list costs a 304.
A sync push never shows a notification, is never logged as a push open, and does nothing while the SDK is disabled.
When Herald sends one
- An agent sends a content card or in-app message to a user who has an SDK device, and the user also has an active, unsuppressed push token on the connection. Herald sends one sync push per token, whatever the number of messages.
- Herald sends at most one sync push per user every five minutes. A message written inside that window waits for the next session or the next sync push.
- An admin test of a card or modal sends a sync push and skips the five-minute limit. See Testing push.
The push is queued in the same outbox as other pushes and delivered at once. If it cannot be sent, the message is still stored and the SDK fetches it at the next session. The SDK also skips a sync push that arrives within 30 seconds of the last one it handled.
iOS may delay or drop background pushes, for example when Background App Refresh is off. Treat a sync push as a faster path, not a guarantee.
Before you start
- Push set up: iOS setup or Android setup.
- SDK version 0.2.0 or later.
Wire it up
- In your app target, open Signing & Capabilities, add Background Modes and turn on Remote notifications. In
Info.plistthis addsremote-notificationtoUIBackgroundModes. - Forward the push from your app delegate:
func application(
_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void
) {
Task {
let synced = await Herald.shared.handleRemoteNotification(userInfo)
completionHandler(synced ? .newData : .noData)
}
}handleRemoteNotification runs on the main actor and returns true when the push was a Herald sync push, once the refresh has finished or after 25 seconds at most (iOS allows about 30 for the completion handler); if configure has not run yet, it starts the refresh and returns at once. It returns false for any other notification, so you can handle your own pushes after it. Without the Remote notifications background mode, iOS does not wake the app, and new messages arrive at the next session.
Nothing to add. herald-fcm's HeraldMessagingService receives sync pushes and calls the SDK for you.
If your app has its own FirebaseMessagingService, call one line from onMessageReceived:
override fun onMessageReceived(message: RemoteMessage) {
if (Herald.handleRemoteMessage(message.data)) return
// Your own handling.
}handleRemoteMessage returns true for a sync push and false for any other message. It waits for the refresh to finish (8 seconds at most) before it returns, so FCM keeps the process alive until then.
Verify it works
- Sign in as a test user on a device with push allowed, and put the app in the background.
- Use Send a test from Herald with the Content card channel.
- Open the app. The card is already in the feed, without a session start having fetched it.
Troubleshooting
- iOS never receives it. The Remote notifications background mode is off, or the delegate method is not implemented. Background pushes also do not arrive when the user force-quits the app.
- Android never receives it. Another messaging service receives FCM messages. Remove it, or call
Herald.handleRemoteMessagefrom it. - A second test does nothing within 30 seconds. The SDK skips a sync push that follows another within 30 seconds.
- Cards still wait for the next session. The user has no active push token, or Herald already sent a sync push in the last five minutes.
Keep your own silent pushes
If your app already uses silent pushes from another service, keep them. Herald sync pushes carry herald_sync; on iOS handleRemoteNotification returns false for other notifications, and on Android Herald.handleRemoteMessage returns false.
To refresh at a moment you choose, call refreshContentCards(). It fetches cards and in-app messages together.
Herald.shared.refreshContentCards()Herald.refreshContentCards()