expo-notifications dont work when app is not working.

Expo sdk: 40
responseListener.current =
Notifications.addNotificationResponseReceivedListener((response) => {
axios.post(“https://<logUrl.onOurServer.com>”, {
test: “background”,
});
});

This method sends me a notification mail when app is running but if the apps is not running, I can’t capture this event from a cold start. Is there a permission or something for this to work?

addNotificationResponseReceivedListener is only run when you interact with the notification. If you aren’t triggering this when tapping on a notification when the app is killed, you need to mount the listener earlier. The example snack in the docs is a good example and will work whether you are launching the app from a cold start or the background

I did tap on the notification. This morning I’ve tried it on a Iphone and it worked as it should. Android devices still dont work.

Maybe the “axios.post” inside of the callback function don’t work? How can I check this other than doing a http req to myself?

you can Alert() some string Alert - Expo Documentation

I’ve tried that first, didn’t work. I need to send user to a route when they click on a notification, but the event listener don’t fire on android devices if the app is killed.
It did work as expected on ios.

Is this normal, or I need to update to expov42 as written down here?

Moving this outside of useEffect worked:

Notifications.addNotificationResponseReceivedListener((response) => {
  handleNotificationToRoute(response);
});

But that is not how it is in done the docs.

The SDK 42 feature is for handling notifications while the app is backgrounded, I don’t think that’s what you’re trying to do here. If you had to move it outside of useEffect, then that means you’re taking too long to mount that listener. The example from the docs works so you can always work backwards from there

Hello, I´m trying to also use the notifications when the app (iOS) is in background but it does not work (they work when I’m inside the app, though), @alimertcakar do you put Notifications.addNotificationResponseReceivedListener as a function? I have it like this:

cc @charliecruzan

I’ve moved it out of useEffect after many tries.
image

Ok, how do you call those functions outside the useEffect() ? I made a hook, what do you use? A component?

Please let me know how do you call them.

Thank you @alimertcakar

import * as Notifications from “expo-notifications”;

Then just do as i did

@alimertcakar I did this but yet I cannot receive notifications outside of the app, I do receive them if I am inside the app:

comment out removeNotificationSubscription’s. It didn’t cause a problem for me.

@alimertcakar can you copy paste your code? Are you using a hook?

Thank you.

@alimertcakar I did what you say but I still can’t get notifications outside the app on iOS:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.