'Expo Push Notifications Service/API - How to directi anonymous user to specific webview screen in app after engaging with Push Notification
Using Expo Push Notification Service, I currently send Push Notification to all users, which are anonymous. The only data I am collecting and writing to a Firebase Database is the "ExponentPushToken" that identifies a unique device for sending notifications using expo's service. I have been sending Push Notification in terminal using the following command:
curl -H "Content-Type: application/json" -X POST "https://exp.host/--/api/v2/push/send" -d '{
"to": [
"ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]",
"ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]"
],
"title": "Hello World!",
"body": "Example text here!!",
"sound": "default"
}'
Now, I presume this is not the most flexible way to send notifications, but it allowed me to QUICKLY hit the ground running. My goal now, is to be able to send users that interact with (click/press on) a particular push notification when receiving it on their device to a specific "state" (webview of a particular URL) within the app... I have read through most of the documentation, but I believe some of the content is a bit beyond my ability to interpret regarding what is necessary to make this happen (E.g. setting up Listener's, etc). Wondering if anyone can help simplify implementing this for me? Even if its a bit "janky", I am open to anything!
Solution 1:[1]
You can test to send push using the tool at: https://expo.dev/notifications You can see a Data (JSON) field, where you can send, via push, some additional information (ex: {"id": 3, "idcustomer": 35, "event": "foo"}
When you send this push to a registered token, the app catch events with:
notificationListener.current =
Notifications.addNotificationReceivedListener(notification => {
console.log("Foreground notification", notification)
// setNotification(notification); // Set state
});
// This listener is fired whenever a user taps on or interacts with a notification (works when app is foregrounded, backgrounded, or killed)
responseListener.current = Notifications.addNotificationResponseReceivedListener(response => {
console.log("Interaction done", response);
});
I'm used, when a push arrives while on foreground, to show a Snackbar (RN Paper Snackbar) for an additional interaction. Using {notification} you can show things, navigate or anything else you wanna do with those data.
Don't forget to include:
import * as Notifications from 'expo-notifications';
const notificationListener = useRef();
const responseListener = useRef();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | AleMux |
