Please provide the following:
- SDK Version: 42
- Platforms(Android/iOS/web/all): Android
- Add the appropriate “Tag” based on what Expo library you have a question on.
I’m using “expo-notifications”, and Listeners working perfectly as I’m expecting with “scheduleNotificationAsync” only, not with the FCM notifications.
I’m getting notifications from FCM requests (PHP code) when APP in the background only, But nothing happens when I clicking on a notification even APP not opening and listening to notification listeners.
Please help me ASAP
Using following code - app.js
import Constants from "expo-constants";
import * as Notifications from "expo-notifications";
import React, { useState, useEffect, useRef } from "react";
import { Text, View, Button, Platform, Clipboard, Alert } from "react-native";
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: false,
priority: Notifications.AndroidNotificationPriority.HIGH,
}),
});
export default function App() {
const [expoPushToken, setExpoPushToken] = useState("");
const [notification, setNotification] = useState(false);
const notificationListener = useRef();
const responseListener = useRef();
useEffect(() => {
registerForPushNotificationsAsync().then((token) =>
setExpoPushToken(token)
);
notificationListener.current =
Notifications.addNotificationReceivedListener((notification) => {
Alert.alert("notification ==>>", JSON.stringify(notification));
setNotification(notification);
});
responseListener.current =
Notifications.addNotificationResponseReceivedListener((response) => {
Alert.alert("notification response ==>>", JSON.stringify(response));
setNotification(response);
});
return () => {
Notifications.removeNotificationSubscription(notificationListener);
Notifications.removeNotificationSubscription(responseListener);
};
}, []);
const copyToClipboard = () => {
Clipboard.setString(expoPushToken);
};
return (
<View
style={{
flex: 1,
alignItems: "center",
justifyContent: "space-around",
}}
>
<Text>Your expo push token: {expoPushToken}</Text>
<View style={{ alignItems: "center", justifyContent: "center" }}>
<Text>
CompleteContent: {notification && JSON.stringify(notification)}{" "}
</Text>
</View>
<Button title="Press to Copy" onPress={copyToClipboard} />
<Button
title="Press to schedule a notification"
onPress={async () => {
await schedulePushNotification();
}}
/>
</View>
);
}
async function schedulePushNotification() {
await Notifications.scheduleNotificationAsync({
content: {
title: "You've got mail! 📬",
body: "Here is the notification body",
data: { data: "goes here" },
},
trigger: { seconds: 2 },
});
}
async function registerForPushNotificationsAsync() {
let token;
if (Constants.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.getDevicePushTokenAsync()).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;
}
Using following code in app.json
{
"expo": {
"name": "Coin",
"slug": "Coin",
"version": "1.0.0",
"orientation": "portrait",
"icon": "./assets/icon.png",
"notification": {
"iosDisplayInForeground": true
},
"splash": {
"image": "./assets/splash.png",
"resizeMode": "contain",
"backgroundColor": "#ffffff"
},
"updates": {
"fallbackToCacheTimeout": 0,
"enabled": false
},
"assetBundlePatterns": ["**/*"],
"ios": {
"supportsTablet": true
},
"android": {
"useNextNotificationsApi": true,
"package": "com..........",
"googleServicesFile": "./google-services.json",
"adaptiveIcon": {
"foregroundImage": "./assets/adaptive-icon.png",
"backgroundColor": "#FFFFFF"
}
},
"web": {
"favicon": "./assets/favicon.png"
}
}
}