Push Notifications
Set up push notifications with the Tappd Mobile SDK.
Overview
The SDK allows you to register device push tokens and track push notification subscription status. This enables you to send push notifications to users through the Tappd platform.
Prerequisites
Before setting up push notifications:
- Firebase Cloud Messaging (FCM) for Android
- Apple Push Notification Service (APNs) for iOS
- Push notification certificates/keys configured
- User must be identified first
Installation
React Native Firebase (Recommended)
npm install @react-native-firebase/app @react-native-firebase/messagingFor iOS:
cd ios && pod install && cd ..Alternative: React Native Push Notification
npm install react-native-push-notificationSetup
1. Request Permissions
Request push notification permissions:
import messaging from '@react-native-firebase/messaging';
import { Platform, PermissionsAndroid } from 'react-native';
async function requestPushPermission() {
if (Platform.OS === 'ios') {
const authStatus = await messaging().requestPermission();
const enabled =
authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
authStatus === messaging.AuthorizationStatus.PROVISIONAL;
return enabled;
} else {
// Android - permissions are requested automatically by FCM
return true;
}
}2. Get Push Token
Get the FCM token:
import messaging from '@react-native-firebase/messaging';
async function getPushToken() {
try {
const token = await messaging().getToken();
return token;
} catch (error) {
console.error('Failed to get push token:', error);
return null;
}
}3. Register Token with Tappd
Register the token with Tappd SDK:
import TappdSDK from '@tappd/mobile-sdk';
import messaging from '@react-native-firebase/messaging';
import { Platform } from 'react-native';
// Initialize SDK
const tappd = new TappdSDK({
appId: 'YOUR_APP_ID'
});
// Identify user first
await tappd.identify('user_123', {
email: 'john@example.com'
});
// Get and register token
const token = await messaging().getToken();
if (token) {
await tappd.registerPushToken(
token,
Platform.OS === 'ios' ? 'ios' : 'android'
);
}Complete Setup Example
import React, { useEffect } from 'react';
import messaging from '@react-native-firebase/messaging';
import { Platform } from 'react-native';
import TappdSDK from '@tappd/mobile-sdk';
const tappd = new TappdSDK({
appId: 'YOUR_APP_ID'
});
function App() {
useEffect(() => {
setupPushNotifications();
}, []);
async function setupPushNotifications() {
try {
// Request permission
if (Platform.OS === 'ios') {
const authStatus = await messaging().requestPermission();
const enabled =
authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
authStatus === messaging.AuthorizationStatus.PROVISIONAL;
if (!enabled) {
console.log('Push notifications not authorized');
return;
}
}
// Get token
const token = await messaging().getToken();
if (token && tappd) {
// Register with Tappd (user must be identified first)
await tappd.registerPushToken(
token,
Platform.OS === 'ios' ? 'ios' : 'android'
);
console.log('Push token registered');
}
// Listen for token refresh
messaging().onTokenRefresh(async (newToken) => {
if (tappd) {
await tappd.registerPushToken(
newToken,
Platform.OS === 'ios' ? 'ios' : 'android'
);
console.log('Push token refreshed');
}
});
} catch (error) {
console.error('Push notification setup failed:', error);
}
}
return (
// Your app
);
}Check Subscription Status
Check if a user has subscribed to push notifications:
async function checkSubscription() {
try {
const status = await tappd.checkPushSubscription();
console.log('Subscribed:', status.subscribed);
console.log('Status:', status.status); // 'subscribed', 'opted_in', or 'unsubscribed'
console.log('Device Count:', status.deviceCount);
return status;
} catch (error) {
console.error('Failed to check subscription:', error);
return null;
}
}Handle Token Refresh
Tokens can be refreshed by FCM. Listen for token refresh events:
import messaging from '@react-native-firebase/messaging';
import { Platform } from 'react-native';
messaging().onTokenRefresh(async (newToken) => {
try {
await tappd.registerPushToken(
newToken,
Platform.OS === 'ios' ? 'ios' : 'android'
);
console.log('Token refreshed and registered');
} catch (error) {
console.error('Failed to register refreshed token:', error);
}
});Handle Push Notifications
Foreground Messages
Handle messages when app is in foreground:
import messaging from '@react-native-firebase/messaging';
messaging().onMessage(async (remoteMessage) => {
console.log('Foreground message:', remoteMessage);
// Track notification received event
await tappd.track('push_notification.received', {
notificationId: remoteMessage.messageId,
title: remoteMessage.notification?.title,
body: remoteMessage.notification?.body
});
// Show notification UI
// ...
});Background Messages
Handle messages when app is in background:
// In index.js
import messaging from '@react-native-firebase/messaging';
messaging().setBackgroundMessageHandler(async (remoteMessage) => {
console.log('Background message:', remoteMessage);
// Track notification received
// Note: SDK may not be initialized in background handler
// Track via API directly if needed
});Notification Taps
Handle when user taps a notification:
import messaging from '@react-native-firebase/messaging';
// Check if app was opened from notification
messaging()
.getInitialNotification()
.then(async (remoteMessage) => {
if (remoteMessage) {
// App opened from notification
await tappd.track('push_notification.opened', {
notificationId: remoteMessage.messageId,
title: remoteMessage.notification?.title
});
}
});
// Listen for notification opened while app is running
messaging().onNotificationOpenedApp(async (remoteMessage) => {
await tappd.track('push_notification.opened', {
notificationId: remoteMessage.messageId,
title: remoteMessage.notification?.title
});
});Best Practices
- Identify user first - Push tokens require identified users
- Handle token refresh - Always listen for token refresh events
- Track notification events - Track received and opened events
- Error handling - Wrap token registration in try-catch
- Platform detection - Use
Platform.OSto determine platform
iOS Specific Setup
1. Enable Push Notifications Capability
In Xcode:
- Open your project
- Select your target
- Go to "Signing & Capabilities"
- Click "+ Capability"
- Add "Push Notifications"
2. Configure APNs
Upload your APNs certificate or key to Firebase Console:
- Go to Firebase Console → Project Settings → Cloud Messaging
- Upload APNs Authentication Key or Certificate
Android Specific Setup
1. Configure FCM
- Download
google-services.jsonfrom Firebase Console - Place it in
android/app/ - Add to
android/build.gradle:
dependencies {
classpath 'com.google.gms:google-services:4.3.15'
}- Add to
android/app/build.gradle:
apply plugin: 'com.google.gms.google-services'Troubleshooting
Token Not Registering
Problem: registerPushToken() fails
Solutions:
- Ensure user is identified first
- Check FCM/APNs configuration
- Verify network connectivity
- Check error logs
Notifications Not Received
Problem: Notifications not arriving
Solutions:
- Verify token is registered
- Check subscription status
- Verify FCM/APNs setup
- Check device permissions
Next Steps
- API Reference - Complete method documentation
- React Navigation - Screen tracking integration
- Examples - Complete push notification examples
