Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to retrieve data when app is in kill/background mode to navigate to specific screen #1036

Open
Raghu-M-S opened this issue May 11, 2024 · 0 comments

Comments

@Raghu-M-S
Copy link

Raghu-M-S commented May 11, 2024

Currently I'm using Notifee to display notification and firebase FCM is my backend.

When app is in Foreground state I'm able to access data payload, based on that i can navigate to specific pages, but when app is in Background and in Kill mode when user tap on the notification, the app is getting open but data payload comes as empty.

So not able to navigate to specific screen

backend code

`try {
console.log("Sending message to token", token);
const response = await fcm.sendEachForMulticast({
tokens: [token],
data: {
notifee: JSON.stringify({
body,
title,
data: { screen: "screen-name" },
}),
data: JSON.stringify({ screen: "screen-name" }),
},

  android: {
    priority: "high",
    notification: {
      title: title,
      body: body,
      sound: "default",
      priority: "high",
    },
    data: {
      screen: "screen-name",
      keyu: "valu",
      data: JSON.stringify({ screen: "screen-name" }),
    },
  },
  apns: {
    payload: {
      aps: {
        contentAvailable: true,
      },
    },
    headers: {
      // "apns-push-type": "background",
      "apns-priority": "5",
      "apns-topic": "com.app.application",
    },
  },
});
console.log("Successfully sent message", response);

} catch (error) {
console.error("Error sending message:", error);
}`

I'm handling in frontend in the below manner

`const setupListeners = () => {
handleForegroundEvents(); //wrote onTap press actions

messaging().onMessage(async (remoteMessage) => {
  await onDisplayNotification(remoteMessage);
});

messaging().onNotificationOpenedApp(async (remoteMessage) => {
  console.log("Notification opened in background:", remoteMessage);
  const screen = remoteMessage.data?.["screen"];
  console.log("remoteMessage.data?", screen);
  // const screen = remoteMessage.data?.screen;
  navigateFromNotification(screen);
  // if (screen) {
  //   navigation.navigate(screen);
  // }
});

Notifications.addNotificationResponseReceivedListener((response) => {
  console.log("Notification response received:", response);
  const screen = response.notification.request.content.data.screen;
  console.log("screen", response.notification);
  // if (screen) {
  //   navigation.navigate(screen);
  // }
});

messaging().setBackgroundMessageHandler(async (remoteMessage) => {
  console.log("Background notification received:", remoteMessage);
  await onDisplayNotification(remoteMessage);
});

messaging()
  .getInitialNotification()
  .then((remoteMessage) => {
    if (remoteMessage) {
      console.log("App launched from notification:", remoteMessage);
      const screen = remoteMessage.data?.["screen"];
      console.log("screen", screen);
    }
  });

};
useEffect(() => {
requestUserPermission();
setupListeners();
}, []);
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant