Firebase Cloud Messaging-在发送通知之前获取数据?

时间:2019-02-22 08:08:53

标签: javascript firebase firebase-cloud-messaging

我有一个通知系统。我不会将数据直接保存在用户节点下。

这是我的Firebase数据树:

+notifall:
    +-L-oplpmjınnlın:
        +type:"followedyou"
        +from:"{followerUid}"

+users:
    +{userUid}:
        +notif:
            +-L-oplpmjınnlın:"true"

当我想在recyclerview中显示通知时,我会这样做:

FirebaseDatabase.getInstance().getReference().child("notifall").child(list.get(position)).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if(dataSnapshot.exists()){
                    holder.uid=dataSnapshot.child("from").getValue().toString();
                    FirebaseDatabase.getInstance().getReference().child("users").child(holder.uid).child("block").child(FirebaseAuth.getInstance().getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot1) {

                                FirebaseDatabase.getInstance().getReference().child("users").child(dataSnapshot.child("from").getValue().toString()).child("about").addListenerForSingleValueEvent(new ValueEventListener() {
                                    @Override
                                    public void onDataChange(@NonNull DataSnapshot dataSnapshot1) {
                                        Glide.with(context).load(dataSnapshot1.child("profilephotourl").getValue().toString()).into(holder.profilephoto);
                                        holder.name.setText(dataSnapshot1.child("namesurname").getValue().toString());
                                        if(dataSnapshot.child("type").getValue().toString().equals("followedyou")){
                                            holder.desc.setText(activity.getString(R.string.followedyou));
                                            Drawable drawable = activity.getResources().getDrawable(R.drawable.ic_newfollow);
                                            holder.notiftype.setImageDrawable(drawable);
                                        }

                                    }

                                    @Override
                                    public void onCancelled(@NonNull DatabaseError databaseError) {

                                    }
                                });

                        }

                        @Override
                        public void onCancelled(@NonNull DatabaseError databaseError) {

                        }
                    });


                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

现在,我想使用Firebase Cloud Messaging实施通知。我在github中查看了示例代码。

这是我的代码,但是当我尝试部署功能时,它给了我意外的令牌错误:

exports.sendFollowerNotification = functions.database.ref('/users/{followedUid}/notifs/{notifId}')
.onWrite(async (change, context) => {
  const notifId = context.params.notifId;
  const followedUid = context.params.followedUid;


  var db = admin.database();
  db.ref("notifall").child(notifId).once('value',function(snapshot){
    if(snapshot.exists()){
      const type = snapshot.child("type").val();
      if(type==="followedyou"){
        const followeruid = snapshot.child("from").val();
        db.ref("users").child(followeruid).child("about").once('value',function(snapshot1){
          if(snapshot1.exists()){
            const name=snapshot1.child("namesurname").val();
            const ppurl=snapshot1.child("profilephotourl")
            db.ref("users").child(followedUid).child("token").once('value',function(snapshot3){
              if(snapshot3.exists()){
                const token=snapshot3.val();
                var message = {
                  data: {
                    title: 'followedyou',
                    from: followeruid,
                    name: name,
                    image: ppurl
                  },
                  token: token
                };
                admin.messaging().send(message)
                .then((response) => {
                })
                .catch((error) => {
                });
              }
            });
          }
        });
      }

    }
  });

});

我该如何解决?

值得一提。我真的是JavaScript的开端。

编辑:错误

更新:即使当我使用github(https://github.com/firebase/functions-samples/blob/Node-8/fcm-notifications/functions/index.js)上的官方示例代码时,仍然会给出相同的错误。

“意外令牌=>”

完整错误日志:

    0 info it worked if it ends with ok
1 verbose cli [ '/usr/bin/node',
1 verbose cli   '/usr/bin/npm',
1 verbose cli   '--prefix',
1 verbose cli   '/home/ali/Documents/ShareFunction/functions',
1 verbose cli   'run',
1 verbose cli   'lint' ]
2 info using npm@6.4.1
3 info using node@v11.1.0
4 verbose run-script [ 'prelint', 'lint', 'postlint' ]
5 info lifecycle functions@~prelint: functions@
6 info lifecycle functions@~lint: functions@
7 verbose lifecycle functions@~lint: unsafe-perm in lifecycle true
8 verbose lifecycle functions@~lint: PATH: /usr/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/home/ali/Documents/ShareFunction/functions/node_modules/.bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/var/lib/snapd/snap/bin
9 verbose lifecycle functions@~lint: CWD: /home/ali/Documents/ShareFunction/functions
10 silly lifecycle functions@~lint: Args: [ '-c', 'eslint .' ]
11 silly lifecycle functions@~lint: Returned: code: 1  signal: null
12 info lifecycle functions@~lint: Failed to exec lint script
13 verbose stack Error: functions@ lint: `eslint .`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (/usr/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:301:16)
13 verbose stack     at EventEmitter.emit (events.js:182:13)
13 verbose stack     at ChildProcess.<anonymous> (/usr/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
13 verbose stack     at ChildProcess.emit (events.js:182:13)
13 verbose stack     at maybeClose (internal/child_process.js:970:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:257:5)
14 verbose pkgid functions@
15 verbose cwd /home/ali/Documents/ShareFunction
16 verbose Linux 4.19.2-1-MANJARO
17 verbose argv "/usr/bin/node" "/usr/bin/npm" "--prefix" "/home/ali/Documents/ShareFunction/functions" "run" "lint"
18 verbose node v11.1.0
19 verbose npm  v6.4.1
20 error code ELIFECYCLE
21 error errno 1
22 error functions@ lint: `eslint .`
22 error Exit status 1
23 error Failed at the functions@ lint script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]

0 个答案:

没有答案