Push Notification Setup
Implement push notifications across mobile and web platforms.
Firebase Cloud Messaging (React Native)
import messaging from '@react-native-firebase/messaging';
// Request permission
async function requestPermission() {
const status = await messaging().requestPermission();
if (status === messaging.AuthorizationStatus.AUTHORIZED) {
const token = await messaging().getToken();
await sendTokenToServer(token);
}
}
// Handle foreground messages
messaging().onMessage(async message => {
console.log('Foreground message:', message);
showLocalNotification(message);
});
// Handle background/quit messages
messaging().setBackgroundMessageHandler(async message => {
console.log('Background message:', message);
});
// Handle token refresh
messaging().onTokenRefresh(token => {
sendTokenToServer(token);
});
iOS Native (Swift)
import UserNotifications
func requestAuthorization() {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in
if granted {
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
}
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
sendTokenToServer(token)
}
Android (Kotlin)
class MyFirebaseService : FirebaseMessagingService() {
override fun onNewToken(token: String) {
sendTokenToServer(token)
}
override fun onMessageReceived(message: RemoteMessage) {
message.notification?.let {
showNotification(it.title, it.body)
}
}
private fun showNotification(title: String?, body: String?) {
val channelId = "default"
val notification = NotificationCompat.Builder(this, channelId)
.setContentTitle(title)
.setContentText(body)
.setSmallIcon(R.drawable.ic_notification)
.build()
NotificationManagerCompat.from(this).notify(0, notification)
}
}
Best Practices
- Request permission at appropriate time
- Handle token refresh
- Support notification channels (Android)
- Implement deep linking
- Never send sensitive data in payload
- Test on real devices
1---2name: push-notification-setup3description: Implements push notifications across iOS, Android, and web using Firebase Cloud Messaging and native services. Use when adding notification capabilities, handling background messages, or setting up notification channels.4metadata:5 source: https://skillmd.com/skills/secondsky/push-notification-setup6 keywords: "push notifications, FCM, Firebase Cloud Messaging, iOS notifications, Android notifications, web notifications, React Native notifications, notification permissions, background messages, notification channels, APNS, APNs, notification tokens, remote notifications, foreground notifications, notification handlers"7license: MIT8---910# Push Notification Setup1112Implement push notifications across mobile and web platforms.1314## Firebase Cloud Messaging (React Native)1516```javascript17import messaging from '@react-native-firebase/messaging';1819// Request permission20async function requestPermission() {21 const status = await messaging().requestPermission();22 if (status === messaging.AuthorizationStatus.AUTHORIZED) {23 const token = await messaging().getToken();24 await sendTokenToServer(token);25 }26}2728// Handle foreground messages29messaging().onMessage(async message => {30 console.log('Foreground message:', message);31 showLocalNotification(message);32});3334// Handle background/quit messages35messaging().setBackgroundMessageHandler(async message => {36 console.log('Background message:', message);37});3839// Handle token refresh40messaging().onTokenRefresh(token => {41 sendTokenToServer(token);42});43```4445## iOS Native (Swift)4647```swift48import UserNotifications4950func requestAuthorization() {51 UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in52 if granted {53 DispatchQueue.main.async {54 UIApplication.shared.registerForRemoteNotifications()55 }56 }57 }58}5960func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {61 let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()62 sendTokenToServer(token)63}64```6566## Android (Kotlin)6768```kotlin69class MyFirebaseService : FirebaseMessagingService() {70 override fun onNewToken(token: String) {71 sendTokenToServer(token)72 }7374 override fun onMessageReceived(message: RemoteMessage) {75 message.notification?.let {76 showNotification(it.title, it.body)77 }78 }7980 private fun showNotification(title: String?, body: String?) {81 val channelId = "default"82 val notification = NotificationCompat.Builder(this, channelId)83 .setContentTitle(title)84 .setContentText(body)85 .setSmallIcon(R.drawable.ic_notification)86 .build()8788 NotificationManagerCompat.from(this).notify(0, notification)89 }90}91```9293## Best Practices9495- Request permission at appropriate time96- Handle token refresh97- Support notification channels (Android)98- Implement deep linking99- Never send sensitive data in payload100- Test on real devices