Notifications.addListener with Notifications.getDevicePushTokenAsync

I am unable to receive data from push notifications received on my standalone app using Expo.Notifications.addListener.

The push notification is not being sent through the Expo push notification service, but by a 3rd party service (Pusher Beams) through APNS. The function Expo.Notifications.addListener is firing, but I receive the following data structure, which contains none of the app-specific event data that we are inputting to Pusher Beams.

Is Expo.addListener compatible with Push Notifications as sent by a service other than Expo? In this case we are using Pusher Beams to deliver a Push Notification and want to respond to it via Expo.addListener.

We have an Expo managed app (not ejected) that is successfully using Expo.getDevicePushTokenAsync to retrieve the device’s push notification token. Note that this is not Expo.getExpoPushTokenAsync which returns an Expo-specific push notification token.

We then use Expo.addListener to listen for an incoming push notification as sent through a 3rd party service (in this case Pusher Beams using the token we obtained with Expo.getDevicePushTokenAsync). When the push notification is sent to the device we are successfully receiving a payload that looks like:

{ remote: true, data: {}, actionId: null, userText: null, origin: ‘selected’ }

So, we know the communication is working. However, the problem here is that there is no “data” transmitted (it is an empty object instead of {“foo”:“bar”} etc). We have tried all possibilities with Pusher Beams (which uses the APNS under the hood) to transmit data.

There is an article here that explains how to format your FCM or APNS payload to work with Expo’s under-the-hood api here, but it does not seem to work:

We are using Expo 33 and my suspicion is that this no longer works. What is the solve?

Hi @shiftio- I’m not sure if this is supported…

When you say your suspicion is that this no longer works, do you mean that the same code worked with a previous SDK?

I have not tested the code with a previous version of the SDK (for example, 32) so I was only mentioning that as a possible reason why this would not work.

I am a little surprised that Expo provides me with getDevicePushTokenAsync (which works and gives me the correct device push token), and then gives me addListener to respond to push notifications on that device (also works), but then does not provide a way for me to then read custom data or data fields from the first argument of addListener (I am always getting data equal to “{}”).

Is there an updated way to format our push notification payload in order for the data attribute to be populated?

@charliecruzan sorry, perhaps I should have added this piece at the beginning of this post:

The ultimate goal here is to get Push Notifications working on iOS and Android devices without using the Expo Push Notification service AND without ejecting from Expo (we want to use full expo, not expo kit). The service we have selected for the push notification part is Pusher Beams.

Hope this sheds more light on what we are trying to achieve.

Is there an updated way to format our push notification payload in order for the data attribute to be populated?

It’s not documented, but the files linked in the post you linked above gives you an idea of the format.

The ultimate goal here is to get Push Notifications working on iOS and Android devices without using the Expo Push Notification service AND without ejecting from Expo (we want to use full expo, not expo kit).

This isn’t officially supported. A lot of third party notification services require changes to native code, which means you’d have to eject or go the bare route (see Bare vs Managed). But if you don’t need to make native changes, then I don’t see why it wouldn’t work

@charliecruzan I just realized something, the post I referenced above on the Expo forum seems to point to Expo code that is on a branch, not master.

Expo Forum Post: Push notifications using native tokens and Expo.
Points to code on a commit not on master: expo/EXRemoteNotificationManager.m at d718b4b8c390c116837c4d440c06085cca8db5b0 · expo/expo · GitHub

So my new question is: Can you link me to the native iOS code in the Expo project where the “addListener” function is being called? I want to examine how that works.

Here’s the master branch of the file you linked, and I’m not 100% sure what you’re looking for in regards to your new question, but here is the js source of the Notifications module.

@charliecruzan thanks. That’s getting closer. Basically I am receiving push notifications but I cannot read any of the custom data fields we are providing via Pusher Beams.

On my iPhone I have this

Expo.Notifications.addListener(payload => alert(JSON.stringify(payload)));

which always produces:

{ remote: true, data: {}, actionId: null, userText: null, origin: ‘selected’ }

and no matter what we try, we cannot figure out how to get “data” or “userText” or whatever to be populated with our test data of { “foo”: “bar” }

Does this help?

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