DEV Community

Cover image for Basics and caveats of Expo-notifications

Basics and caveats of Expo-notifications

Mariana Costa on September 13, 2021

Push notifications are currently a widely used functionality in mobile applications. They provide an easy way to establish communication with the u...
Collapse
 
joaquinbian profile image
joaquinbian • Edited

Nice post! I have one question, if I have to send a notification like a reminder of an event, in a specific date, should I use local-notifications? And there is a way to send notifications when the app is killed?

Collapse
 
marianapatcosta profile image
Mariana Costa

Thank you for your comment.
Yes, the local notifications should be used for reminders. And yes, we can receive notifications when the app is killed but, as far as I know, there is no way to react to it, I mean there is no way add a listener and execute some code when a notification is received when the app is killed. Thus, we can receive and read a message, but we cannot increase the badge count when we receive a notification when the app is killed, for example.

Collapse
 
joaquinbian profile image
joaquinbian

Nice! Thanks for the answer! And I can i send these notifications event if they are local-notifications? Or I can receive only notifications from a server when app is killed?

Collapse
 
marianapatcosta profile image
Mariana Costa

Unfortunately, I did not test that but considering that the app triggers a local notification to display in the device when a push notification is received, I believe the behaviour is the same. Thus, we probably can still receive a local notification when the app is killed but we cannot react to it.

Collapse
 
gruckion profile image
Stephen Rayner

Interested to know how to tested this. So we need a real device and we can use the tool to send notifications. But is there a quicker way than uninstalling expo go to reset the app?

I like your idea of remove on logout. But that’s just delete from database, how do I reset to test as if from scratch each time?

Collapse
 
marianapatcosta profile image
Mariana Costa

Thank you for your comment.
I did not understand the first question. Why would you need to uninstall expo go? Nevertheless, to reset your app you don't need to uninstall expo go, you can just clear expo go cache, in "Applications" settings in your device.
Yes, the idea to redefined the token on login is to update the database and be sure that the correct token is being used. As far as I understand reading about expo-notifications package, every time the app is installed you get a new token. So, if you need to test from the scratch, I think it would be enough to uninstall and install the app again.

Collapse
 
adii9 profile image
Aditya Mathur

This is a very informative post. I just had one doubt, I see you are generating the expo push token using Notifications.getExpoPushTokenAsync() which will create a expo-push-token that can only be used when you are running your app using expo go app. Will the push token created by Notifications.getExpoPushTokenAsync() Can be used in physical devices as well?
According to my understanding, when you create the binaries for physical devices I think we need to use getDevicePushTokenAsync() to get a native FCM / APN token which can used with notification services.
You can find more detail here.

Collapse
 
marianapatcosta profile image
Mariana Costa

Hello, thanks for your message.
Yes, the token obtained by calling getExpoPushTokenAsync also works in a physical device, when a production-ready app is running. The article documents the process I went through to implement push notifications in an app I developed for personal usage. I am using it ocasionally and the push notifications work as expected. I am using expo-server-sdk to send notifications from a server.

Collapse
 
rop89 profile image
Rita(they/them) • Edited

Olá Mariana,
Obrigado pelo post.

I have a question though, how do you actually send the push notifications to a specific user? From my understanding this is just the build for sending "general notifications" to all app users but what about if I want to send a notification to a specific user? I am using React Native, mainly targetting IOS, do I need something like Firebase real time DB? Is there any example you can give for specific users notifications?

Desculpa por todas as perguntas, estou ha algum tempo a olhar para isto mas ainda nao consegui achar nada que me fizesse entender como fazer para alcançar o meu objectivo.

Thansk again

Collapse
 
adii9 profile image
Aditya Mathur

The expo notification token that will be generated for each user will be unique. So in your case i.e. you want to send the notification to a specific user, you can save the notification token for each user in the database and a the time of sending the push token you can provide the expo push token code of the target user in 'to' parameter:

body: JSON.stringify({
    to: 'Target-User-expo-push-token',
    data: { extraData: 'Some data' },
    title: 'Sent via the app',
    body: 'This push notification was sent by to app!',
  }),
Enter fullscreen mode Exit fullscreen mode
Collapse
 
caiomars profile image
Caio Mars

Great post! I have one question about sending notification errors, specifically DeviceNotRegistered error, how does one associate a receiptId to an expo token?

I'm sending notifications as chunks, for those users that have uninstalled the app I will get a DeviceNotRegistered error in my receipts (expo.getPushNotificationReceiptsAsync(chunk)) but I haven't figure out how to associate a receipt to a specific expo push token.

Thanks.

Collapse
 
marianapatcosta profile image
Mariana Costa

Thank you for your comment and sorry for the late response. I'm afraid I'll not be able to help you. Since the app I developed is for personal use only, I only implemented the logic to send a notification and generate a log in case an error occurs and it was not sent; I did not implement the logic to deal with the "receipt" of each notification. I've just read the docs(github.com/expo/expo-server-sdk-node) and some possibilities came into my mind, but probably you have already tested them: Did you checked all the information that is being sent in the "ticket" and in the "receipts" (expo.getPushNotificationReceiptsAsync(chunk))? Does the ticket have any information regarding the expo push token that we can relate to the recipient using its ID? Or has the 'details' field of recipient any useful information?

Collapse
 
psnehanshu profile image
Snehanshu Phukon

How to display a message notification with a reply option, similar to WhatsApp?

Collapse
 
dharmeshpathak profile image
dharmeshpathak

Hello guys! I want to know if we can style or customize the notification container in expo-notification ?