I am following GitHub code on how to implement push notification based on realtime database triggers.
Here is the code and the link:
https://github.com/firebase/functions-samples/blob/master/fcm-notifications/functions/index.js
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
/**
* Triggers when a user gets a new follower and sends a notification.
*
* Followers add a flag to `/followers/{followedUid}/{followerUid}`.
* Users save their device notification tokens to `/users/{followedUid}/notificationTokens/{notificationToken}`.
*/
exports.sendFollowerNotification = functions.database.ref('/followers/{followedUid}/{followerUid}').onWrite(event => {
const followerUid = event.params.followerUid;
const followedUid = event.params.followedUid;
// If un-follow we exit the function.
if (!event.data.val()) {
return console.log('User ', followerUid, 'un-followed user', followedUid);
}
console.log('We have a new follower UID:', followerUid, 'for user:', followerUid);
// Get the list of device notification tokens.
const getDeviceTokensPromise = admin.database().ref(`/users/${followedUid}/notificationTokens`).once('value');
// Get the follower profile.
const getFollowerProfilePromise = admin.auth().getUser(followerUid);
return Promise.all([getDeviceTokensPromise, getFollowerProfilePromise]).then(results => {
const tokensSnapshot = results[0];
const follower = results[1];
// Check if there are any device tokens.
if (!tokensSnapshot.hasChildren()) {
return console.log('There are no notification tokens to send to.');
}
console.log('There are', tokensSnapshot.numChildren(), 'tokens to send notifications to.');
console.log('Fetched follower profile', follower);
// Notification details.
const payload = {
notification: {
title: 'You have a new follower!',
body: `${follower.displayName} is now following you.`,
icon: follower.photoURL
}
};
// Listing all tokens.
const tokens = Object.keys(tokensSnapshot.val());
// Send notifications to all tokens.
return admin.messaging().sendToDevice(tokens, payload).then(response => {
// For each message check if there was an error.
const tokensToRemove = [];
response.results.forEach((result, index) => {
const error = result.error;
if (error) {
console.error('Failure sending notification to', tokens[index], error);
// Cleanup the tokens who are not registered anymore.
if (error.code === 'messaging/invalid-registration-token' ||
error.code === 'messaging/registration-token-not-registered') {
tokensToRemove.push(tokensSnapshot.ref.child(tokens[index]).remove());
}
}
});
return Promise.all(tokensToRemove);
});
});
});
My silly question, new to Functions and Node, is in this code notifications are sent to all users who tokens are saved, is that correct? and if it is how can I let's say send just to one particular person instead all?
I was thinking of saving token of each user in different nodes (children) so I can pick the one I want to send notification to. Does it work?
Thanks All
This code will send notification to just one user (follower in this example). This user can have multiple tokens, representing multiple devices, and hence the variable name: tokensSnapshot.
What you intend to do is very doable with Cloud Functions. You just have to be careful with paths of your nodes where you save users, or tokens, for instance. Also as Frank van Puffelen suggested, having some acquaintance with Admin SDK (Realtime Database and FCM) will really help you out.
Related
While wokring with gprc in dart, if the response type of the very first rpc call is a streaming response the client app fails to connect to the server when stream handler is envoked. I found the issue while building upon the helloworld example from the package.
Is there any way to ensure that the connection is established? Or is there is anything I am doing incorrectly?
I have tried it with await channel.getConnection(); but it makes no difference.
grpc version: 3.0.2
helloworld.proto:
// Copyright 2015 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
syntax = "proto3";
option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";
option objc_class_prefix = "HLW";
package helloworld;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
rpc SayHelloStream (HelloRequest) returns (stream HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
server.dart:
// Copyright (c) 2018, the gRPC project authors. Please see the AUTHORS file
// for details. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/// Dart implementation of the gRPC helloworld.Greeter server.
import 'package:grpc/grpc.dart';
import 'package:helloworld/src/generated/helloworld.pbgrpc.dart';
class GreeterService extends GreeterServiceBase {
#override
Stream<HelloReply> sayHelloStream(
ServiceCall call, HelloRequest request) async* {
for (var i = 0; i < 10; i++) {
yield HelloReply()..message = 'Hello, ${request.name}!';
await Future.delayed(Duration(seconds: 1));
}
}
#override
Future<HelloReply> sayHello(ServiceCall call, HelloRequest request) async {
return HelloReply()..message = 'Hello, ${request.name}!';
}
}
Future<void> main(List<String> args) async {
final server = Server(
[GreeterService()],
const <Interceptor>[],
CodecRegistry(codecs: const [GzipCodec(), IdentityCodec()]),
);
await server.serve(port: 50051);
print('Server listening on port ${server.port}...');
}
client.dart
// Copyright (c) 2018, the gRPC project authors. Please see the AUTHORS file
// for details. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/// Dart implementation of the gRPC helloworld.Greeter client.
import 'package:grpc/grpc.dart';
import 'package:helloworld/src/generated/helloworld.pbgrpc.dart';
Future<void> main(List<String> args) async {
final channel = ClientChannel(
'localhost',
port: 50051,
options: ChannelOptions(
credentials: ChannelCredentials.insecure(),
codecRegistry:
CodecRegistry(codecs: const [GzipCodec(), IdentityCodec()]),
),
);
final stub = GreeterClient(channel);
final name = args.isNotEmpty ? args[0] : 'world';
try {
// works if this call is made first
// final response = await stub.sayHello(HelloRequest()..name = name);
// print('Greeter client received: ${response.message}');
// this has no effect
// await channel.getConnection();
final responseStream = stub.sayHelloStream(
HelloRequest()..name = name,
);
// This doesn't work standalone.
responseStream
.listen((value) => print('Greeter client received: ${value.message}'));
// Works when using await for
// await for (var value in responseStream) {
// print('Greeter client received: ${value.message}');
// }
} catch (e) {
print('Caught error: $e');
}
await channel.shutdown();
}
Expected result: It should have worked correctly and printed Greeter client received: ${value.message}' 10 times at 1 second interval.
Actual result: On running client.dart the following error is recieved.
gRPC Error (code: 14, codeName: UNAVAILABLE, message: Error connecting: Connection shutting down., details: null, rawResponse: null, trailers: {})
On adding the following lines (as shown in comments) there are no issues and the result is printed 1 + 10 times as expected.
// final response = await stub.sayHello(HelloRequest()..name = name);
// print('Greeter client received: ${response.message}');
You should only shutdown the channel when you're done with the stream. In your case you shutdown the channel immediately so there's no way gRPC would keep updating the stream as you've already shutdown the connection.
I have successfully added rapidoreach ios app monetization SDK into my app. i am not very much aware about server to server callbacks..Any idea how it can be done with nodejs and express?
You can use request method which comes by default with node.js or by using axios library for server to server callback, but a quick check at rapidoreach site (https://www.rapidoreach.com/docs#/callbacks) leads to this doc for callback setup and looks like you want to receive callbacks, in that case you have to setup a api at your end to receive their server call, this is one example done in Nodejs
Example:
function rapidoreachPostback(req: Request, res: Response) {
var IP = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
var Input = req.query;
if (Input.status == 'C' || Input.status == "P" || Input.status == "F") {
if(Input.status === "C"){
//record your completion here
}
//if rapidoreach allows disaqualification points
if(Input.status === "P"){
//record your disqualification transaction over here
}
if(Input.status === "F"){
//record your survey offer failure transaction over here
}
}
} catch (error) {
// send "1" in response to server call from rapidoreach.
res.send("1");
return;
}
res.send("1");
return;
})
return;
}
res.send("1");
return;
}
Rapidoreach will send server to server callback using get request to the callback URL maintained in App setings.
Assuming Callback URL in app settings is set as
https://local.callback.com/callback.php
Below is an example of server to server callback. Details of parameters available in callback can be found in their official docs
https://www.rapidoreach.com/docs#/callbacks
https://local.callback.com/callback.php?cmd=P&userId=SYKUser-CvkBPlfibeV-aba04fe2fc72219fdbb9a9353a68713d&amt=0.01&offerInvitationId=trans0002&status=P&offerHash=4fdcc6461ec225ba8af3bc66ccf4017c¤cyAmt=0.80&transactionId=trans0002&endUserId=SYKUser&txnHash=d5fecc45d5be250ff757f8694a6d65a1&useragent=Rapidoreach¤cyName=Local Coins&offerType=1&deviceType=Desktop&intergrationMethod=IFRAME
Here is an example how you can handle callback using PHP.
File callback.php
<?php
/**
* ApplicationKey can be found in credentials tab of app created in Rapidoreach
*/
$ApplicationKey = "<Replace this>"; //<- replace this with real application key
/**
* $endUserId is unique endUserId in publisher's system.
*/
$EndUserId = $_REQUEST['endUserId'];
/**
* Unique offer Id
*/
$OfferInvitationId = $_REQUEST['offerInvitationId'];
/**
* TransactionId
*/
$TransactionId = $_REQUEST['transactionId'];
/**
* Status
* C: Completed - User has successfully completed an offer and should be rewarded with currencyAmt
* P: Attempted - User has attempted an offer and attempt is valid will be rewarded with currencyAmt
* currencyAmt will not be full in this case and will be equal to screenout reward maintained in App Setting of Rapidoreach Publisher portal
* F: Failed - User has failed to complete an offer and hence terminated. No rewards should be awarded
*/
$Status = $_REQUEST['status'];
/**
* Validate offerHash
* You should verify oidHash upon receipt of the callback by recomputing it with the callback offerInvitationId and your ApplicationKey.
* This will secure against users faking their own id and passing it in if by some chance they come across the script.
*/
$offerHash = $_REQUEST['offerHash'];
$CalculatedOfferHash = md5($OfferInvitationId . $ApplicationKey);
if ($offerHash != $CalculatedOfferHash) {
/**
* TODO: User is trying to manupulate offerId to get credits. Flag this user within this condition if required (optional).
* Do not credit this user and send success response to Rapidoreach
* Do not continue further
*/
echo "1";
die;
}
/**
* Validate txnHash
*/
$txnHash = $_REQUEST['txnHash'];
$CalculatedTxnHash = md5($TransactionId . $ApplicationKey);
if ($txnHash != $CalculatedTxnHash) {
/**
* TODO: User is trying to manupulate transaction id to get credits. Flag this user within this condition if required (optional).
* Do not credit this user and send success response to Rapidoreach
* Do not continue further
*/
echo "1";
die;
}
/**
* Credit the user based on offer status
*/
switch ($Status) {
case 'C':
# TODO: Credit the $EndUserId with $currencyAmt use $TransactionId to avoid duplicates
echo "1";
die;
break;
case 'P':
# TODO: Credit the $EndUserId with $currencyAmt use $TransactionId to avoid duplicates
echo "1";
die;
break;
case 'F':
# TODO: User has failed to complete an offer
echo "1";
die;
break;
default:
# code...
break;
}
I am trying to set up a Google Cloud Function (GCF) to handle Subscription Notifications from Apple. I am familiar with GCF, but not very familiar with writing my own REST API and the Nodejs methods of handling the data Apple sends with the notification. I am receiving the Apple notification, but only a "chunk" of it. Here's my code (using express and body-parser frameworks). I put my whole function here to help people since there is absolutely nothing about how to use GCF for Subscription Notifications anywhere I could find on the web (note this code is very much a work in progress and I am new to Nodejs):
// Set up express object
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
exports.iosNotification = functions.https.onRequest((req, res) => {
console.log("We are receiving a request from Apple.");
app.use(bodyParser.json());
let receipt = req.body.latest_receipt;
console.log(req.body);
const chunks = [];
req.on('data', chunk => {
chunks.push(chunk);
console.log('A chunk of data has arrived:', chunk);
});
req.on('end', () => {
const data = Buffer.concat(chunks);
console.log('Data: ', data);
console.log('No more data');
});
const type = req.body.notification_type;
console.log("Notification type: ", type);
const lri = req.body.latest_receipt_info;
console.log(lri, receipt);
// Verify the receipt.
validateAppleReceipt(receipt)
.then((appleResponse) => {
console.log("Receipt from App Store server validated.", appleResponse);
res.sendStatus(200);
const oTxId = appleResponse.latest_receipt_info[0].original_transaction_id;
// Receipt is valid and we let Apple know. Let's process the notification.
switch (type) {
case 'CANCEL':
// User canceled the subscription (like on accidental purchase).
console.log("User canceled a subscription.");
break;
case 'DID_CHANGE_RENEWAL_PREF':
console.log("The subscriber downgraded. Effective on next renewal. Handle.");
break;
case 'DID_CHANGE_RENEWAL_STATUS':
console.log("The subscriber downgraded or upgraded. Effective on next renewal. Handle.");
break;
case 'DID_FAIL_TO_RENEW':
console.log("Subscription has a billing issue. Check if in billing retry period.");
break;
case 'DID_RECOVER':
console.log("Renewal of expired subscription that failed to renew.");
break;
case 'INITIAL_BUY':
console.log("Initial purchase. Ignored because we already handled with another function.");
break;
case 'INTERACTIVE_RENEWAL':
console.log("Interactive renewal. Not sure if we'll ever see this.");
break;
case 'RENEWAL':
console.log("Renewal after failure. Same as DID_RECOVER. Handle there.");
break;
default:
console.log("Hit default.");
break;
};
})
.catch((error) => {
console.log("Error validating receipt from App Store server.", error);
});
});
This is the output I'm getting (which is only a portion of the notification Apple says it is sending). I don't get notification_type or any of the other parts of the JSON file the Apple docs say I should be receiving:
{ latest_receipt: 'ewoJInNpZ25hdHVyZSIgPSAiQTNVM0FjaDJpbXRPMG53cEtrQW9 <<shortened for this post>>
I never see the console.log for any chunks.
What can I do to make sure I receive all the "chunks" and put them together into the complete JSON file that Apple is sending me so I can work with it?
I solved it. It was so simple. I just had to use req.body instead of req. Here's the code for anyone who is trying to use Google Cloud Functions to handle Server to Server Notifications for subscriptions from Apple.
exports.iosNotification = functions.https.onRequest((req, res) => {
console.log("We are receiving a request from Apple.");
let receipt = req.body.latest_receipt;
const type = req.body.notification_type;
console.log("Notification type: ", type);
const lri = req.body.latest_receipt_info;
console.log(type, lri, receipt);
// Verify the receipt.
validateAppleReceipt(receipt)
See code above for how to handle the types Apple sends...
I have deployed a cloud function to my Firebase In order to use Firebase as my backend server to handle Stripe payment.
The link of the sample Firebase cloud functions i have used: https://github.com/firebase/functions-samples/tree/master/stripe
Here is the function I should trigger when I charge the user in my app
exports.createStripeCharge = functions.database.ref('/stripe_customers/{userId}/charges/{id}').onWrite((event) => {
const val = event.data.val();
if (val === null || val.id || val.error) return null;
return admin.database().ref(`/stripe_customers/${event.params.userId}/customer_id`).once('value').then((snapshot) => {
return snapshot.val();
}).then((customer) => {
const amount = val.amount;
const idempotency_key = event.params.id;
let charge = {amount, currency, customer};
if (val.source !== null) charge.source = val.source;
return stripe.charges.create(charge, {idempotency_key});
}).then((response) => {
return event.data.adminRef.set(response);
}).catch((error) => {
return event.data.adminRef.child('error').set(userFacingMessage(error));
}).then(() => {
return reportError(error, {user: event.params.userId});
});
});
I know that the above function will be triggered when my database changed.
My question is, what is the proper way to pass the Stripe payment detail to my Firebase Database? I am not sure what should I pass to my firebase database after reading the stripe document.
Could anyone help me with this question? Thanks!!
P.S. My developing environment: Objective C, IOS application.
The easiest way is to have your app make an HTTP POST request to an HTTP Firebase function. Your app should already be doing this in order to create the Stripe ephemeral key and you can handle payments in a similar way. After the charge is successful you can save the resulting info to your database.
The function:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const express = require('express');
const bodyParser = require('body-parser');
const stripeApp = express();
stripeApp.use(bodyParser.json());
stripeApp.post('/order', (req, res) => {
const { source } = req.body; // This is the token provided by your app
return stripe.charges.create({...}) // the actual payment
});
exports.stripeAPI = functions.https.onRequest(stripeApp);
Have a took at Stripe's demo iOS app - its swift but it should still make sense in Obj-C world. https://github.com/stripe/stripe-connect-rocketrides/tree/master/ios/RocketRides.
Particularly https://github.com/stripe/stripe-connect-rocketrides/blob/master/ios/RocketRides/MainAPIClient.swift#L39
So the process that works for me is -
1) iOS provides card details to Stripe using native SDK
2) Stripe provides a token which you send it to your Firebase backend
you could store it in stripeTokens/userId/yourToken
3) Firebase cloud function then triggers a function and uses this token to create Stripe customer (See saving for later and Customer)
you could store it in stripe_customers/userId/stripeCustomerId like your example
4) Remember to remove yourToken because it's only valid once
5) finally you can use this stripeCustomerId to make payments
Important concept here is to create a customer and store in your backend for future payments.
Hope this helps.
In the example below, is there a way to get the uid of the user who wrote to /messages/{pushId}/original?
exports.makeUppercase = functions.database.ref('/messages/{pushId}/original')
.onWrite(event => {
// Grab the current value of what was written to the Realtime Database.
const original = event.data.val();
console.log('Uppercasing', event.params.pushId, original);
const uppercase = original.toUpperCase();
// You must return a Promise when performing asynchronous tasks inside a Functions such as
// writing to the Firebase Realtime Database.
// Setting an "uppercase" sibling in the Realtime Database returns a Promise.
return event.data.ref.parent.child('uppercase').set(uppercase);
});
UPDATED ANSWER (v1.0.0+):
As noted in #Bery's answer above, version 1.0.0 of the Firebase Functions SDK introduced a new context.auth object which contains the authentication state such as uid. See "New properties for user auth information" for more details.
ORIGINAL ANSWER (pre v1.0.0):
Yes, this is technically possible, although it is not currently documented. The uid is stored with the event.auth object. When a Database Cloud Function is triggered from an admin situation (for example, from the Firebase Console data viewer or from an Admin SDK), the value of event.auth is:
{
"admin": true
}
When a Database Cloud Function is triggered from an unauthenticated reference, the value of event.data is:
{
"admin": false
}
And finally, when a Database Cloud Function is triggered from an authed, but not admin, reference, the format of event.auth is:
{
"admin": false,
"variable": {
"provider": "<PROVIDER>",
"provider_id": "<PROVIDER>",
"user_id": "<UID>",
"token": {
// Decoded auth token claims such as sub, aud, iat, exp, etc.
},
"uid": "<UID>"
}
}
Given the information above, your best bet to get the uid of the user who triggered the event is to do the following:
exports.someFunction = functions.database.ref('/some/path')
.onWrite(event => {
var isAdmin = event.auth.admin;
var uid = event.auth.variable ? event.auth.variable.uid : null;
// ...
});
Just note that in the code above, uid would be null even if isAdmin is true. Your exact code depends on your use case.
WARNING: This is currently undocumented behavior, so I'll give my usual caveat of "undocumented features may be changed at any point in the future without notice and even in non-major releases."
Ever since Firebase functions reached version 1.0, this behavior is no longer undocumented but has sligtly changed. Be sure to read the docs.
Context has been added to cloud functions and you can use it like this
exports.dbWrite = functions.database.ref('/path/with/{id}').onWrite((data, context) => {
const authVar = context.auth; // Auth information for the user.
const authType = context.authType; // Permissions level for the user.
const pathId = context.params.id; // The ID in the Path.
const eventId = context.eventId; // A unique event ID.
const timestamp = context.timestamp; // The timestamp at which the event happened.
const eventType = context.eventType; // The type of the event that triggered this function.
const resource = context.resource; // The resource which triggered the event.
// ...
});