如何在扑朔迷离中为每个设备/用户禁用云消息传递? [英] How to disable cloud messaging per device/user in flutter?

查看:49
本文介绍了如何在扑朔迷离中为每个设备/用户禁用云消息传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Flutter应用程序,我正在使用Firebase Cloud Messaging和Cloud功能通过其FCM注册令牌向用户发送推送通知.该应用程序具有一个设置页面,用户应可以在其中关闭某些推送通知.通知是针对特定用户的,因此无法订阅或取消订阅的主题,但是可以将通知分类为某些类别.

For a flutter app I’m using Firebase Cloud Messaging and cloud functions to send push notifications to users, using their FCM registration tokens. The app has a settings page where users should be able to turn off certain push notifications. The notifications are user specific, so a topic to subscribe or unsubscribe to wouldn’t work, but the notifications can be classified in certain categories.

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

For example in a chat app when user A send a message to user B that push notification could be in a category of ‘chat messages’, while user A could also delete the chat with user B and that push notification could be in a category of ‘deleted chats’.

我如何做到这一点,以便用户B可以关闭已删除的聊天"的通知,同时仍然接收聊天消息"的通知?是否可以以一种方式或另一种方式使用带有主题和用户注册令牌的条件?任何想法都将不胜感激!

How can I make it so that user B can turn off notifications for ‘deleted chats’, while still receiving notifications for ‘chat messages’? Is it possible to use a condition with a topic and a user’s registration token on one way or the other? Any ideas are greatly appreciated!

推荐答案

由于道格(Doug)朝着正确的方向前进了一大步,我才得以弄清楚!在下面发布我的代码,以帮助任何人朝正确的方向迈出相同的一步.

Thanks to a big nudge in the right direction from Doug, I was able to figure it out! Posting my code below to help anyone take the same step in the right direction.

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

So, in my flutter app' settings page the user can turn notifications on and off for a few categories. The user's preference is then stored in a user specific document in my Cloud Firestore users collection. See the below code for an example of the SwitchListTile I used on the settings page.

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 的值是否等于.如果是这样,则发送通知,否则不发送通知给用户.在下面,我添加了云功能.请注意,cloud函数会呈现'nested promises'警告,但它现在可以使用!我仍然是Flutter的初学者,所以我很高兴能使其正常工作.再次非常感谢Doug!

In my cloud function I added a reference to the document in the users collection and a check to see if the value of the field adminNotifications is equal to true. If so, a notification is send, otherwise a notification is not send to the user. Below I've added the cloud function. Please do note that the cloud function renders 'nested promises' warnings, but it works for now! I'm still a Flutter beginner so I was pretty happy to get it working. Big thanks again to 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)
    })
});

这篇关于如何在扑朔迷离中为每个设备/用户禁用云消息传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆