OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

can't get notification from expo notification after build apk

  • Thread starter Thread starter Muhammed Walied
  • Start date Start date
M

Muhammed Walied

Guest
I am currently working on a React Native Expo application, and I'm trying to use Expo Notifications. It works well on Expo Go, but after building the APK, I can't receive any notifications. Can anyone help me? this is my app.js code :

Code:
import "react-native-gesture-handler";
import { StatusBar, StyleSheet } from "react-native";
import Stacks from "./src/Routes/Stacks";
import { I18nManager } from "react-native";
import NoConnectionScreen from "./src/Seller Screens/Screens/NoConnectionScreen";
import { useNetInfo } from "@react-native-community/netinfo";
import { useEffect, useRef, useState } from "react";
import * as Device from "expo-device";
import * as Notifications from "expo-notifications";
import AsyncStorage from "@react-native-async-storage/async-storage";
Notifications.setNotificationHandler({
  handleNotification: async () => ({
    shouldShowAlert: true,
    shouldPlaySound: true,
    shouldSetBadge: false,
  }),
});
try {
  I18nManager.allowRTL(false);
  I18nManager.forceRTL(false);
  // the next line is the most effective one
  I18nManager.swapLeftAndRightInRTL(false);
} catch (e) {
  console.log(e);
}
const App = () => {
  state = { rtl: false };
  const [isLoading, setIsLoading] = useState(true);
  const notificationListener = useRef();
  const responseListener = useRef();
  async function registerForPushNotificationsAsync() {
    let token;

    if (Platform.OS === "android") {
      await Notifications.setNotificationChannelAsync("default", {
        name: "default",
        importance: Notifications.AndroidImportance.MAX,
        vibrationPattern: [0, 250, 250, 250],
        lightColor: "#FF231F7C",
      });
    }

    if (Device.isDevice) {
      const { status: existingStatus } =
        await Notifications.getPermissionsAsync();
      let finalStatus = existingStatus;
      if (existingStatus !== "granted") {
        const { status } = await Notifications.requestPermissionsAsync();
        finalStatus = status;
      }
      if (finalStatus !== "granted") {
        alert("Failed to get push token for push notification!");
        return;
      }
      token = await Notifications.getExpoPushTokenAsync({
        projectId: "b37ba3bc-286c-43a3-b3f2-ae2fa40ca6cd",
      });
      const expotoken = token.data;
      console.log(token.data);
      await AsyncStorage.setItem("expotoken", expotoken);
    } else {
      alert("Must use physical device for Push Notifications");
    }

    return token;
  }

  useEffect(() => {
    registerForPushNotificationsAsync();
    notificationListener.current =
      Notifications.addNotificationReceivedListener((notification) => {
        console.log(notification);
      });

    responseListener.current =
      Notifications.addNotificationResponseReceivedListener((response) => {
        console.log(response);
      });

    return () => {
      Notifications.removeNotificationSubscription(
        notificationListener.current
      );
      Notifications.removeNotificationSubscription(responseListener.current);
    };
  }, []);
  setTimeout(() => {
    setIsLoading(false);
  }, 20);
  const net = useNetInfo();

  return (
    <>
      <StatusBar backgroundColor={"#6c757d"} barStyle="light-content" />
      {net.isConnected == true && isLoading == false && <Stacks />}
      {net.isConnected == false && <NoConnectionScreen />}
    </>
  );
};
export default App;
const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#fff",
    alignItems: "center",
    justifyContent: "center",
  },
});

tried useNextNotificationsApi:true and this expo push:android:upload --api-key

<p>I am currently working on a React Native Expo application, and I'm trying to use Expo Notifications. It works well on Expo Go, but after building the APK, I can't receive any notifications. Can anyone help me?
this is my app.js code :</p>
<pre><code>import "react-native-gesture-handler";
import { StatusBar, StyleSheet } from "react-native";
import Stacks from "./src/Routes/Stacks";
import { I18nManager } from "react-native";
import NoConnectionScreen from "./src/Seller Screens/Screens/NoConnectionScreen";
import { useNetInfo } from "@react-native-community/netinfo";
import { useEffect, useRef, useState } from "react";
import * as Device from "expo-device";
import * as Notifications from "expo-notifications";
import AsyncStorage from "@react-native-async-storage/async-storage";
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: true,
shouldSetBadge: false,
}),
});
try {
I18nManager.allowRTL(false);
I18nManager.forceRTL(false);
// the next line is the most effective one
I18nManager.swapLeftAndRightInRTL(false);
} catch (e) {
console.log(e);
}
const App = () => {
state = { rtl: false };
const [isLoading, setIsLoading] = useState(true);
const notificationListener = useRef();
const responseListener = useRef();
async function registerForPushNotificationsAsync() {
let token;

if (Platform.OS === "android") {
await Notifications.setNotificationChannelAsync("default", {
name: "default",
importance: Notifications.AndroidImportance.MAX,
vibrationPattern: [0, 250, 250, 250],
lightColor: "#FF231F7C",
});
}

if (Device.isDevice) {
const { status: existingStatus } =
await Notifications.getPermissionsAsync();
let finalStatus = existingStatus;
if (existingStatus !== "granted") {
const { status } = await Notifications.requestPermissionsAsync();
finalStatus = status;
}
if (finalStatus !== "granted") {
alert("Failed to get push token for push notification!");
return;
}
token = await Notifications.getExpoPushTokenAsync({
projectId: "b37ba3bc-286c-43a3-b3f2-ae2fa40ca6cd",
});
const expotoken = token.data;
console.log(token.data);
await AsyncStorage.setItem("expotoken", expotoken);
} else {
alert("Must use physical device for Push Notifications");
}

return token;
}

useEffect(() => {
registerForPushNotificationsAsync();
notificationListener.current =
Notifications.addNotificationReceivedListener((notification) => {
console.log(notification);
});

responseListener.current =
Notifications.addNotificationResponseReceivedListener((response) => {
console.log(response);
});

return () => {
Notifications.removeNotificationSubscription(
notificationListener.current
);
Notifications.removeNotificationSubscription(responseListener.current);
};
}, []);
setTimeout(() => {
setIsLoading(false);
}, 20);
const net = useNetInfo();

return (
<>
<StatusBar backgroundColor={"#6c757d"} barStyle="light-content" />
{net.isConnected == true && isLoading == false && <Stacks />}
{net.isConnected == false && <NoConnectionScreen />}
</>
);
};
export default App;
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
});
</code></pre>
<p>tried
useNextNotificationsApi:true
and this
expo push:android:upload --api-key </p>
 

Latest posts

D
Replies
0
Views
1
Domantas Giedraitis
D
S
Replies
0
Views
1
Srikanta saw
S
Top