首页 > 解决方案 > 我们需要每天早上 7 点运行重复通知,并且内容是动态的

问题描述

我们需要每天早上 7 点运行重复通知,并且内容不是静态的,而是
需要从 API 获取的动态内容
是否有任何方法可以在 ios 和 android 的颤振中做到这一点而无需
从服务器端做任何事情?
我们使用了https://pub.dev/packages/flutter_local_notifications并没有解决问题?
我们正在为现有网站开发应用程序,客户拥有所有 API,但他们没有使用 FCM。他们不会在服务器端进行任何更改,并且强烈建议仅从应用程序端进行处理。
如果我们使用 Firebase 消息传递库,是否可以在 ios 和 android 应用程序中在后台进行通知和 API 调用?

标签: androidiosflutterdartflutter-dependencies

解决方案


将 firebase 函数与 pubsub 一起使用。在 firebase 函数中使用此代码。

const admin = require('firebase-admin');
const fcm = admin.messaging();

    exports.dailyNotification = functions.pubsub.schedule('0 7 * * *')
    
    .onRun((context) => {
        //tokens need to be unique to send notifications per device
        //you can call data from firebase firestore to change the notification contents
    
        const payload = admin.messaging.MessagingPayload = {
                            
            notification: {
                title: 'Notification title',
                body: 'This is a notification message',
                icon: 'your-icon-url',
                click_action: 'FLUTTER_NOTIFICATION_CLICK'
            }
        };
       /*this token you need to save from flutter. You need it only when you 
         need to send specific notification to the specific device*/

        fcm.sendToDevice(tokens.token, payload);
    
    });

在Flutter Firebase 消息传递库中使用这个包

从这里了解如何使用 firebase 消息通知。 Flutter firebase 通知教程

如果您有任何问题,请告诉我。


推荐阅读