Claude Skills Guide

Claude Code for React Native Push Notifications Guide

Push notifications are essential for React Native mobile apps, enabling you to re-engage users, deliver real-time updates, and drive user retention. This comprehensive guide shows you how to implement push notifications in React Native using Claude Code, covering both Expo-managed workflows and bare React Native projects.

Understanding Push Notifications in React Native

Push notifications in React Native require coordination between multiple components. The device registers with Apple’s Push Notification Service (APNs) for iOS or Firebase Cloud Messaging (FCM) for Android, receiving a unique device token. Your app sends this token to your backend server, which then uses it to send notifications through the respective push service.

There are two primary approaches for implementing push notifications in React Native:

  1. Expo Notifications - Simplifies the entire process with a managed service
  2. react-native-push-notification - Gives you full control with native module integration

Claude Code can help you set up both approaches, generate the necessary code, and troubleshoot any issues that arise during implementation.

Setting Up Expo Push Notifications

If you’re using Expo (recommended for most new projects), push notifications are remarkably straightforward. Expo handles all the native complexity behind the scenes.

Installing and Configuring Expo Notifications

Start by installing the Expo notification package:

npx expo install expo-notifications

Then, configure your app to request notification permissions. Create a utility file to handle permission requests:

// utils/notifications.ts
import * as Notifications from 'expo-notifications';
import * as Device from 'expo-device';
import Platform from 'platform';

export async function requestNotificationPermissions() {
  if (!Device.isDevice) {
    console.log('Push notifications require a physical device');
    return null;
  }

  const { status: existingStatus } = await Notifications.getPermissionsAsync();
  let finalStatus = existingStatus;

  if (existingStatus !== 'granted') {
    const { status } = await Notifications.requestPermissionsAsync();
    finalStatus = status;
  }

  if (finalStatus !== 'granted') {
    console.log('Failed to get push token for notifications');
    return null;
  }

  if (Platform.OS === 'android') {
    await Notifications.setNotificationChannelAsync('default', {
      name: 'default',
      importance: Notifications.AndroidImportance.MAX,
      vibrationPattern: [0, 250, 250, 250],
      lightColor: '#FF231F7C',
    });
  }

  return await Notifications.getExpoPushTokenAsync();
}

Handling Incoming Notifications

Set up notification handlers to respond when notifications arrive while the app is in the foreground:

// App.tsx
import { useEffect, useRef } from 'react';
import * as Notifications from 'expo-notifications';

Notifications.setNotificationHandler({
  handleNotification: async () => ({
    shouldShowAlert: true,
    shouldPlaySound: true,
    shouldSetBadge: false,
  }),
});

export default function App() {
  const notificationListener = useRef();
  const responseListener = useRef();

  useEffect(() => {
    notificationListener.current = Notifications.addNotificationReceivedListener(
      (notification) => {
        console.log('Notification received:', notification);
      }
    );

    responseListener.current = Notifications.addNotificationResponseReceivedListener(
      (response) => {
        console.log('User tapped notification:', response);
        // Navigate to relevant screen based on notification data
      }
    );

    return () => {
      Notifications.removeNotificationSubscription(notificationListener.current);
      Notifications.removeNotificationSubscription(responseListener.current);
    };
  }, []);
  // ... rest of your app
}

Claude Code can help you integrate this with your existing navigation stack and state management.

Implementing Push Notifications in Bare React Native

For projects that require native module access or aren’t using Expo, you’ll need to set up react-native-push-notification manually.

Installing Dependencies

npm install @react-native-community/push-notification-ios
npm install react-native-push-notification

Configuring iOS (APNs)

  1. Enable Push Notifications in your Xcode project
  2. Create an APNs certificate or authentication key in the Apple Developer Portal
  3. Configure your App Delegate to handle push notifications:
// AppDelegate.m
#import <RNCPushNotificationIOS.h>
#import <UserNotifications/UNUserNotificationCenter.h>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  // ... existing code
  
  UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  center.delegate = self;
  
  return YES;
}

// Required for the registration event
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
  [RNCPushNotificationIOS didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

// Required for the registrationError event
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
  [RNCPushNotificationIOS didFailToRegisterForRemoteNotificationsWithError:error];
}

// Required for localNotification event
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)(void))completionHandler
{
  [RNCPushNotificationIOS didReceiveNotificationResponse:response];
  completionHandler();
}

Configuring Android (FCM)

For Android, set up Firebase Cloud Messaging:

  1. Create a Firebase project and download google-services.json
  2. Add the google-services.json to your android/app directory
  3. Configure your AndroidManifest.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission android:name="android.permission.USE_EXACT_ALARM" />

JavaScript Implementation

Create a notification service that works across platforms:

// services/PushNotificationService.ts
import PushNotification from 'react-native-push-notification';
import PushNotificationIOS from '@react-native-community/push-notification-ios';
import { Platform } from 'react-native';

export class PushNotificationService {
  static configure() {
    PushNotification.configure({
      onRegister: function (token) {
        console.log('TOKEN:', token);
        // Send token to your server
      },
      onNotification: function (notification) {
        console.log('NOTIFICATION:', notification);
        
        if (Platform.OS === 'ios') {
          notification.finish(PushNotificationIOS.FetchResult.NoData);
        }
      },
      permissions: {
        alert: true,
        badge: true,
        sound: true,
      },
      popInitialNotification: true,
      requestPermissions: true,
    });

    // Create notification channel for Android
    if (Platform.OS === 'android') {
      PushNotification.createChannel(
        {
          channelId: 'default-channel',
          channelName: 'Default Channel',
          channelDescription: 'Default push notifications channel',
          playSound: true,
          soundName: 'default',
          importance: 4,
          vibrate: true,
        },
        () => {}
      );
    }
  }

  static showNotification(title: string, message: string, data: object = {}) {
    PushNotification.localNotification({
      title: title,
      message: message,
      data: data,
      playSound: true,
      soundName: 'default',
      importance: 4,
      priority: 'high',
    });
  }
}

Best Practices for React Native Push Notifications

Implementing push notifications is only the beginning. Follow these best practices to ensure a great user experience:

Request Permissions Gracefully

Don’t ask for notification permissions immediately when users open your app. Instead, explain the value of notifications first, then request permission when the user performs a relevant action. This improves both permission grant rates and user perception.

Handle Notification Data Carefully

Always validate and sanitize notification payloads on your server and in your app. Never blindly trust notification data, especially when it affects app state or navigation.

Implement Notification Categories

For apps with multiple notification types (alerts, messages, reminders), implement notification categories that let users customize their preferences:

Notifications.setNotificationCategoryAsync('message', [
  {
    identifier: 'reply',
    buttonTitle: 'Reply',
    textInput: { submitTitle: 'Send' },
  },
  {
    identifier: 'dismiss',
    buttonTitle: 'Dismiss',
  },
]);

Track Notification Metrics

Monitor key metrics including delivery rates, open rates, and opt-out rates. Use this data to iterate on your notification strategy and improve user engagement.

Troubleshooting Common Issues

Claude Code can help you debug common push notification problems:

Conclusion

Push notifications are a powerful tool for React Native apps, but implementation requires careful attention to platform-specific requirements. Whether you choose Expo’s managed approach or need full native control with bare React Native, this guide provides the foundation you need to deliver engaging push notifications that drive user retention and improve your app’s success.

Built by theluckystrike — More at zovo.one