Push Notifications
Send targeted notifications via Firebase Cloud Messaging
Overview
How push notifications work
Shift2App uses Firebase Cloud Messaging (FCM) to deliver push notifications. Each device gets a unique FCM token that you associate with a user ID for targeted notifications.
Firebase Required
Notifications must be enabled in your dashboard and google-services.json must be configured.
Register User ID
Associate device with user for targeted notifications
shift2appjs.registerFCMToken(userId)
Associates the device FCM token with a user ID. Call this after user logs in.
Parameters
userId
String - Your unique identifier for this user
Returns
void
// Call after user logs in
shift2appjs.registerFCMToken("user_12345");
// Or with email
shift2appjs.registerFCMToken("john@example.com");
Auto Registration
Set a cookie named shift2app_user_id and the app will automatically register the FCM token on each page load.
// Set cookie for automatic registration
document.cookie = "shift2app_user_id=user_12345; path=/; max-age=31536000";
shift2appjs.unregisterFCMToken()
Removes the FCM token association. Call this when user logs out.
Returns
void
// Call on logout
function logout() {
shift2appjs.unregisterFCMToken();
document.cookie = "shift2app_user_id=; path=/; max-age=0";
window.location.href = "/login";
}
Get FCM Token
Retrieve device token for custom integrations
shift2appjs.getFCMToken()
Returns the current device FCM token for custom server implementations.
Returns
String - The FCM token or empty string
// Get and send to your server
const token = shift2appjs.getFCMToken();
if (token) {
fetch("/api/register-device", {
method: "POST",
body: JSON.stringify({ userId: currentUser, fcmToken: token })
});
}
Local Notifications
Show notifications from JavaScript
shift2appjs.showNotification(title, message)
Displays a local notification on the device.
Parameters
title
String - Notification title
message
String - Notification body
Returns
void
// Show notification
shift2appjs.showNotification("Download Complete", "Your file has been downloaded.");
shift2appjs.cancelAllNotifications()
Clears all notifications from the notification tray.
Returns
void
// Clear when app opens
document.addEventListener("visibilitychange", () => {
if (!document.hidden) shift2appjs.cancelAllNotifications();
});
Server-Side Sending
Send notifications from your server
<?php
// PHP example: Send push notification
class FCMSender {
private $serverKey;
public function __construct($serverKey) {
$this->serverKey = $serverKey;
}
public function send($token, $title, $body, $data = []) {
$payload = [
"to" => $token,
"notification" => ["title" => $title, "body" => $body],
"data" => $data
];
$ch = curl_init("https://fcm.googleapis.com/fcm/send");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: key=" . $this->serverKey,
"Content-Type: application/json"
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
return curl_exec($ch);
}
}
// Usage
$fcm = new FCMSender("YOUR_SERVER_KEY");
$fcm->send($userToken, "New Message", "You have a new message!");
// With image and click URL
$fcm->send($userToken, "Sale!", "50% off today!", [
"image" => "https://example.com/sale.jpg",
"url" => "https://example.com/sale",
"send_url" => "true"
]);