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.
- Request permissions
- Register for APNS token
- Remote push notifications
- Foreground
- Background
- Opened by tapping on the notification
- Local push notifications
- You'll need to update your
AppDelegate.swift
to handle push check the example app here for an example. - If your AppDelegate is in Objective-C (
.mm|.m|.h
), create a newAppDelegate.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. - 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');
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.
We are open to contributions. Please read our Contributing Guide for more information.
This project is licensed under the terms of the MIT license.
Post in #oss channel in our Discord if you have any questions or want to contribute.