如何检测通知密钥node.js

时间:2018-06-09 07:53:05

标签: node.js firebase google-cloud-functions

我已经成功实现了node.js中的一个函数,该函数在firebase云函数中触发,运行如下:

  1. 当新用户使用其token_id注册到数据库时,会创建通知密钥。
  2. 当同一用户使用其他设备(此处更新token_id)登录到数据库时,token_id将添加到现有的notification_key中。
  3. 当用户退出设备时,token_id将从notification_key中删除。
  4. 这三点都运行得很棒。

    但我的问题是,当notification_key中只有一个token_id,并且用户注销时(它运行正常,直到此处),并且当用户再次登录时,它会给我错误

    body: { error: 'notification_key not found' } }
    

    但是,我的notification_key存在!

    我得知如果notification_key中没有token_id,则notification_key变为无效。

    但是如何知道notification_key中存在的设备数量,以便我可以采取相应的行动。

    exports.saveGroups = functions.firestore.document("Users/{user_id}").onWrite((change,context) => {
    
    
    
    token_id1 = change.after.data().token_id;
    token_email = change.after.data().email;
    image1 = change.after.data().image;
    name1 = change.after.data().name;
    user_id = context.params.user_id;
    notification_key = change.after.data().notification_key;
    console.log('token_id1: ' + token_id1);
    console.log('token_email: ' + token_email);
    console.log('Image: ' + image1);
    console.log('name: ' + name1);
    console.log('user_id: ' + user_id);
    var a;
    
    
    
    
    
        var headers = {
       'Authorization': 'key = AAAATmJbwHE:APA91bGIEsq0aioIzAa_7g5JvLX1NPU3z1Gkt6vxB2J-I_9IvllwDJaxjmEp5DPw6ZoEBmXfwiYwICrMuE0kQrvxuHTGPc5YKr3i-JNru-o6AHnrAjq4r7iZWmzUuSmPwu8CbR2kXEIq',
       'project_id': '336657629297',
       'Content-Type': 'application/json'
    }
    
    if (notification_key === undefined) {
         console.log('key was not present, hence create');
        var options = {
        url: 'https://android.googleapis.com/gcm/notification',
        method: 'POST',
        headers: headers,
        json: {'operation': 'create',
        'notification_key_name': token_email,
        'registration_ids': [token_id1]}
    }
    
    var ret = rp(options).then(function (response,body) {
        a = response.notification_key;
       return a;
    }).catch(function (error) {
        // POST failed...
        return console.log(error);
    
    }).then(result =>{
      console.log('key: ' + a);
      return db.collection('Users').doc(user_id).set({name:name1,notification_key:a,image:image1,token_id:token_id1,email:token_email});
    });
    }else if(token_id1 !== undefined){
     console.log('key was present, hence add');
    options = {
        url: 'https://android.googleapis.com/gcm/notification',
        method: 'POST',
        headers: headers,
        json: {'operation': 'add',
        notification_key: notification_key,
        'notification_key_name': token_email,
        'registration_ids': [token_id1]}
    }
    
    ret = rp(options).then(function (response,body) {
        a = response.notification_key;
       return a;
    }).catch(function (error) {
        // POST failed...
        return console.log(error);
    
    }).then(result =>{
      console.log('key: ' + a);
      return db.collection('Users').doc(user_id).set({name:name1,notification_key:a,image:image1,token_id:token_id1,email:token_email});
    });
    } else{
     console.log('User Logged Out, Remove The Key Now');
     token_idbefore = change.before.data().token_id;
     console.log('token_idbefore: ' + token_idbefore);
    options = {
        url: 'https://android.googleapis.com/gcm/notification',
        method: 'POST',
        headers: headers,
        json: {'operation': 'remove',
        notification_key: notification_key,
        'notification_key_name': token_email,
        'registration_ids': [token_idbefore]}
    }
    
    ret = rp(options).then(function (response,body) {
        a = response.notification_key;
       return a;
    }).catch(function (error) {
        // POST failed...
        return console.log(error);
    
    }).then(result =>{
      console.log('key: ' + a);
      return db.collection('Users').doc(user_id).set({name:name1,notification_key:a,image:image1,email:token_email});
    });
    } 
      return ret;
     });
    

0 个答案:

没有答案