React(Web) SDK
Raven React SDK lets you receive and manage push notifications in react webapps. This only works when FCM integration is used to send notifications
Setup
Step 1. Install Raven SDK
Run the following commands your project root directory. Latest version is 1.0.3
Step 2. Initialize Firebase
Firebase will get installed as part of Step 1 and you only need to initialize Firebase on App start/launch. You can get the FIREBASE_CONFIG object and FIREBASE_VAPID_KEY from the Firebase console.
Step 3. Setup Push notifications
- Add the service worker to your public folder and name it as
messaging-sw.js
. You can place it under your own path in the public folder. Just replcae the FIREBASE_CONFIG object with the one retrieved from Firebase and RAVEN_APP_ID with your App Id on Raven console.
Step 4. Server Side: API that returns a unique signature for the user
You will need an API on the server side that returns a unique signature for a user.
- This is required to securely identify the user on Raven without exposing the Raven API Key on the client side.
- The client requests from its server a hash of the userId and Raven API key. All subsequent requests from client to Raven will have this signature.
- Following example shows how to generate a HMAC-SHA256 Signature (hash of
your userId signed with your Raven API Key) in Java -
Add this dependency to
pom.xml
for using HmacUtils to generate signature
- For testing purposes, use the free HMAC-SHA256 hash generator tool. Pass the text as the user identifier and the secret key as your Raven API key.
Step 5. Initialize Raven
5.1 Before user logs in
-
You need to do this step if you want to send notifications to the user who has not logged in yet. You will have to use Firebase topics to send notifications which is described in detail in the Sending Messages section.
-
Now you can use the following method to initialize Raven before login.
- You can get RAVEN_APP_ID from Settings in Raven. tokenHash is generated using the Firebase token. Use the token from Raven SDK in Step 3.2 and generate a hash of the token with the API created in Step 4. The idea is to use the Firebase token as a replacement for userId, till the user logs in.
5.2 After user logs in
-
Once the user logs in, you need to reinitialize Raven.
-
Now you can use the following method to initialize Raven before login.
- You can get RAVEN_APP_ID from Settings in Raven. userIdHash is generated by creating a hash of userId using the API created in Step 4.
Step 6. Handle notification clicks
Listen to the broadcast channel: click-notification
. The clickAction
variable will have the action string which you can use to open a window or take any specific action
Sending messages
Follow the Sending Push doc to setup Firebase and create an event on the Raven console. Once done you can use Raven API to send push notifications.
1. Sending 1:1 to a user
To send a notification directly to a user you can pass either the registered fcm_tokens of the user or just pass the userId. Sending to userId will work if you are using Raven to manage the Firebase tokens. In this case, Raven SDK takes care of managing tokens and will send the notification to all the tokens registered for the userId.
2. Sending to a group of users
To send to a group of users, you can use Firebase Topics. In this case, you will have to register the user to a topic using Raven SDK. The SDK has two methods:
You can pass the list of topics to subscribe a user. Note that this can be done only once you have initialized Firebase, retrieved the Firebase token and initialized Raven. Once subscribed you can pass the fcm_topic in the Raven API -
You are all set! Just fire Raven API and you should see your web push notification 👍 If you are facing any issues, please email us on support@ravenapp.dev