Flutter: How can I create repeating push notifications?

Viewed 443

how can I create repeating push notifications? I want to be able to schedule those at least a little bit flexible (e. g. every year, every 3 months, every month, ... nothing too fancy).

I already tried the package flutter_local_notifications but for me it looks like I just can create repeating notifications for every week or every day.

I am already using firebase so I would be open to any solutions which are based on that.

My goal is the following: The users of my app should be able to create a (repeating) reminder in form of a push notification.

1 Answers

A Firebase-based solution: Assuming you are on the Blaze plan, you could create a scheduled cloud function using PubSub, like here. Such methods are billed based on the number of total functions, not invocations, so you have some flexibility, though if you need this to be a per-user feature, it might not be a viable solution. If you can provide a minimal granularity (for example, 5 minutes), you could deploy a single scheduled function to notify all pending "tasks" to users, while the users can schedule a reminder by saving an entity in Cloud Firestore, which describes the exact time and date when they would receive a reminder. The scheduled function then maps all currently pending tasks, and deleted handled tasks.

For example, in TypeScript you might do something like:

import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'

const serviceAccount = require("path/to/serviceAccountKey.json")

// Initialize the app with a service account, granting admin privileges
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://databaseName.firebaseio.com"
})

// Declare the exported PubSub function
export const checkPendingNotifications = = functions.pubsub.schedule('every 5 minutes').onRun(async (context) => {
  console.log('This will be run every 5 minutes!')
  const db = admin.firestore()

  const scheduledTasks = await db.collection('s').get();
  scheduledTasks.forEach(taskSnapshot => {
    const data = taskSnapshot.data()
    // ... TODO Check for user-submitted dates in data
  })

  return null;
});
Related