FlutterFlow / flutterflow-issues

A community issue tracker for FlutterFlow.
117 stars 19 forks source link

Unable to cancel scheduled push notification #1324

Closed Georgios199513 closed 1 year ago

Georgios199513 commented 1 year ago

Has your issue been reported?

Current Behavior

Currently in FlutterFlow we can schedule a notification, and this is great! Unfortunately if I need to delete a scheduled notification this is not possible. Example: I have an appointment app where I send scheduled notification one day before in order to remind the user. In the user cancels the appointment the notification is not canceled and this is a problem.

Expected Behavior

What I was expected is to be able to get the notifications id or to have an action that cancels a scheduled notification.

Steps to Reproduce

-

Reproducible from Blank

Bug Report Code (Required)

-

Context

Due to the problem I don’t use the most important feature for my app. I currently use an external service and I send only emails.

Visual documentation

-

Additional Info

No response

Environment

- FlutterFlow version: v3.1
- Platform:
- Browser name and version:
- Operating system and version affected:
hariprasadms commented 1 year ago

Hi @Georgios199513 - Thanks for submitting the issue. I can see what you are trying to say. This is a feature request or enhancements. I would suggest, request this feature using feature request form available under help section.

image
arielramos commented 1 year ago

Hi @Georgios199513 I found a way to create scheduled push notifcations daily, but with the option to delete, a friendo toldme the steps and create a Firebase Cloud Function, you can check it out and see what you can change. First you have to create a Collection with at least 3 fields: Title, Time, UserReference

You can verify the following code created with nodeJS and adapt it to your requirements:

const functions = require('firebase-functions'); const admin = require('firebase-admin'); const serviceAccount = require('./pepperhealth-2dfgr4dfsd34-7b11552d235a.json');

admin.initializeApp({ credential: admin.credential.cert(serviceAccount) });

exports.sendnoti = functions.pubsub.schedule('every 1 minutes').timeZone('America/Costa_Rica').onRun(async (context) => { const currentTime = new Date(); const currentHour = currentTime.getUTCHours(); const currentMinute = currentTime.getUTCMinutes();

console.log(`Function triggered at: ${currentHour}:${currentMinute}`);

const recordatorioRef = admin.firestore().collection('recordatoriosComidas');
const reminders = await recordatorioRef.get();

for (const reminder of reminders.docs) {
    const reminderData = reminder.data();
    const reminderTime = reminderData.hora.toDate();

    console.log(`Checking reminder with ID: ${reminder.id} and time: ${reminderTime.getUTCHours()}:${reminderTime.getUTCMinutes()}`);

    if (reminderTime.getUTCHours() === currentHour && reminderTime.getUTCMinutes() === currentMinute) {
        console.log(`Reminder ${reminder.id} matches the current time.`);

        const userRef = reminderData.user;
        const userDoc = await userRef.get();
        const fcmTokensRef = userDoc.ref.collection('fcm_tokens');
        const fcmTokensSnapshot = await fcmTokensRef.get();

        const fcmTokens = [];
        fcmTokensSnapshot.forEach(tokenDoc => {
            const token = tokenDoc.data().fcm_token;
            if (token) {
                fcmTokens.push(token);
            }
        });

        console.log(`Retrieved ${fcmTokens.length} FCM tokens for user.`);
        if (!fcmTokens.length) {
            console.log("No FCM tokens found for user.");
            continue;
        }

        const notificationPayload = {
            title: 'Reminder',
            body: reminderData.tiempo,
        };

        for (const token of fcmTokens) {
            const payload = {
                token: token,
                notification: notificationPayload
            };

            try {
                await admin.messaging().send(payload);
                console.log('Notification sent successfully to:', token);
            } catch (error) {
                console.error('Error sending notification to token:', token, 'Error:', error);
            }
        }
    } else {
        console.log(`Reminder ${reminder.id} does not match the current time.`);
    }
}

return null;

});