0

I want to put 1 hour gap in push notifications, Like when the user receives one notification then I want to store time in Shared Preferences and when the next notification comes then I want to check if the gap in both notifications is equal to or greater than 1 hour then I will show the notification to the user otherwise notification will be not shown to the user. So the problem is how can I calculate 1 hour difference in time. Please help me. Thanks in Advance.

a_local_nobody
  • 7,947
  • 5
  • 29
  • 51

2 Answers2

1

One of my project had this same sort of requirement.
You can absolutely do this using WorkManager.

For this explanation,
I'll assume that you are using Firebase Cloud Messaging (FCM)

Flow:

  • The main thing would be to use FCM's data payload pattern.
    Note that this wouln't work as expected if you use FCM from Firebase Dashboard.
    Better to use the POST endpoint (https://fcm.googleapis.com/fcm/send) with valid auth headers.
  • Override the on onMessageReceived() & verify that the RemoteMessage has a valid data & is not null. Get the data from the RemoteMessage object like this:
val data = remoteMessage.data
val title = "msg_title"
val message = "msg_body"

if (data.containsKey(title) && data[title]?.isNotEmpty() == true &&
    data.containsKey(message) && data[message]?.isNotEmpty() == true) {
    // Fire the WorkManager here with a One Hour Initial Delay.
}
  • Create a OneTimeWorkRequest instance (check WorkManager docs) with an initial delay of 1 hour delay & assign it to WorkManager.
    For example:
Workmanager.getInstance()
           .enqueueUniqueWork("unique_work", ExistingWorkPolicy.REPLACE,
            OneTimeWorkRequest.Builder(NewStickerWorker::class.java)
                             .setInitialDelay(1, TimeUnit.HOURS)
                             // Send notification details to OneTimeWorkRequest
                                 .setInputData(Data.Builder()
                                 .putString("title", data[title])
                                 .putString("message", data[message])
                                 .build())
                            .build()
           )
  • In your custom Worker, get the notification title & message via inputData.getString() & simply show the Notification.

  • To send a payload message,
    Create a data payload like below in Postman or in whichever app you want:

{
  "to":"/topics/YourTopic",
  "data":{
      "msg_title":"Notifications Title",
      "msg_body":"Some awesome notification message body",
      "priority" : "high",
      "sound" : "default"
  }
}
  • Make sure to subscribe to a Topic in your App via FirebaseMessaging API, & replace YourTopic in the json payload.

NOTE: The reason for not using the Dashboard to send messages here, is because if the App is in background then the Android System Notification Tray shows the Notification right away, which is not what we want.

So when you use data payload,
it is guaranteed that your app's onMessageReceived() is called even when the app is in background or foreground.

Darshan
  • 4,020
  • 2
  • 18
  • 49
0

Pushing notification is hit and go. so if you wanted to make a schedule you have to maintain via server-side integration, there you can schedule the notification after 1 hour or according to your needs.

or you can those specific events via queues and then process them when it is required.

Kishor Pant
  • 136
  • 1
  • 4
  • I am sending notifications from my server-side but let suppose one of the users have turned off the wifi or data network and turn it on after some so all the notifications will start coming at once and I think this is annoying for the user so that's why i want put 1 hour gap. – Hilal Ahmad Mar 01 '21 at 07:53
  • Got it, in this you have to process two way binding for notification delivery so that you can have the gapping between the notification – Kishor Pant Mar 01 '21 at 07:56