In my app i used expo push notification it works fine in development in both the device ios and android but after making build apk it works only on ios. I don’t know why its is not working on android.
Here is my notification code
import React, { ReactNode, useEffect, useRef, useState } from "react";
import * as Notifications from "expo-notifications";
import * as Permissions from "expo-permissions";
import Constants from "expo-constants";
import { Platform, View } from "react-native";
import restServices from "../services/restServices";
interface NotificationProps {}
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: false,
shouldSetBadge: false,
}),
});
async function registerForPushNotificationsAsync() {
let token;
if (Constants.isDevice) {
const { status: existingStatus } = await Permissions.getAsync(
Permissions.NOTIFICATIONS
);
let finalStatus = existingStatus;
if (existingStatus !== "granted") {
const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
finalStatus = status;
}
if (finalStatus !== "granted") {
alert("Failed to get push token for push notification!");
return;
}
token = (await Notifications.getExpoPushTokenAsync()).data;
} else {
alert("Must use physical device for Push Notifications");
}
if (Platform.OS === "android") {
Notifications.setNotificationChannelAsync("default", {
name: "default",
importance: Notifications.AndroidImportance.MAX,
vibrationPattern: [0, 250, 250, 250],
lightColor: "#FF231F7C",
});
}
return token;
}
const Notification = () => {
const [expoPushToken, setExpoPushToken] = useState("");
const [notification, setNotification] = useState<boolean | any>(false);
const notificationListener: any = useRef<any>();
const responseListener: any = useRef<any>();
useEffect(() => {
registerForPushNotificationsAsync().then((token: any) =>
setExpoPushToken(token)
);
notificationListener.current = Notifications.addNotificationReceivedListener(
(notification) => {
setNotification(notification);
}
);
responseListener.current = Notifications.addNotificationResponseReceivedListener(
(response) => {
console.log("######res", response);
}
);
return () => {
Notifications.removeNotificationSubscription(notificationListener);
Notifications.removeNotificationSubscription(responseListener);
};
}, []);
console.log("notification", notification);
useEffect(() => {
if (expoPushToken !== "") {
const _rest = new restServices();
_rest
.post(`/notification/token?token=${expoPushToken}`, {})
.then((res) => {
console.log("tokenR", res);
})
.catch((err) => {
console.log("tokenRError", err);
});
}
}, [expoPushToken]);
return null;
};
export default Notification;