Push Notifications
Every plugin covered so far — camera, geolocation, device info, the file system, the in-app browser — works entirely on the device, with a plugin install and some JavaScript. Push notifications are different: they are the first feature in this track that cannot work with just a plugin. They fundamentally require a backend server and a platform push service working together with your app. This page focuses on that architecture and the concepts you need before writing any backend code, since building an actual push backend is beyond a Cordova-focused tutorial.
Why This Always Needs a Server
A push notification, by definition, needs to reach a device even when your app isn't open or running. That's impossible for your app's own JavaScript to arrange by itself — there's no code running to "check for new messages" if the app is closed. Instead, the operating system itself keeps a persistent, low-power connection to a push delivery service (Google's Firebase Cloud Messaging, or FCM, for Android; Apple Push Notification service, or APNs, for iOS), and it's that OS-level service which wakes up and displays the notification — your app code only runs once the user taps it (or, for some notification types, briefly in the background to process it).
The Architecture, Step by Step
| Step | What Happens |
|---|---|
| 1 | Your app launches and registers for push notifications using a Cordova plugin, which talks to FCM (Android) or APNs (iOS) under the hood |
| 2 | FCM/APNs hands back a unique device token identifying this specific app install on this specific device |
| 3 | Your app sends that token to your own backend server and stores it (e.g. tied to that user's account) |
| 4 | Later, your backend decides to send a notification — it calls the FCM/APNs API with the stored device token and a message payload |
| 5 | FCM/APNs delivers the notification to the device — even if your app isn't running at all |
| 6 | The OS displays the notification (background case) or your app's code receives it directly to handle (foreground case) |
Installing a Push Plugin
A commonly used community option for handling Firebase Cloud Messaging in a Cordova app is
cordova-plugin-firebasex, which wraps FCM for Android and APNs (via Firebase) for
iOS behind one JavaScript API:
cordova plugin add cordova-plugin-firebasex
Registering for Push and Getting a Token
Conceptually, the registration flow happens once per install (or whenever the token changes) — your app asks the plugin to register, and the plugin's callback hands back the device token your backend will need. The exact method names vary by plugin, but the general shape looks like this:
document.addEventListener('deviceready', onDeviceReady, false);
function onDeviceReady() {
// Conceptual example -- exact plugin API varies. This registers the device
// and receives back a token identifying this install to FCM/APNs.
FirebasePlugin.getToken(function(token) {
console.log('Got push token: ' + token);
// Send this token to your own backend server so it can target this device later
sendTokenToBackend(token);
}, function(error) {
console.error('Failed to get push token: ' + error);
});
// Listening for a notification the app receives while running
FirebasePlugin.onMessageReceived(function(notification) {
console.log('Notification received: ' + notification.title);
}, function(error) {
console.error('Listener setup failed: ' + error);
});
}
Foreground vs. Background Notifications
How a received notification is handled depends heavily on whether your app was open at the time:
| App State | What Happens |
|---|---|
| Foreground (app open) | The OS does not automatically show a notification banner — your app's JavaScript receives the message data directly, and if you want the user to see a visual alert, you must display it yourself (e.g. a custom in-app banner or a local notification) |
| Background or app closed | The OS automatically displays the notification in the system notification tray using the payload's title/body, with no app code running at all until the user taps it |
iOS Permission Prompts
Unlike Android — which historically allowed apps to receive push notifications without any explicit runtime prompt — iOS requires the user to explicitly opt in via a system permission dialog before your app can display any notification at all. Your registration code needs to request this permission (usually via the same plugin call, or a dedicated permission-request method) and gracefully handle the case where the user declines; your app must keep working normally, just without notifications.
Practical Tips
- Always design your app to function fully even if push registration fails or the user denies permission — never make a core feature depend on notifications being enabled.
- Store the device token server-side keyed to a user/session, and refresh it whenever the plugin reports the token has changed (tokens can rotate).
- Test both foreground and background delivery explicitly — they exercise genuinely different code paths and are easy to only test one of.
- Re-check your chosen push plugin's maintenance status periodically; this is one of the faster-moving corners of the Cordova plugin ecosystem.