Please provide the following:
- SDK Version: 39
- Platforms(Android/iOS/web/all): all
I am having difficulty with the new push notification process with the expo upgrade to SDK 39. I was hoping to post what I am doing and try to get some help on how to get it working.
I have a chat section of my app. When a user sends a message, I want to fire off a notification to the receiver.
//chat file
let matchExpoToken = match.info.pushNotificationToken;
if (matchExpoToken != "") {
let title = "New Message";
let messageBody = msg || user.infoData.fullName + " sent you a new message";
//sending the push notification after the chat message has been sent
schedulePushNotification(title, messageBody);
}
//push notifications js file
const schedulePushNotification = async (title, messageBody) => {
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: false,
shouldSetBadge: false,
}),
});
// Second, call the method
Notifications.scheduleNotificationAsync({
content: {
title: title,
body: messageBody,
},
trigger: null,
});
};
What I don’t understand is how we set the token of who we are sending it to. The old expo notification way had a place for the expo token to go in the body of the message. It looked like this:
//old way that made sense to me
const message = {
to: matchExpoToken,
sound: "default",
title: "New Message",
body: messageBody,
data: { data: "new message" },
_displayInForeground: true,
};
const response = await fetch("https://exp.host/--/api/v2/push/send", {
method: "POST",
headers: {
host: "exp.host",
Accept: "application/json",
"Accept-encoding": "gzip, deflate",
"Content-Type": "application/json",
},
body: JSON.stringify(message),
});
Am I missing some documentation? I am looking here and at this snack but both seem to be very simplified ways of sending a notification to everyone? Should I still keep the token in the body like the old way?
Also, Notifications.addListener()
isn’t recognized as a function. When I review the documentation it doesn’t look like there is anything as simple as addListener()
. All of the available functions seems to be a scheduled listener. I assume the correct replacement would be addNotificationReceivedListener but I have tried with no success. What do I replace addListener()
with?
import * as Notifications from "expo-notifications";
const PushNotifications = async (matchExpoToken, messageBody) => {
this._notificationSubscription = Notifications.addListener(
this._handleNotification
);
_handleNotification = notification => {
Vibration.vibrate();
this.setState({ notification: notification });
};
const message = {
to: matchExpoToken,
sound: "default",
title: "New Message",
body: messageBody,
data: { data: "new message" }, //todo: what is this "data"?
_displayInForeground: true,
};
const response = await fetch("https://exp.host/--/api/v2/push/send", {
method: "POST",
headers: {
Accept: "application/json",
"Accept-encoding": "gzip, deflate",
"Content-Type": "application/json",
},
body: JSON.stringify(message),
});
};
export default PushNotifications;