Android App Development
iOS App Development
Flutter App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 3.0.3.7 release ready!
Deniel Kerr
Founder. Opencart
Top Partners
Firebase has launched a new tool to make Application development more easier. The Firebase Cloud Functions is currently in its Beta version and is introduced to help developers creating an application without their own servers. They can use Firebase Servers and write their code over Firebase Servers directly using Firebase Cloud Functions.
Yes I know you will think why I should use it if I have my own server. But wait, because there is more to it. Even if you have your own server, its time and resource taking to first hit your server and then your server will hit Firebase server for some result. You might think what’s the idle scenario for that to happen so let me give you one or two scenarios.
Here we are implementing the first scenario of sending push notification. But before that lets see what our database looks like.As you can see I have two different sections namely data and message. In data section all the customer related properties are added. While in message section there are messages stored.
Step 1: You need Firebase CLI and Node.js for cloud functions to work and then you have to initialize your project. If you haven’t done it yet, please follow the firebase guide.
Step2: Now when everything is setup, open <your_firebase_directory>/functions/index.js file. Here we will write our code for sending push notification when a new message is added in message section of database.
And here you go. You are ready with a cloud function that will execute whenever a new message is added to your Firebase database. And you can do many more with the new Firebase cloud functions. For more such examples go to Firebase Cloud Functions documentation.
Your email address will not be published. Required fields are marked*
Name*
Email*
Save my name email and website in this browser for the next time I comment.
const functions = require(‘firebase-functions’);
const admin = require(‘firebase-admin’); admin.initializeApp(functions.config().firebase);
exports.pushNotification = functions.database.ref(‘/messages/{pushId}’).onWrite(( change,context) => {
console.log(‘Push notification event triggered’);
// Grab the current value of what was written to the Realtime Database. // var valueObject = event.data.val(); const message = change.after.val(); const payload = { notification: { title: ‘App Name’, body: “New message”, sound: “default” }, data: { title: message.title, message: message.message } };
const options = { priority: “high”, timeToLive: 60 * 60 * 24 //24 hours };
return admin.messaging().sendToTopic(“notifications”, payload, options); })
Firebase has updated the API to a send function. They recommend this structure now.
// The topic name can be optionally prefixed with “/topics/”. var topic = ‘highScores’;
// See documentation on defining a message payload. var message = { data: { score: ‘850’, time: ‘2:45’ }, topic: topic };
// Send a message to devices subscribed to the provided topic. admin.messaging().send(message) .then((response) => { // Response is a message ID string. console.log(‘Successfully sent message:’, response); }) .catch((error) => { console.log(‘Error sending message:’, error); });
No, the timeToLive property does not mean that the notification will be sent after 24 hours.
It actually means the default time for which the notification will be kept in FCM storage while the device is offline.
So, for this particular case if the receiving device is offline and not able to receive the notification then the FCM will try to keep this notification in their storage for the next 24 hours and if in the next 24 hours the device is in the state to receive the notification then the notification will be delivered to the device, else the notification will fail to deliver.
For more details on these params of the notification request, you can check the official explaination from the FCM panel over here –> https://firebase.google.com/docs/cloud-messaging/http-server-ref#downstream-http-messages-json
For more details on these params of the notification request, you can check the official explanation from the FCM panel over here –> https://firebase.google.com/docs/cloud-messaging/http-server-ref#downstream-http-messages-json
We use cookies to personalize your experience. By continuing to visit this website you agree to our use of cookies. Learn more about privacy policy
Excellent work, fast, good quality and understood the brief perfectly! Quick responses developing the project and very good cooperation. I suggest to anyone.
Stathis Plakidas
USA
India
Global
Name
Email
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.