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

与世无争的帅哥 提交于 2021-02-11 14:55:32

问题


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.


回答1:


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:

  1. 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.
  2. 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.


来源:https://stackoverflow.com/questions/50601957/ionic-is-it-possible-to-delay-incoming-push-fcm-push-notification-from-showing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!