Android Firebase 云功能通知 [英] Android Firebase cloud function notification

查看:22
本文介绍了Android Firebase 云功能通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已设法设置 Firebase 云功能以向主题发送通知.问题是它发送给包括发件人在内的所有用户,我如何设置我的云功能,使其不向发件人显示通知?请帮忙?以下是如何发送到主题

I have managed to setup firebase cloud functions to send notification to topics. the problem is that it sends to all users including the sender, how can i setup my cloud function so that it doesn't show a notification to sender? please help? below is how am sending to topic

exports.sendNotesNotification = functions.database.ref('/Notes/{pushId}')
    .onWrite(event => {
        const notes = event.data.val();

        const payload = {
                notification: {

                    username: notes.username,
                    title: notes.title,
                    body: notes.desc

                }

            }

            admin.messaging().sendToTopic("New_entry", payload)
            .then(function(response){
                console.log("Successfully sent notes: ", response);
            })
            .catch(function(error){
                console.log("Error sending notes: ", error);
            });
        }); 

推荐答案

根据 firebase 的文档,对于公开且时间不紧迫的通知,应使用主题发送通知.在您的情况下,通知是不公开的,并且由于发件人也订阅了该特定主题,他也会收到通知.因此,如果您想避免向发件人发送通知,您必须从您的主题中取消订阅该发件人.

From the Docs of firebase, Using topics for sending notifications should be done for notifications that are public and are not time critical. In your case notification is not public and as sender is also subscribed to that particular topic he will also get the notification. therefore if you want to avoid sending notification to sender you have to unsubscribe that sender from your topic.

或者更好的解决方案是您应该使用 FCM 令牌将通知发送到单个设备.用于发送 FCM 令牌通知的 node.js 代码是

Or better solution is that you should send the notifications to single devices using there FCM tokens. the node.js code for sending notification for FCM tokens is

admin.messaging().sendToDevice(<array of tokens>, payload);

你可以从你安卓的 FirebaseInstanceIdService 的 onTokenRefresh() 方法获取设备令牌.

and you can get the device tokens from onTokenRefresh() method of your android's FirebaseInstanceIdService.

 @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        // TO DO: send token to your server or firebase database
}

更新:

将 firebase 令牌存储到您的数据库现在您应该像这样构建您的数据库

To store the firebase tokens to your database Now you should structure your database like this

   -users
      |-user1uid
      |   |-name //your choice
      |   |-email //your choice
      |   |-fcmTokens
      |        |-valueOftoken1:true
      |        |-valueOftoken2:true
   -notes
      |  |-notesId
      |      |-yourdata
      |      |-createdBy:uidofUser  //user who created note
      |
   -subscriptions       //when onWrite() will trigger we will use this to get UID of all subscribers of creator of "note". 
      |      |-uidofUser    
      |           |-uidofSubscriber1:true //user subscribe to notes written. by parent node uid
      |           |-uidofSubscriber2:true

这里将令牌保存到数据库中是 onTokenRefresh()

to save the tokens in database here is the code for onTokenRefresh()

 @Override
        public void onTokenRefresh() {
            // Get updated InstanceID token.
            String refreshedToken = FirebaseInstanceId.getInstance().getToken(); //get refreshed token
            FirebaseAuth mAuth = FirebaseAuth.getInstance();
            FirebaseUser user = mAuth.getCurrentUser(); //get currentto get uid
            if(user!=null){
            DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference().child("users").child(user.getUid()); // create a reference to userUid in database
            if(refreshedToken!=null) //
              mDatabase.child("fcmTokens").child(refreshedToken).setValue(true); //creates a new node of user's token and set its value to true.
            else
              Log.i(TAG, "onTokenRefresh: token was null");
    }
    Log.d(tag, "Refreshed token SEND TO FIREBASE: " + refreshedToken);
    }

当为该用户创建新令牌时,上述代码将在用户的 fcmTokens 中创建新节点.

when ever new token is created for this user the above code will create new node in fcmTokens of the user.

这里是检索用户令牌并向这些令牌发送通知的 node.js 部分.为此

Here comes the node.js part of retrieving users token and sending notification to those tokens. for this

exports.sendNotesNotification = functions.database.ref('/Notes/{pushId}')
    .onWrite(event => {

        const notes = event.data.val();
        const createdby = notes.createdBy;
        const getAllSubscribersPromise = admin.database().ref(`/subscriptions/${createdby}/`).once('value'); // retrieving subscribers 

         const payload = {
                notification: {

                    username: notes.username,
                    title: notes.title,
                    body: notes.desc

                }

            }

        return getAllSubscribersPromise.then(result => {
        const userUidSnapShot = result; //results will have children having keys of subscribers uid.
        if (!userUidSnapShot.hasChildren()) {
          return console.log('There are no subscribed users to write notifications.'); 
        }
        console.log('There are', userUidSnapShot.numChildren(), 'users to send notifications to.');
        const users = Object.keys(userUidSnapShot.val()); //fetched the keys creating array of subscribed users

        var AllFollowersFCMPromises = []; //create new array of promises of TokenList for every subscribed users
        for (var i = 0;i<userUidSnapShot.numChildren(); i++) {
            const user=users[i];
            console.log('getting promise of user uid=',user);
            AllFollowersFCMPromises[i]= admin.database().ref(`/users/${user}/fcmToken/`).once('value');
        }

        return Promise.all(AllFollowersFCMPromises).then(results => {

            var tokens = []; // here is created array of tokens now ill add all the fcm tokens of all the user and then send notification to all these.
            for(var i in results){
                var usersTokenSnapShot=results[i];
                console.log('For user = ',i);
                if(usersTokenSnapShot.exists()){
                    if (usersTokenSnapShot.hasChildren()) { 
                        const t=  Object.keys(usersTokenSnapShot.val()); //array of all tokens of user [n]
                        tokens = tokens.concat(t); //adding all tokens of user to token array
                        console.log('token[s] of user = ',t);
                    }
                    else{

                    }
                }
            }
            console.log('final tokens = ',tokens," notification= ",payload);
            return admin.messaging().sendToDevice(tokens, payload).then(response => {
      // For each message check if there was an error.
                const tokensToRemove = [];
                response.results.forEach((result, index) => {
                    const error = result.error;
                    if (error) {
                        console.error('Failure sending notification to uid=', tokens[index], error);
                        // Cleanup the tokens who are not registered anymore.
                        if (error.code === 'messaging/invalid-registration-token' || error.code === 'messaging/registration-token-not-registered') {
                            tokensToRemove.push(usersTokenSnapShot.ref.child(tokens[index]).remove());
                        }
                    }
                    else{
                        console.log("notification sent",result);
                    }
                });

                return Promise.all(tokensToRemove);
            });

            return console.log('final tokens = ',tokens," notification= ",payload);
        });





            });
        }); 

我还没有检查 node.js 部分,如果您还有问题,请告诉我.

i have not checked the node.js part let me know if you still have problem.

这篇关于Android Firebase 云功能通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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