Push notifications module
npm install @startupjs/push-notifications!IMPORTANT! The package only works on ios and android platforms!
``js`
@react-native-community/push-notification-ios >= 1.8.0
react-native-push-notification >= 7.2.3
`js`
yarn add @startupjs/push-notifications @react-native-community/push-notification-ios react-native-push-notification
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.
!Certificates, Identifiers & Profiles
Give it a name and enable the Apple Push Notifications service (APNs). Choose Continue and on the next screen choose Register.
!Apple Push Notifications service (APNs)
It is important to record the following three elements from this page:
- Select Download to save the p8 file locally. You will need to upload this to Firebase. You will not be able to download this certificate by leaving this page.Key ID
- Copy and save the .Membership Details
- 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 section.
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.
Add Capabilities : Background Mode - Remote NotificationsMyReactProject/ios
Go into your 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
`js
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)
...
}
`serviceAccount
You can generate a in the Firebase console of your application. Open the Service accounts tab and click Generate new private key.
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.
`js
import { initPushNotifications, notifications } from '@startupjs/push-notifications'
...
App(
...
apps={ ..., notifications }
useGlobalInit=() => {
initPushNotifications()
return true
}
)
`
Arguments
ee (Object) - eventEmitter of server.
Example
`js`
startupjsServer({ ... }, (ee, options) => {
...
initPushNotifications(ee)
...
}
application. How to create a serviceAccount see above.Arguments
serviceAccountPath (String) - the absolute path to serviceAccount.Example
`js
import { initFirebaseApp, initPushNotifications } from '@startupjs/push-notifications/server'init({ orm })
initFirebaseApp(serviceAccountPath)
`$3
The function of sending push notifications. Same as client function.Usage client API
$3
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 and onNotification [Function] fields as this may break the package's behavior.Example
`js
App(
...
useGlobalInit=() => {
initPushNotifications()
return true
}
)
`$3
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
`js
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']})
}
``