Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Connect Google Play Real-Time Developer Notifications to Firebase Cloud Function via Pub/Sub?

I'm in the final stages of development for my new mobile app, but can't seem to find a way to allow Google Play Real-Time Developer Notifications to communicate to a firebase cloud function via the recommended Google Pub/Sub method.

The expected payload flow should go as follows:

User Purchases Subscription via Play Store > Play Store sends R-T Dev Notification to Pub/Sub > Pub/Sub sends message across to firebase cloud function > Cloud function runs with payload.

I currently have an Apple Developer webhook set-up in a similar way, which webhooks a receipt payload to an iOS cloud function I have setup.

During the pub/sub setup phase, the Pub/Sub page asks to verify the cloud function URL, which I cannot do as I am not the true webmaster of cloud function domain, hence halting me about halfway down the 'Add real-time developer notification' docs that google supplies.

Is there a way to get the RT notification to either a Pub/Sub cloud function or a HTTPS cloud function bypassing the google Pub/Sub and going directly to the webhook or another way to complete the flow above?

The ultimate aim is to provide a way to ensure the purchase made is actually a valid purchase, and not a forged request made by someone intercepting the client > server webhook and submitting one of their own accord.

like image 490
Matthew M Avatar asked Sep 18 '19 20:09

Matthew M


People also ask

How do you trigger a cloud function in Pubsub?

For Cloud Functions (1st gen): In the Trigger type field, select Cloud Pub/Sub. In the Select a Cloud Pub/Sub topic field, select a topic for the trigger to monitor. Messages published to this topic will trigger calls to your function.

What is Pub/Sub in Firebase?

Google Cloud's Pub/Sub is a globally distributed message bus that automatically scales as you need it. You can create a function that handles Pub/Sub events by using functions. pubsub .


1 Answers

After creating the new topic you DO NOT have to create manually a Pub/Sub subscription as explained in the documentation.

To make it work with firebase you have to deploy a new cloud function like this:

exports.YOUR_FUNCTION_NAME = functions.pubsub.topic('YOUR_TOPIC_NAME').onPublish((message) => {

  // Decode the PubSub Message body.
  const messageBody = message.data ? Buffer.from(message.data, 'base64').toString() : null;

  // Print the message in the logs.
  console.log(`Hello ${messageBody || 'World'}!`);
  return null;


});

Please note that you need to replace YOUR_FUNCTION_NAME and YOUR_TOPIC_NAME.

When the deploy of this function finish, you will finally find the function in the subscription list. At this point, you can edit the params of the automatically created subscription, and you will find the url endpoint already filled with an internal url.

Here you can find an example: how to setup a PubSub triggered Cloud Function using the Firebase SDK for Cloud Functions.

like image 71
cark Avatar answered Oct 07 '22 18:10

cark