How to pass multiple reminders in react native firebase push notification? - firebase

I'm working on a scheduled local push notification in react native using firebase. I've successfully generated a notification on a specific time like this:
firebase.notifications().scheduleNotification(this.buildNotification(), {
fireDate: notificationTime.valueOf(), //a specific date is set using a datetimepicker
repeatInterval: "minute",
exact: true
});
My question is how can I set multiple reminders in scheduleNotification()? Is is possible in react-native-firebase?

Yes is possible in react-native-firebase
In your buildNotification
buildNotification = (notificationId) => {
const notification = new firebase.notifications.Notification()
.setNotificationId(notificationId) --> set here different id
return notification;
};
repeatInterval: 'day',
How frequently the notification should be repeated. One of minute,
hour, day or week.
If not present, the notification will be displayed once.

Related

How I can send some data to firebase from React-Native app for tracking user activity?

I have an APP and I would like to track user activity when they make orders. So, when user makes an order I would like to send to Firebase some object like this:
{
productName: 'product',
id: 1,
price: 100,
quantity: 1
}
And then in Firebase I would like to see table with this data, for example:
image
In my project I use this library for tracking quantity of users #react-native-firebase/analytics
firebase
.analytics()
.setAnalyticsCollectionEnabled(
trackingStatus === TRACKIG_STATUS_ENUM.authorized
);
So how to create firebase table like I described and how to send this data from my app?
you can use
import * as Analytics from 'expo-firebase-analytics'
Analytics.logEvent('new_order', { cart_id : id,product_qty:qty })

Flutter/Firebase How to increment Badge number of App Icon

I am student who is studying computer programming in Canada.
Nowadays , I am making the app as my side project with flutter/firebase.
In my app chat function is included. The problem that I am facing is how to increment badge number and keep it if the message is not read.
I attached the picture of my Message data structure in the firebase.
and below code is the firebase function code. For now I am pushing the notification with this code.
I am getting the notification successfully , but the problem is the how to count badge number of the app icon. As you can see in the notification badge number is '1'. Whenever I push the notification, that number is overriding previous number. I think this is wrong way, what I am expecting, if the value isMessageRead in the firebase database increment badge number. I am just wondering the logical way , or any other source that I can study that can explain how to count the Icon badge Number.
For now I have no idea how to manage badge number.
Thanks for reading,
I am waiting for your reply.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.onCreateMessage = functions.firestore.document('/Chat/{currentUserID}/UserList/{anotherUserID}/Messages/{message}')
.onCreate(async (snap, context) => {
const currentUserID = context.params.currentUserID;
const anotherUserID = context.params.anotherUserID;
console.log(currentUserID);
console.log(anotherUserID);
if (currentUserID == snap.data().recieverID) {
return admin.messaging().sendToTopic(`${currentUserID}`, {
notification: {
title: snap.data().senderID,
body: snap.data().message,
clickAction: 'FLUTTER_NOTIFICATION_CLICK',
sound: 'default',
badge: '1'
}
});
}
});
enter code here
For badge to do that manually you have to store user unread message count somewhere in firebase (recommend in user node.)So when user read or get messages then you need to update that count number
Here is a package that will help you show a badge on app icons.
flutter_app_badger
https://pub.dev/packages/flutter_app_badger
FlutterAppBadger that can you update your count on app icons. It supports Both IOS and Android like this.
FlutterAppBadger.updateBadgeCount(1);
Remove a badge
FlutterAppBadger.removeBadge();

Disable sound for local notification

I'm trying to display a local notification without a sound using react-native-firebase, is that possible?
I have tried playing around with the AndroidNotification class, but couldn't find a way to do this.
const notification = new firebase.notifications.Notification()
.setNotificationId("notificationId")
.setTitle("Title")
.setBody("Text")
.android.setBigText("Big Text")
.android.setColor("#f04e29")
.android.setAutoCancel(true)
.android.setOnlyAlertOnce(true)
.android.setChannelId("channel")
.android.setLargeIcon("ic_launcher")
.android.setSmallIcon("ic_notification");
firebase.notifications().displayNotification(notification);
I would like for the notification to be displayed without any noise at all.
From my own experience, two things have to be taken into account:
No setSound method must be called or no sound properties should be set
When developing for Android API level >=26, the Android Importance passed as third argument of the Channel method has to be set to Low, Min or None. Otherwise the default sound is played anyway. So an example of creating your Channel (e.g. in the componentDidMount of your App.tsx):
const channel = new firebase.notifications.Android.Channel(
"testNotification",
"Test Notification",
firebase.notifications.Android.Importance.Low
)
firebase.notifications().android.createChannel(channel);
And then showing a nonobtrusive notification:
const notification = new firebase.notifications.Notification()
.setTitle("My notification title")
.setBody("My notification body");
notification.android.setChannelId("testNotification");
firebase.notifications().displayNotification(notification);

Ionic: Is it possible to delay incoming push FCM push notification from showing to my device until a specific time

So i am using Firebase for my project. And what is does is that every time a new record is added into a specific collection in Firestore, my Firebase Function will be triggered thus sending the push notification into the devices of users who are subscribe to a specific topic.
Here is the code that i used for Firebase Function:
exports.sendNotifications = functions.firestore
.document('notifications/{sessionId}').onCreate(async event => {
const newValue = event.data();
// Gets data from the new notification record in Firestore.
const topicId = newValue.topicId;
const themeId = newValue.themeId;
const colorId = newValue.colorId;
let lable = newValue.lable;
let header = newValue.header;
// Limit the number of characters for push notifications
if(header.length >= 50){
header = header.substring(0,47) + "...";
} if(lable.length >= 100){
lable = lable.substring(0,97) + "...";
}
// Set the message for the notification.
const payload = {
notification: {
title: header,
body: lable,
}
}
let options = {
priority: 'high'
};
// Sends the notification to the users subscribed to the said topic
return admin.messaging().sendToTopic(topicId, payload, options)
});
Since the Function triggers and sends push notifications to all my users subscribe to a topic at the same time. Is it possible for the device (the receiving end) to not show the notification sent by my Function up until a specific time comes?
Cause users of my Ionic app needs to choose what time of the day they will be receiving push notification from the cloud.
I am aware that in the Firebase console, you can send notifications manually using a form and set the time and date in where the notification will be sent, but this does not work on my favor since my users did not set the same time and date for notifications to be showing.
There is no "deliver the message at this time" API in Firebase Cloud Messaging. See FCM Schedule delivery date or time of push notification
There are two ways to reach your goal:
Only call FCM when you want the message to be delivered. This will require that you create a Cloud Function that runs at an interval, and then when it is called only delivers the messages for that interval.
Deliver the message as a data message, which your app handles. And then hold the message on the client, until it is ready to display to the user. See the Android documentation on creating a notification for more on that.

Daily Firebase HTTP Request to trigger FCM Notification

I have FCM up and running in my app and I can send notifications through the Firebase console to all users or a specific user based on the fcmToken stored in a user's profile.
My app allows users to submit daily updates, answers to a set of questions about their health. Whenever a user does this I update the child path /userId/lastDailyUpdate with a date in form yyyyMMdd (ie. 20170805) as an Integer. I do this so that I can hide the questions and submit views if a user has already submitted their input for that specific day.
What I would like to do now, is to use Firebase Cloud Functions in combination with a daily cron job at 8pm to check if a user has submitted their answers. In short, if current date (yyyyMMdd as Int) is larger than date Int stored in /userId/lastDailyUpdate send a notification.
I can send notifications using Cloud Functions, but cannot find a way to loop through all users in my database and checking their lastDailyUpdate values. My code so far:
let functions = require('firebase-functions');
let admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendDailyReminder = functions.https.onRequest((request, response) => {
response.send("Daily Reminder Function");
let userId = "P3fPW62PaxX45Ccvo4OiyOk6fJC2"
// See the "Defining the message payload" section below for details
// on how to define a message payload.
var payload = {
notification: {
title: 'Had a healthy day?',
body: 'Submit you stats before you go to sleep.',
badge: '1',
sound: 'default'
}
};
return admin.database().ref('/users/P3fPW62PaxX45Ccvo4OiyOk6fJC2/fcmToken').once('value').then(allToken => {
if (allToken.val()) {
const token = Object.keys(allToken.val());
return admin.messaging().sendToDevice(token, payload).then(response => {
});
}
});
});
The above code works, but as you can imagine I want to send to more than 1 user :)
The path to lastDailyUpdate value (for user 'P3fPW62PaxX45Ccvo4OiyOk6fJC2' in this example) is:
'/users/P3fPW62PaxX45Ccvo4OiyOk6fJC2/lastDailyUpdate'
I have only been programming for a short while, I know a good amount of JS, but these functions have got me puzzled. Is there anyone who can help me in the right direction?
I am thinking of a HTTP request to trigger a for loop that checks the lastDailyInput value and if smaller than current date trigger then sending of a notification.
Any help with code or tips (possibly on a crash course for Express/Node.js) would be very much appreciated.

Resources