# #android #firebase-cloud-messaging
Вопрос:
Я не получаю push-уведомления FCM на своем устройстве Android. Я протестировал на консоли обмена сообщениями Firebase Cloud и получаю уведомления о тестировании. Однако, когда я пытаюсь отправить, запустив push-уведомление, внеся изменения в документ, я не получаю уведомления на физическом устройстве или эмуляторе. Я не получаю ошибок, и в журналах говорится, что сообщение было отправлено «ОК».
Есть ли какие-либо настройки, которые мне нужно выполнить на моем эмуляторе или физическом устройстве или на стороне Firebase?
Это работало, но в какой-то момент прекратилось. Я внес много изменений, поэтому не могу вернуться назад.
Вот код для функций.
/* eslint-disable */
const functions = require("firebase-functions");
//const addTrxns = require("./trxnAdd.js");
const admin = require("firebase-admin");
const { user } = require("firebase-functions/lib/providers/auth");
const { event } = require("firebase-functions/lib/providers/analytics");
//exports.addTrxns.addTrxns;
admin.initializeApp();
const db = admin.firestore();
/* ==================== USER PROFILE CHANGED ============================================ */
exports.userProfileChanged = functions.firestore.document('/agents/{userId}').onWrite( async (change, context) => {
const userId = context.params.userId;
const afterData = change.after.data();
const agentId = afterData.agentId;
console.log('A change has been made to ' userId ' profile');
/**** GET DEVICE INFORMATION ****/
const deviceDoc = db.collection('device').doc(agentId);
if (deviceDoc == null) {
console.log('No device document found');
}
const deviceData = await deviceDoc.get();
const deviceId = deviceData.get('token');
console.log('deviceId: ', deviceId);
if (deviceId == null) {
return console.error('No device tokens found');
}
let title = "Changes have been made your Tonnah profile";
let body = "Changes have been made to your user profile";
const payload = {
notification: { title: title, body: body},
data: {click_action: 'FLUTTER_NOTIFICATION_CLICK' }
};
const response = await admin.messaging().sendToDevice(deviceId, payload);
if (response.error) {
console.error("Error sending message: ", response.error);
} else {
return console.log("Message sent successfully!");
};
return Promise.all(console.log('End of function'));
});
/* ==================== ADD NEW TRXN ==================================================== */
exports.onTrxnCreate = functions.firestore.document('/trxns/{trxnId}').onCreate(async(snap, context) => {
/*const userId = context.params.userId;*/
const agentId = snap.data().agentId;
console.log('A new transaction has been added');
/**** GET DEVICE INFORMATION ****/
const deviceDoc = db.collection('device').doc(agentId);
if (deviceDoc == null) {
console.log('No device document found');
}
const deviceData = await deviceDoc.get();
const deviceId = deviceData.get('token');
console.log('deviceId: ', deviceId);
if (deviceId == null) {
return console.error('No device tokens found');
}
/**** GET TRXN INFORMATION ****/
const trxnId = context.params.trxnId;
/*console.log('trxnId');*/
const trxnDoc = db.collection('trxns').doc(trxnId);
if (trxnDoc == null) {
console.log('No trxn document found');
}
const trxnData = await trxnDoc.get();
const clientFName = trxnData.get('clientFName');
const clientLName = trxnData.get('clientLName');
console.log('Trxn: ' clientFName ' ' clientLName ' updated ');
let title = "Transaction added";
let body = "A new transaction has been added for " clientFName ' ' clientLName;
const payload = {
notification: { title: title, body: body},
data: {click_action: 'FLUTTER_NOTIFICATION_CLICK', screen: 'TransactionDetailScreen' }
};
const response = await admin.messaging().sendToDevice(deviceId, payload);
if (response.error) {
console.error("Error sending message: ", response.error);
} else {
return console.log("Message sent successfully!");
};
return Promise.all(console.log('End of function'));
});
/* ==================== UPDATE TRXNS ==================================================== */
exports.onTrxnUpdate = functions.firestore.document('/trxns/{trxnId}').onUpdate(async (change, context) => {
const afterData = change.after.data();
const agentId = afterData.agentId;
/*console.log('context: ', context);*/
console.log('A transaction has been updated');
/**** GET DEVICE INFORMATION ****/
const deviceDoc = db.collection('device').doc(agentId);
/*console.log('deviceDoc: ', deviceDoc);*/
if (deviceDoc == null) {
console.log('No device document found');
}
const deviceData = await deviceDoc.get();
const deviceId = deviceData.get('token');
/*console.log('deviceId: ', deviceId);*/
if (deviceId == null) {
return console.error('No device tokens found');
}
/**** GET TRXN INFORMATION ****/
const trxnId = context.params.trxnId;
/*console.log('trxnId');*/
const trxnDoc = db.collection('trxns').doc(trxnId);
if (trxnDoc == null) {
console.log('No trxn document found');
}
const trxnData = await trxnDoc.get();
const clientFName = trxnData.get('clientFName');
const clientLName = trxnData.get('clientLName');
console.log('Trxn: ' clientFName ' ' clientLName ' updated ');
let title = "Transaction updated";
let body = "Trxn: " clientFName " " clientLName " updated ";
const payload = {
notification: { title: title, body: body},
data: {click_action: 'FLUTTER_NOTIFICATION_CLICK' }
};
const response = await admin.messaging().sendToDevice(deviceId, payload);
if (response.error) {
console.error("Error sending message: ", response.error);
} else {
return console.log("Message sent successfully!");
};
return Promise.all(console.log('End of function'));
});
Комментарии:
1. Не могли бы вы вставить какой-нибудь код для справки?
2. @che10 Я добавил код из своего index.js файл.