Last active
April 6, 2019 15:32
-
-
Save Eder87rh/9a79db45868ffc8df5f57fb7b95720d8 to your computer and use it in GitHub Desktop.
5 - cropchien - GeneralSubscription - index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const functions = require('firebase-functions'); | |
| const admin = require('firebase-admin'); | |
| const axios = require('axios'); | |
| var cors = require('cors') ({origin:true}); | |
| const serverKey = require('./serverKey'); | |
| admin.initializeApp(functions.config().firebase); | |
| let firestore = admin.firestore() | |
| exports.GeneralSubscription = functions.https.onRequest((request, response) => { | |
| console.log('token',request.body.token); | |
| cors(request,response,function(){ | |
| axios.post(`https://iid.googleapis.com/iid/v1/${request.body.token}/rel/topics/general`, | |
| {}, | |
| { | |
| headers: { | |
| 'Content-Type':'application/json', | |
| 'Authorization': `key=${serverKey}` | |
| }, | |
| }).then((res) => { | |
| firestore.collection('tokens').add({ | |
| token: request.body.token | |
| }).then(ref => { | |
| console.log('Document added ID: ', ref.id); | |
| response.status(200).send(`notifications subscription successful`); | |
| }); | |
| }).catch((err) => { | |
| console.log(err) | |
| console.log(err.response); | |
| response.status(500).send({ | |
| message: 'Whops! there was an error', | |
| error:err.response | |
| }); | |
| }) | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment