create account

[Busy API] Adding support for Expo Notifications by singhpratyush

View this thread on: hive.blogpeakd.comecency.com
· @singhpratyush · (edited)
$159.18
[Busy API] Adding support for Expo Notifications
<center>
![background.png](https://ipfs.busy.org/ipfs/QmXe3UFtX7BtBcZwHJ66i5wNrK2wRB6VAia6wJJ22g3np7)
*Courtesy of @busy.org and [expo.io](https://expo.io/)*
</center>

#### Repository

https://github.com/busyorg/busy-api

---

# 1. Features Added

I worked on adding support for sending notifications to registered [Expo](https://expo.io) devices. More precisely, the features include - 

1. Adding endpoints to register/unregister Expo tokens.
2. Sending push notifications to devices while streaming the blockchain.

---

# 2. The Requirement / Problem Statement

<center>
![Screenshot from 2018-07-12 14-18-40.png](https://ipfs.busy.org/ipfs/QmfFhk6TiqWbt2j6GixAfNk7L6Bq2UJELLvXqnodCiTnNM)
[busyorg/busy#1872](https://github.com/busyorg/busy/issues/1872)
</center>

[bSteem](https://play.google.com/store/apps/details?id=com.bsteem.bsteemapp) uses its own [notification server](https://github.com/Sekhmet/bsteem-notifications/) to process them, while Busy maintains [a different one](https://github.com/busyorg/busy-api). Since this is all repetitive, the two APIs could be combined to serve notifications from the same source of truth.

---

# 3. Implementation Details

## 3.1. Registering/Unregistering Expo Tokens

[`expo-server-sdk`](https://github.com/expo/exponent-server-sdk-node) is used to handle all the operations related to Expo.

### 3.1.1. Expo Push Tokens

Expo push tokens (or simply Expo tokens) are strings that are used to uniquely identify Expo devices. This helps in sending push notifications uniquely to the registered devices with ease, irrespective of the platform on which the app is running (Android or iOS).

<center>
https://docs.expo.io/static/images/generated/v28.0.0/guides/saving-token.png
Expo tokens are typically stored on the server side [(Source)](https://docs.expo.io/versions/latest/guides/push-notifications)
</center>

### 3.1.2. Validating Tokens

`busy-api` uses [`express`](https://expressjs.com/) as the web framework. In order to verify tokens on the notification related endpoints, a custom middleware is used - 

![carbon1.png](https://ipfs.busy.org/ipfs/QmW3rtbPfUn3FtCLcH5g5iaNnXjXZnvKKuKaEV7qetAzVC)

### 3.1.3. Parsing Request Body as JSON

To parse the request body, [`body-parser`]() was added.

![carbon3.png](https://ipfs.busy.org/ipfs/QmRu4mc8NMaPu3cZpYdAuWkrXrWnWShR8PQa6DfiCxedn4)

This would parse body as JSON whenever the `Content-Type` header is set to `application/json`.

### 3.1.3. Authenticating Users

Users are authenticated using the SteemConnect `access_token` that is sent with the request in `Authorization` header. A custom middleware was again used for this purpose - 

![carbon2.png](https://ipfs.busy.org/ipfs/QmZWUKKjaF61TcwR1p6d3UQKma8vzTGQGjou4QFqv7omXn)

> Both the middlewares are taken from [bsteem-notifications](https://github.com/Sekhmet/bsteem-notifications/tree/master/src/middlewares), originally written by @Sekhmet.

### 3.1.4. Storing Expo tokens on Server

Expo tokens are stored in the [Redis](https://redis.io/) datastore on the server. The key namespace looks like `tokens:{username}` and its value includes [`SET`](https://redis.io/commands/set) of tokens. Using SET helps in preventing duplicate tokens from getting registered (which would, in turn, result in duplicate notifications).

## 3.2. Getting Notifications for a User

To get all notifications from a user, the `/notifications` endpoint was introduced. This would again use the user authentication middleware and return all the notifications that are stored in the Redis store for the requesting user.

## 3.3. Sending Pushing Notifications

- Push notifications are sent whenever a Steem block is parsed.
- The message for notification is generated by parsing it. Notifications will be sent for follow, reply, transfer, vote, reblog and mention.
- Tokens to push to are fetched from the Redis store based on the users to notify. One user can have multiple tokens for multiple devices. In this case, all devices will be notified.

![carbon4.png](https://ipfs.busy.org/ipfs/QmcQLbGF6VxAsdshGsA6KzmyBEpbsFHdAUouDxrEEraCZi)

---

#### Pull Request

https://github.com/busyorg/busy-api/pull/12

#### Github Account

https://github.com/singhpratyush

---

> Thanks to @fabien and @Sekhmet for helping with this patch.
πŸ‘  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
πŸ‘Ž  
properties (23)
authorsinghpratyush
permlinkbusy-api-adding-support-for-expo-notifications
categoryutopian-io
json_metadata{"community":"busy","app":"busy/2.5.2","format":"markdown","tags":["utopian-io","development","busy","bsteem","steem"],"users":["busy.org","Sekhmet.","fabien","Sekhmet"],"links":["/@busy.org","https://expo.io/","https://github.com/busyorg/busy-api","https://expo.io","https://github.com/busyorg/busy/issues/1872","https://play.google.com/store/apps/details?id=com.bsteem.bsteemapp","https://github.com/Sekhmet/bsteem-notifications/","https://github.com/busyorg/busy-api","https://github.com/expo/exponent-server-sdk-node","https://docs.expo.io/versions/latest/guides/push-notifications"],"image":["https://ipfs.busy.org/ipfs/QmXe3UFtX7BtBcZwHJ66i5wNrK2wRB6VAia6wJJ22g3np7","https://ipfs.busy.org/ipfs/QmfFhk6TiqWbt2j6GixAfNk7L6Bq2UJELLvXqnodCiTnNM","https://docs.expo.io/static/images/generated/v28.0.0/guides/saving-token.png","https://ipfs.busy.org/ipfs/QmW3rtbPfUn3FtCLcH5g5iaNnXjXZnvKKuKaEV7qetAzVC","https://ipfs.busy.org/ipfs/QmRu4mc8NMaPu3cZpYdAuWkrXrWnWShR8PQa6DfiCxedn4","https://ipfs.busy.org/ipfs/QmZWUKKjaF61TcwR1p6d3UQKma8vzTGQGjou4QFqv7omXn","https://ipfs.busy.org/ipfs/QmcQLbGF6VxAsdshGsA6KzmyBEpbsFHdAUouDxrEEraCZi"]}
created2018-07-12 10:00:09
last_update2018-07-12 10:07:12
depth0
children3
last_payout2018-07-19 10:00:09
cashout_time1969-12-31 23:59:59
total_payout_value119.617 HBD
curator_payout_value39.566 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length4,277
author_reputation7,035,648,262,478
root_title"[Busy API] Adding support for Expo Notifications"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id64,390,802
net_rshares72,256,148,135,976
author_curate_reward""
vote details (41)
@helo ·
$0.02
* Very beautiful post, I love the images frames.
* Great coding as well, lets add more comments next time.


Your contribution has been evaluated according to [Utopian policies and guidelines](https://join.utopian.io/guidelines), as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, [click here](https://review.utopian.io/result/3/1211114).

---- 
Need help? Write a ticket on https://support.utopian.io/. 
Chat with us on [Discord](https://discord.gg/uTyJkNm). 
[[utopian-moderator]](https://join.utopian.io/)
πŸ‘  , ,
properties (23)
authorhelo
permlinkre-singhpratyush-busy-api-adding-support-for-expo-notifications-20180713t164750625z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"links":["https://join.utopian.io/guidelines","https://review.utopian.io/result/3/1211114","https://support.utopian.io/","https://discord.gg/uTyJkNm","https://join.utopian.io/"],"app":"steemit/0.1"}
created2018-07-13 16:47:51
last_update2018-07-13 16:47:51
depth1
children1
last_payout2018-07-20 16:47:51
cashout_time1969-12-31 23:59:59
total_payout_value0.016 HBD
curator_payout_value0.003 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length598
author_reputation121,547,934,535,311
root_title"[Busy API] Adding support for Expo Notifications"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id64,556,745
net_rshares9,339,870,729
author_curate_reward""
vote details (3)
@singhpratyush ·
Hi @helo!

Thanks for reviewing. Will surely add more comments next time.
properties (22)
authorsinghpratyush
permlinkre-helo-re-singhpratyush-busy-api-adding-support-for-expo-notifications-20180713t185756393z
categoryutopian-io
json_metadata{"community":"busy","app":"busy/2.5.2","format":"markdown","tags":["utopian-io"],"users":["helo"],"links":["/@helo"],"image":[]}
created2018-07-13 18:58:00
last_update2018-07-13 18:58:00
depth2
children0
last_payout2018-07-20 18:58:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length73
author_reputation7,035,648,262,478
root_title"[Busy API] Adding support for Expo Notifications"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id64,569,981
net_rshares0
@utopian-io ·
Hey @singhpratyush
**Thanks for contributing on Utopian**.
We’re already looking forward to your next contribution!

**Want to chat? Join us on Discord https://discord.gg/h52nFrV.**

<a href='https://v2.steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1'>Vote for Utopian Witness!</a>
πŸ‘  
properties (23)
authorutopian-io
permlinkre-busy-api-adding-support-for-expo-notifications-20180714t175008z
categoryutopian-io
json_metadata"{"app": "beem/0.19.42"}"
created2018-07-14 17:50:09
last_update2018-07-14 17:50:09
depth1
children0
last_payout2018-07-21 17:50:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length305
author_reputation152,955,367,999,756
root_title"[Busy API] Adding support for Expo Notifications"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id64,673,659
net_rshares5,838,392,836
author_curate_reward""
vote details (1)