Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions developer-motivator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Send Firebase Cloud Messaging each time a new user open your app the first time or remove your app from his device.

This sample demonstrates how to send a Firebase Cloud Messaging (FCM) notification from a Analytics triggered Function.


## Functions Code

See file [functions/index.js](functions/index.js) for the code.

Sending the notification is done using the [Firebase Admin SDK](https://www.npmjs.com/package/firebase-admin).

The dependencies are listed in [functions/package.json](functions/package.json).


## Trigger rules

The functions triggers every time a new user open your app the first time or remove your app from his device.
6 changes: 6 additions & 0 deletions developer-motivator/database.rules.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
8 changes: 8 additions & 0 deletions developer-motivator/firebase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"database": {
"rules": "database.rules.json"
},
"hosting": {
"public": "public"
}
}
52 changes: 52 additions & 0 deletions developer-motivator/functions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';
const admin = require('firebase-admin');
const functions = require('firebase-functions');
admin.initializeApp(functions.config().firebase);

exports.appinstalled = functions.analytics.event('first_open').onLog(event => {
const payload = {
notification: {
title: 'new ' + event.data.user.deviceInfo.mobileModelName + ' from ' + event.data.user.geoInfo.city + ', ' + event.data.user.geoInfo.country,
body: 'new ' + event.data.user.deviceInfo.mobileModelName + ' from ' + event.data.user.geoInfo.city + ', ' + event.data.user.geoInfo.country
}
};

admin.messaging().sendToDevice("put_your_developer_device_token_here", payload).then(response => {
const tokensToRemove = [];
response.results.forEach((result, index) => {
const error = result.error;
if (error) {
console.error('Failure sending notification to', tokens[index], error);
if (error.code === 'messaging/invalid-registration-token' ||
error.code === 'messaging/registration-token-not-registered') {
tokensToRemove.push(tokensSnapshot.ref.child(tokens[index]).remove());
}
}
});
});
});

exports.appremoved = functions.analytics.event('app_remove').onLog(event => {
const payload = {
notification: {
title: 'lost ' + event.data.user.deviceInfo.mobileModelName + ' from ' + event.data.user.geoInfo.city + ', ' + event.data.user.geoInfo.country,
body: 'lost ' + event.data.user.deviceInfo.mobileModelName + ' from ' + event.data.user.geoInfo.city + ', ' + event.data.user.geoInfo.country
}
};

admin.messaging().sendToDevice("put_your_developer_device_token_here", payload).then(response => {
const tokensToRemove = [];
response.results.forEach((result, index) => {
const error = result.error;
if (error) {
console.error('Failure sending notification to', tokens[index], error);
if (error.code === 'messaging/invalid-registration-token' ||
error.code === 'messaging/registration-token-not-registered') {
tokensToRemove.push(tokensSnapshot.ref.child(tokens[index]).remove());
}
}
});
});

});

8 changes: 8 additions & 0 deletions developer-motivator/functions/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "developer-motivator-functions",
"description": "A simple developer motivator using Cloud Function and firebase analytics",
"dependencies": {
"firebase-admin": "^4.1.2",
"firebase-functions": "^0.5.1"
}
}