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

StepWhat Happens
1Your app launches and registers for push notifications using a Cordova plugin, which talks to FCM (Android) or APNs (iOS) under the hood
2FCM/APNs hands back a unique device token identifying this specific app install on this specific device
3Your app sends that token to your own backend server and stores it (e.g. tied to that user's account)
4Later, your backend decides to send a notification — it calls the FCM/APNs API with the stored device token and a message payload
5FCM/APNs delivers the notification to the device — even if your app isn't running at all
6The OS displays the notification (background case) or your app's code receives it directly to handle (foreground case)
Note: Your backend server → FCM/APNs → device → your app is the full round trip. Skipping any one of these pieces (no backend, no registered token, no plugin) means push simply cannot work — this is fundamentally different from, say, the camera plugin, which needs nothing beyond the device itself.

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
Note: Plugin names, maintenance status, and recommended libraries for push notifications change over time as the Cordova and Firebase ecosystems evolve. Before starting a real project, check the plugin's current npm page and GitHub repository for recent commits, open issues, and compatibility with your target Cordova/platform versions — don't assume any specific plugin name will still be the best choice indefinitely.

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);
    });
}
Note: This example is deliberately conceptual rather than a guaranteed-runnable snippet — the exact method names differ between push plugins and change as they're updated. The important part to take away is the shape: register, receive a token, forward the token to your backend, and separately listen for incoming messages.

Foreground vs. Background Notifications

How a received notification is handled depends heavily on whether your app was open at the time:

App StateWhat 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 closedThe 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.
Next steps: Head to Building a Multi-Page UI to structure multiple screens inside your app.