🔔 Push Notifications
Shipnative provides comprehensive support for push notifications, enabling you to engage your users with timely and relevant updates. This guide covers both local and remote notifications, deep linking, and platform-specific configurations for iOS (APNs) and Android (FCM).Overview
Key features of Shipnative’s push notification system:- ✅ Local Notifications: Schedule and deliver notifications directly on the user’s device.
- ✅ Remote Notifications: Support for Apple Push Notification service (APNs) for iOS and Firebase Cloud Messaging (FCM) for Android.
- ✅ Deep Linking: Navigate users directly to specific screens within your app when they tap a notification.
- ✅ Mock Mode: Develop and test notification flows without requiring live credentials.
- ✅ Permission Handling: Streamlined process for requesting and managing user notification permissions.
- ✅ Customization: Notification badges, sounds, and categories.
Configuration
iOS Setup (APNs)
For remote push notifications on iOS, you’ll primarily use Apple Push Notification service (APNs). When building with EAS (Expo Application Services), much of this is handled automatically.- Enable Push Notifications Capability:
- In your Apple Developer account, navigate to “Certificates, Identifiers & Profiles”.
- Select your App ID and ensure the “Push Notifications” capability is enabled.
- EAS Build will typically handle the necessary certificates for you.
- Update
app.json: Ensure yourapp.jsonincludes theUIBackgroundModesfor remote notifications and theexpo-notificationsplugin configuration: - Build with EAS:
EAS Build simplifies the process by managing APNs certificates and provisioning profiles.
Android Setup (FCM)
For remote push notifications on Android, you’ll use Firebase Cloud Messaging (FCM).- Create Firebase Project:
- Go to the Firebase Console.
- Create a new project or select an existing one.
- Add an Android app to your Firebase project, ensuring the package name matches your app’s package name in
app.json.
- Download
google-services.json:- From your Firebase project settings, download the
google-services.jsonfile. - Place this file in your
apps/app/directory. EAS Build will automatically detect and use it.
- From your Firebase project settings, download the
- Obtain FCM Server Key:
- In the Firebase Console, navigate to Project Settings > Cloud Messaging.
- Copy your Server key.
- Configure Environment Variable:
Add your FCM Server Key to your
apps/app/.envfile: - Update
app.json: Ensure yourapp.jsonreferences thegoogleServicesFileand includes necessary permissions:
For more detailed information on configuring push notifications with Expo, refer to the official Expo Notifications documentation.
Usage
Shipnative provides auseNotificationStore and utility functions to manage notifications.
Requesting Permission
It’s best practice to request notification permission when the user understands the value they will receive.Scheduling Local Notifications
Local notifications are delivered directly by the device.Sending Remote Notifications (Backend)
Remote notifications are sent from your backend server to APNs (iOS) or FCM (Android) using the Expo Push Notification Service.- Obtain Push Token:
Your app needs to register for push notifications and send the
pushTokento your backend. TheuseNotificationStorehandles obtaining this token. - Send from Backend:
Use the
expo-server-sdkin your backend to send notifications.
For more details on sending notifications from your backend, refer to the Expo Push Notification Service documentation.
Handling Notification Taps (Deep Linking)
When a user taps a notification, you can navigate them to a specific screen using deep linking.API Reference
useNotificationStore
The Zustand store for managing notification state and actions.
notifications Service
Utility functions for direct interaction with expo-notifications.
Advanced Usage
Notification Categories
Define interactive notification categories with custom actions (e.g., “Reply”, “Mark as Read”).Notification Badges
Control the number displayed on your app icon.Testing
Mock Mode Testing
- Ensure no
EXPO_PUBLIC_FCM_SERVER_KEYis set in yourapps/app/.envfile. - Run your app (
yarn iosoryarn android). - Check your console for “Mock Notifications: ENABLED”.
- Schedule a test local notification (e.g., with a 5-second trigger).
- Verify the notification appears on your device/simulator.
Production Testing (iOS)
- Build your app for a physical device using EAS (e.g.,
eas build --platform ios --profile development:device). Push notifications generally do not work reliably on iOS simulators. - Install the app on a physical iOS device.
- Run the app and grant notification permissions.
- Send a test remote notification from your backend.
- Verify the notification appears on the device.
Production Testing (Android)
- Build your app for a device using EAS (e.g.,
eas build --platform android --profile development). - Install the APK on a physical Android device or emulator.
- Run the app and grant notification permissions.
- Send a test remote notification from the Firebase Console or your backend.
- Verify the notification appears on the device.
Troubleshooting
Notifications Not Appearing
- Check Permissions: Ensure the app has been granted notification permissions. You can check the status using
useNotificationStore().permissionStatus. - Check Push Token: Verify that a push token is being generated and sent to your backend.
- iOS Specific: Remote notifications require a physical device. Ensure APNs certificates are correctly configured (EAS usually handles this).
- Android Specific: Ensure
google-services.jsonis correctly placed inapps/app/and yourEXPO_PUBLIC_FCM_SERVER_KEYis correct. - Backend Issues: If sending remote notifications, check your backend logs for errors when calling the Expo Push Notification Service.
Deep Links Not Working
- Check Data Payload: Ensure your notification’s
datapayload correctly specifies thescreenand any necessary parameters. - Verify Router Paths: Confirm that the paths used in your deep linking logic (e.g.,
router.push('/${data.screen}')) match yourexpo-routerconfiguration. - Test Manually: You can test deep links manually using
xcrun simctl openurl booted "your-scheme://your-path"on iOS simulators.
Mock mode in production
Problem: Your app is using mock notification services in production. Solution:- Verify that your
apps/app/.envfile exists and is correctly configured withEXPO_PUBLIC_FCM_SERVER_KEY(for Android). - Ensure your environment variables are prefixed with
EXPO_PUBLIC_. - Restart your Metro bundler with
yarn app:start --clearto ensure environment variables are reloaded. - Review your build configuration to ensure
.envvariables are correctly bundled.