0

I have an app built on Flutter (firebase) and I am using the cloud Function to send notifications. I want to show/hide notifications based on condition. The condition is, if a user is in the range of 50 km then the notification should show otherwise it should hide. This condition should work if the app is in the foreground/background/ or killed.

To calculate the range I am having lat/long for a user which I am getting from FireStore and the second lat/long I am getting from notification payload.

I have read the official document to handle notifications but my case is different than the example given here https://firebase.flutter.dev/docs/messaging/notifications/

Here is the skeleton of the cloud function

enter code here

   
     var flutterMessage = {
                data:
                 {
                    "id": docID,
                    "companyId": companyId,
                    "userId": userId,
                         -
             -
             -
             -
             some more data
             -
             -
             -
             -
             
                    "latitude": JSON.stringify(latitude),
                    "longitude": JSON.stringify(longitude),
                     },
        notification: {
            title: title,
            body: companyName,

        },
        apns: {
                    payload: {
                        aps: {
                            sound: "default",
                        },
                    },
                },
        topic: 'New-Doc',
};


    let flutterResponse = await admin.messaging().send(flutterMessage).catch((error) => {console.log("Error " + error);});
    console.log("response Flutter App" + flutterResponse);

});
  • If you know the two lat/lon pairs, you can calculate the distance between them with the Haversine formula as shown here: https://stackoverflow.com/questions/27928/calculate-distance-between-two-latitude-longitude-points-haversine-formula. Then you can decide whether to send/show the notification based on the result. – Frank van Puffelen Jul 03 '23 at 04:17
  • Thanks for the reply but one pair of lat/long is coming from notification itself, I dont get in advance. show/hide notifications will be decided after receiving notification. – Amit chaudhary Jul 03 '23 at 04:46
  • OK. In that case you will need to send a **data only** message (without a `notification` in there), and decide whether to display the notification on the `onMessage`/`onBackgroundMessage` of your application code. – Frank van Puffelen Jul 03 '23 at 04:52
  • Thanks Frank, now I am able to send only data messages from the cloud function and get the same in FirebaseMessaging.onMessage.listen((RemoteMessage message) {..}, (foreground notification) where I am taking a decision to show/hide notification from that data message. For the foreground, everything is working fine. but for background data messages, I don't know which method would receive a data message so that I can send a notification from that data message. – Amit chaudhary Jul 06 '23 at 22:06
  • 1
    Figured out the background notification by changing apns structure to apns: { payload: { aps: { contentAvailable: true, sound: "default", }, }, }, contentAvailable: true, is required otherwise message gets deprioritized – Amit chaudhary Jul 07 '23 at 01:36

0 Answers0