This is the third part of the tutorial series on push notifications for an Android react native app. We are building the Retweeter app. And we are all set to give the finishing touches in this post.

In the first part, we covered how to configure react-native-firebase and get the FCM token. We learnt how to send a push notification manually from the Firebase console.

In the second part, we built a Lambda function which sends out a push notification with a list of tweets. We updated the UI to display the tweets.

In this post, we will learn how to display the tweets from the push notification, build another lambda which sets a hourly trigger and implement the Retweet functionality.

Displaying tweets from push notification

We have already configured push notifications in react native app using react-native-firebase. The code below receives the push notification and displays the tweets within it when the app is active.

The message property has the tweets. All we need to do for the tweets to be displayed is to set the tweets in the component state. If the push notification activates the app, we use the getInitialNotification.

Lambda to set hourly trigger

So far, we have built a lambda function to retrieve tweets by hashtag and send a push notification. What remains to be done is trigger the lambda on a hourly basis. To set a hourly trigger, we write another lambda.

The two classes that we use from the AWS SDK are CloudWatchEvents and Lambda.

To create a new rule in CloudWatch events, we use the putRule method. The method accepts the params – rule name and schedule.

The response (data argument) has the RuleARN property which we will use to add a new permission for the lambda function. The addPermission method accepts the lambda function, rule ARN and permitted action to create a new permission. In our case, the permitted action is to invoke the lambda function (lambda:InvokeFunction) with the rule.

The last step is to set a target for the rule using the putTargets method. The CloudWatch event triggers an event based on the schedule. The event is then assigned a payload and is targeted to invoke the lambda. The input payload consists of query (hashtag or search query) and fcm_token (firebase registration token).

Wrap the lambda invocation in an API Gateway. We already have a gateway with an API endpoint of GET /tweets. Create a POST /tweets method. The new method will call our set-rule lambda function with appropriate parameters – query and fcm_token. Finally, we integrate our app with the API gateway.

It is now time to Retweet.

Retweet

Retweeting is quite simple with react-native-twitter. The other twitter libraries work in NodeJS environment. The react-native-twitter is designed to work with react-native. The twitter client requires four parameters as shown below.

After the client is created, we do a simple POST operation providing the tweet id which needs to be retweeted. After the retweet, we update the internal state of the tweet to indicate that the tweet has been retweeted.

The id of the tweet is provided by the Retweet button. Finally, we swap the button component for a text component when the retweet is completed.

Finally, we have come to the end of the tutorial.

Summary

We have covered a lot of ground in the tutorial. We have done the following steps:

  1. Created a react-native app.
  2. Integrated react-native-firebase with the app.
  3. Send a push notification to the device from firebase console.
  4. Built a lambda function to search tweets by keyword.
  5. Setup an API Gateway for the lambda function.
  6. Created the UI for the app to display tweets.
  7. Built a lambda function to send push notification.
  8. Display tweets from the push notification.
  9. Built a lambda to set hourly trigger.
  10. Retweet a tweet displayed in the UI.

There are a few things that we did not cover.

  1. Login using twitter (OAuth).
  2. Allowing the app to work with multiple users.
  3. Change the keyword for tweets.
  4. Pause or stop receiving push notifications.

These features are available in the Retweeter app in the Play store. Fortunately, I have the code in a Git repo for you to follow along. I hope you found the tutorial useful.

Displaying data from push notification
Tagged on:         

Leave a Reply

Your email address will not be published. Required fields are marked *