Skip to content

React Native push notification bridge

License

Notifications You must be signed in to change notification settings

vijaygojiya/push

 
 

Repository files navigation



bell

Push for React Native


Installation

yarn add @candlefinance/push

The motivation to write this came from the unmaintained and outdatated libraries that exist today. This implementation is written in Swift in less than 200 lines of code.

Andriod support is coming soon. Checkout #1 if you want to help.

Usage

iOS

  • Request permissions
  • Register for APNS token
  • Remote push notifications
    • Foreground
    • Background
    • Opened by tapping on the notification
  • Local push notifications

Setup

  1. You'll need to update your AppDelegate.swift to handle push check the example app here for an example.
  2. If your AppDelegate is in Objective-C (.mm|.m|.h), create a new AppDelegate.swift file and bridging header then delete the Objective-C AppDelegate and main.m file. Finally copy the contents of the example app's AppDelegate.swift and bridge header to your project.
  3. Make sure you're on iOS 15 or later.

The following code is used to handle push notifications on the React Native side:
import push from '@candlefinance/push';

// Shows dialog to request permission to send push notifications, gets APNS token
const isGranted = await push.requestPermissions();

// Get the APNS token w/o showing permission, useful if you want silent push notifications
await push.registerForToken();

// Check permission status: 'granted', 'denied', or 'notDetermined'
const status = await push.getAuthorizationStatus();

// Check if APNS token is registered
const isRegistered = await push.isRegisteredForRemoteNotifications();

// Listeners
push.addListener('notificationReceived', (data) => {
  switch (data.kind) {
     case 'opened':
       console.log('opened');
       break;
     case 'foreground':
     case 'background':
       console.log('foreground/background');
       const { uuid } = data;
       await push.onFinish(uuid);
       break;
  }
  const { title, body } = data;
});

push.addListener('deviceTokenReceived', (token) => {});
push.addListener('errorReceived', (error) => {});

// Remove listeners
push.removeListener('notificationReceived');
push.removeListener('deviceTokenReceived');
push.removeListener('errorReceived');

Testing

If you run the example app, you can test push notifications by running the following command:

yarn push

This will use the payload.json file to send a push notification to the device. You can modify the payload to test different scenarios.

Apple also has a new console to test push notifications. If you print out the token from deviceTokenReceived listener, you can use it to send a push notification from the console.

Contributing

We are open to contributions. Please read our Contributing Guide for more information.

License

This project is licensed under the terms of the MIT license.

Discord

Post in #oss channel in our Discord if you have any questions or want to contribute.

About

React Native push notification bridge

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Swift 33.4%
  • TypeScript 19.9%
  • Ruby 13.7%
  • Kotlin 13.4%
  • Objective-C 8.8%
  • JavaScript 7.6%
  • Objective-C++ 3.2%