使用适用于Firebase的云功能杀死FCM时,不会收到通知

时间:2017-03-18 21:33:16

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

Firebase目前使用Firebase推出Cloud Functions以添加服务器端代码。

我目前正在使用此方法收到来自发件人的邮件通知。

一切似乎都很好但是当我杀了应用程序时我没有收到通知。

我看到了一些关于它的答案,我应该只使用数据消息并在onMessageReceived()中接收,但不适用于已杀死的应用。我该怎么办?

NodeJS Index.js

exports.sendNewMessageNotification = functions.database.ref('/rootssahaj/authGplus/users/{userTorS}/{userTeacherUID}/messages/{chatWithOthUser}/{messageUID}').onWrite(event => {
console.log('called1 ');
const TeacherUid = event.params.userTeacherUID;
const whoTorS=event.params.userTorS;
var whoOppTorS=null;
if (whoTorS=="teachers") {
    whoOppTorS="students";
}else{
    whoOppTorS="teachers";
}
var StudentUid = event.params.chatWithOthUser;
StudentUid=StudentUid.substring(8);

console.log('called2 ')

if (!event.data.val()) {
return console.log('No Change ');
}

console.log('Event data: ',StudentUid, event.data.val());
if (StudentUid!=event.data.val().sender) {
return console.log('Different sender ',event.data.val().sender);
}

// Get the list of device notification tokens.
const getDeviceTokensPromise = admin.database().ref(`/rootssahaj/authGplus/users/${whoTorS}/${TeacherUid}/profile/fcmtoken`).once('value');

// Get the follower profile.
const getFollowerProfilePromise = admin.database().ref(`/rootssahaj/authGplus/users/${whoOppTorS}/${StudentUid}/profile`).once('value');

return Promise.all([getDeviceTokensPromise, getFollowerProfilePromise]).then(results => {
const tokensSnapshot = results[0];
const follower = results[1];
console.log('Token: ', tokensSnapshot.val(),' ',follower.val());
// Check if there are any device tokens.
if (tokensSnapshot.val()==null) {
  return console.log('There are no notification tokens to send to.');
}
console.log('There are', tokensSnapshot.numChildren(), tokensSnapshot,'tokens to send notifications to.');
console.log('Fetched follower profile', follower.val().userNAME);

// Notification details.
const payload = {
  data: {
    body: `new message: ${event.data.val().text}`,
    title: `${follower.val().userNAME}`,
  }
};
var options = {
priority: "high"
};

// Listing all tokens.
//const tokens = Object.keys(tokensSnapshot.val());
// console.log('tokens', tokens);

// Send notifications to all tokens.
return admin.messaging().sendToDevice(tokensSnapshot.val(), payload,options).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', 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(tokensSnapshot.ref.child(tokens[index]).remove());
      }
    }
  });
  return Promise.all(tokensToRemove);
});
});
});

这是我在app杀死状态时触发Fcm时收到的内容。我找了它但找​​不到合适的解决方案来解决它。

  

W / GCM-DMM(29459):广播意图回调:result = CANCELED forIntent {act = com.google.android.c2dm.intent.RECEIVE flg = 0x10000000 pkg = com.rana.sahaj.myyu(有额外内容) }

2 个答案:

答案 0 :(得分:0)

请确保您在承诺中检索的设备令牌是最新的。设备令牌可以在模拟器或Android设备中更改/更新,原因有很多。

请确认您在Firebase中的功能的LOGS标签下没有云功能的任何错误日志。

当你杀死应用时我可以确认(在Android中刷掉应用)你仍会收到通知。

编辑:添加了清单

<form method="POST" action="/update">
    Task Name:<input type="text" value="{{$task->TaskName}}" name="taskname"><br>
    Description:<input type="text" value="{{$task->Description}}" name="description"><br>
    Location id:<input type="text" value="{{$task->Location_id}}" name="location_id"><br>
    @if($task->status === 1)
    Status:<input type="checkbox" checked name="status"><br>
    @else
    Status:<input type="checkbox" name="status"><br>
    @endif
    Created at:<input type="" value="{{$task->created_at}}" name="created_at"><br>
    Modified at:<input type="date" value="{{$task->updated_at}}" name="modified_at"><br>
    <input type="submit" value="update" name="upadte" ><br>
    {{csrf_field()}}
</form>

答案 1 :(得分:0)

Firebase支持两种类型的通知:

  1. 通知消息
  2. 数据讯息
  3. 数据消息需要在客户端处理,并且在应用程序被杀死时不会收到。

    Firebase控制台发送通知消息,即使应用程序被终止也会收到通知消息

    从云端功能中,您需要发送通知消息,以便在应用被杀时接收到

    //Notification message
    var patyload = {
        notification: {
           title: "title"
      }
    };
    
    //Data Message
    var payload = {
        data: {
          title: "title"
      }
    };
    
    admin.messaging().sendToTopic(topic, payload,options)
    

    详细了解此处的通知

    https://firebase.google.com/docs/cloud-messaging/concept-options