in order to send push notifications with expo, we need to get "expo push token" which is unique for each device. Token is an identifies the device so all the release channel (staging/preprod/prod) use the same token for each device. whenever user logins, we ask for notifications and permissions to get expo push token for the device. So when user logs in "initNotification" function gets fired.
Let's say in a multiplayer game app, a player wants to invite the player1, in the app, we send a request to expo to send a notification to player1. expo first will send you "expo push token".this token is going to be saved into database. when we create a table for our push tokens in DB, this table will have One-To-Many relation with the Players. one player can have "many tokens" because one player can log in using multiple devices and each device will have its unique token. Player instance will hold an array of tokens.
When you send a request to Expo, Expo will return you answer. Either an error message or a "ticket". when we get a successful response from expo, response has ticketId. if response has an error for the token, we remove the token from the DB becuase for certain type of errors we need to stop sending multiple notifications.
This ticketId will be stored in the DB. because when we send request to expo and expo sends back its response, even if response is successful it does not mean that notification is received by the device. it just means that the expo received your request to send notification. Expo will then send that notification to Apple or Google and then Apple and Google will send the notification to devices. Using the "ticketId" saved in database, we make a follow up request to expo to check the response that expo received from Apple or Google. if notification was not sent and you try to send notification again, Apple or Google might ban you. that is why we have to remove the tokens from the db. also remove the tickets that handled.
whenever user logs out, we need to remove the token from the db.