【问题标题】:How to disable cloud messaging per device/user in flutter?如何在颤动中禁用每个设备/用户的云消息传递?
【发布时间】:2020-04-28 14:38:18
【问题描述】:

对于 Flutter 应用,我使用 Firebase 云消息传递和云函数向用户发送推送通知,使用他们的 FCM 注册令牌。该应用程序有一个设置页面,用户应该能够关闭某些推送通知。通知是特定于用户的,因此订阅或取消订阅的主题不起作用,但通知可以分类为某些类别。

例如,在聊天应用程序中,当用户 A 向用户 B 发送消息时,推送通知可能属于​​“聊天消息”类别,而用户 A 也可以删除与用户 B 的聊天,并且推送通知可能属于一类“已删除的聊天”。

我怎样才能使用户 B 可以关闭“已删除聊天”的通知,同时仍接收“聊天消息”的通知?是否可以以一种或另一种方式将条件与主题和用户的注册令牌一起使用?任何想法都非常感谢!

【问题讨论】:

  • 这似乎很容易实现,结合自定义代码和每个用户的配置来确定是否应将消息发送到用户的设备令牌。到目前为止,您尝试过哪些没有按预期工作的方法?
  • 到目前为止,只要在应用程序中触发通知事件,就会在 Firestore 的“通知”集合中创建一个文档。负责向注册令牌发送推送通知的云函数有一个 onCreate 方法,每次将新文档添加到此集合时都会触发该方法。我将编辑我的问题以包含该功能!就每个用户的配置而言,我一无所知 - 共享偏好是我所知道的。
  • 只允许用户使用可以在函数中查询的其他文档的内容来配置他们想要的行为吗?
  • 这句话很有道理!就像在通知文档中设置一个布尔值,然后在云功能中检查是否应该发送推送通知。我之前显然没有想到这一点..
  • 我能够让它工作,非常感谢您的帮助!

标签: flutter google-cloud-functions firebase-cloud-messaging


【解决方案1】:

多亏了 Doug 在正确方向上的大力推动,我才得以弄清楚!在下面发布我的代码,以帮助任何人朝着正确的方向迈出相同的一步。

因此,在我的 Flutter 应用程序的设置页面中,用户可以打开和关闭几个类别的通知。然后,用户的首选项存储在我的 Cloud Firestore users 集合中的用户特定文档中。我在设置页面上使用的SwitchListTile 示例请参见下面的代码。

SwitchListTile(
   title: Text('Admin notifications'),
   subtitle: Text('Maintenance and general notes'),
   onChanged: (value) {
     setState(() {
       adminNotifications = value;
       Firestore.instance
       .collection('users')
       .document(loggedInUser.uid)
       .updateData({
       'adminNotifications': value,
       });
    });
    save('adminNotifications', value);
  },
  value: adminNotifications,
),

在我的云函数中,我在users 集合中添加了对文档的引用,并检查adminNotifications 字段的值是否等于true。如果是,则发送通知,否则不向用户发送通知。下面我添加了云功能。请注意,云函数会呈现 'nested promises' 警告,但它现在有效!我仍然是 Flutter 的初学者,所以我很高兴能够让它工作。再次感谢 Doug!

exports.userNotifications = functions.firestore.document('notifications/{any}').onCreate((change, context) => {
    const userFcm = change.data().fcmToken;
    const title = change.data().title;
    const body = change.data().body;
    const forUser = change.data().for;
    const notificationContent = {
    notification: {
        title: title,
        body: body,
        badge: '1',
        click_action: 'FLUTTER_NOTIFICATION_CLICK',
        }
    };
    var usersRef = db.collection('users');
    var queryRef = usersRef.where('login', '==', forUser).limit(1).get()
    .then(snapshot => {
        snapshot.forEach(doc => {
            const adminNotifications = doc.data().adminNotifications;
            console.log(adminNotifications);

            if(swapNotifications === true){
                return admin.messaging().sendToDevice(userFcm, notificationContent)
                    .then(() => {
                    console.log('notification sent')
                    return
                    })
                    .catch(error =>{
                        console.log('error in sending notification', error)
                    })
                } else {
                    console.log('message not send due to swapNotification preferences')
                }
    return console.log('reading user data success');
    })
    .catch(err => {
        console.log('error in retrieving user data:', err)
    })
});

【讨论】:

    猜你喜欢
    • 2016-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-30
    相关资源
    最近更新 更多