@startupjs/push-notifications v0.55.11
Push Notifications
!IMPORTANT! The package only works on ios and android platforms!
Dependencies
@react-native-community/push-notification-ios >= 1.8.0
react-native-push-notification >= 7.2.3
Installation
yarn add @startupjs/push-notifications @react-native-community/push-notification-ios react-native-push-notification
Connecting certificates
Create p8 certificate
In order to send push notifications to ios devices, you need to create certificates.
Login to your Apple Developer account.
Select Certificates, Identifiers & Profiles
and go to Keys
. Click the + circle button to create a new key.
Give it a name and enable the Apple Push Notifications service (APNs)
. Choose Continue
and on the next screen choose Register
.
It is important to record the following three elements from this page:
- Select
Download
to save thep8
file locally. You will need to upload this toFirebase
. You will not be able to download this certificate by leaving this page. - Copy and save the
Key ID
. - Copy and save your membership ID. It is located next to your name in the upper right corner of the Membership Center or in the
Membership Details
section.
Setting up a Firebase project
You need to connect the p8
certificate to your application in Firebase
. In your Firebase
project, select the gear next to Project Overview
and select Project settings
:
Then set up your iOS app in the General
section of your project settings. Do all the operations indicated:
Then upload your p8
certificate by going to Cloud Messaging
in the Firebase
project settings. In the APNs Authentication Key
section, select Upload
.
Enter the details that you saved in the step of creating the p8
certificate.
iOS
Add Capabilities : Background Mode
- Remote Notifications
Go into your MyReactProject/ios
dir and open MyProject.xcworkspace
workspace with xcode. Select the top project MyProject
and select the Signing & Capabilities
tab. Add a 2 new Capabilities using +
button:
Background Mode
capability and tick Remote Notifications
.
Push Notifications
capability
Connecting the package to your project
server
import { initFirebaseApp, initPushNotifications } from '@startupjs/push-notifications/server'
const serviceAccountPath = path.join(process.cwd(), 'path/to/serviceAccountKey.json')
...
init({ orm })
initFirebaseApp(serviceAccountPath)
...
startupjsServer({
...
}, (ee, options) => {
...
initPushNotifications(ee)
...
}
You can generate a serviceAccount
in the Firebase
console of your application. Open the Service accounts
tab and click Generate new private key
.
client
Call initPushNotifications
in the place where you need to initialize the device token of the current user (devices are written based on the userId from the current session). It makes sense to perform initialization only for an authorized user. But, if necessary, initialization is allowed for each visitor, for this functions can be called directly in the useGlobalInit
callback of App
.
import { initPushNotifications, notifications } from '@startupjs/push-notifications'
...
App(
...
apps={ ..., notifications }
useGlobalInit=() => {
initPushNotifications()
return true
}
)
Usage server API
initPushNotifications (ee)
Initialization of push notifications on the server.
Arguments
ee (Object)
- eventEmitter of server.
Example
startupjsServer({ ... }, (ee, options) => {
...
initPushNotifications(ee)
...
}
initFirebaseApp(serviceAccount)
Initialization of the Firebase
application. How to create a serviceAccount
see above.
Arguments
serviceAccountPath (String)
- the absolute path to serviceAccount
.
Example
import { initFirebaseApp, initPushNotifications } from '@startupjs/push-notifications/server'
init({ orm })
initFirebaseApp(serviceAccountPath)
sendNotification (userIds, options)
The function of sending push notifications. Same as client function.
Usage client API
initPushNotifications (options)
function of initializing push notifications.
Arguments
options [Object]
- an options object for initializing push notifications. A complete list of options can be found in the documentation. !!IMPORTANT!! It is highly discouraged to override the onRegister
andonNotification [Function]
fields as this may break the package's behavior.
Example
App(
...
useGlobalInit=() => {
initPushNotifications()
return true
}
)
sendNotification (userIds, options)
function for sending notifications.
Arguments
userIds [Array]
- array of user id to which push notification will be sent.
options [Object]
:
title [String]
- header string.body [String] (required)
- content string.platforms [Array]
- an array of platforms to send notification to. If not specified, the message will be sent to all registered devices.
Example
function getPlan (id) {
// WARNING: This is abstract example
// This can be any function of yours
return { name: 'silver' }
}
async function subscribe (planId) {
const plan = getPlan(planId)
// plan subscription logic
await sendNotification([userId], { title: 'Subscription ', body: `You have subscribed to plan ${plan.name}`, platforms: ['ios', 'android']})
}
4 months ago
4 months ago
5 months ago
5 months ago
5 months ago
6 months ago
6 months ago
7 months ago
7 months ago
6 months ago
7 months ago
7 months ago
7 months ago
6 months ago
9 months ago
7 months ago
12 months ago
12 months ago
12 months ago
1 year ago
1 year ago
1 year ago
2 years ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago