0.55.11 • Published 4 months ago

@startupjs/push-notifications v0.55.11

Weekly downloads
-
License
MIT
Repository
-
Last release
4 months ago

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.

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.
  • 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.

Command id and key id

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:

Firebase settings

Then set up your iOS app in the General section of your project settings. Do all the operations indicated:

Add ios app

Then upload your p8 certificate by going to Cloud Messaging in the Firebase project settings. In the APNs Authentication Key section, select Upload.

Add APNs Authentication Key

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:

Apple XCode Capabilities

Background Mode capability and tick Remote Notifications. Push Notifications capability

Background mode

Push Notifications

Remote Notifications

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.

Generate 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']})
}
0.56.0-alpha.7

4 months ago

0.56.0-alpha.0

4 months ago

0.55.11

5 months ago

0.55.7

5 months ago

0.55.8

5 months ago

0.55.1

6 months ago

0.55.0-alpha.12

6 months ago

0.55.0-alpha.1

7 months ago

0.55.0-alpha.0

7 months ago

0.55.0

6 months ago

0.54.1

7 months ago

0.54.2

7 months ago

0.53.3

7 months ago

0.55.0-alpha.25

6 months ago

0.53.0

9 months ago

0.54.0

7 months ago

0.52.1

12 months ago

0.51.0

12 months ago

0.52.0

12 months ago

0.50.13

1 year ago

0.50.12

1 year ago

0.50.5

1 year ago

0.48.0

2 years ago

0.49.0

1 year ago

0.47.0

2 years ago

0.46.0

2 years ago

0.45.0

2 years ago

0.44.0

2 years ago

0.42.0

2 years ago

0.43.0

2 years ago

0.41.2

2 years ago

0.41.0

2 years ago

0.43.1

2 years ago

0.39.11

3 years ago

0.40.0

3 years ago

0.39.0

3 years ago

0.38.0

3 years ago

0.37.0

3 years ago

0.36.0

3 years ago

0.35.10

3 years ago