I have written a Firebase Cloud Function to send a notification. After successfully sending the notification (as per logs), when I swipe down the notification bar to check Notifications in my Android Phone, the screen turns black. This is some weird behavior and has been happening with both the devices I tried this.
exports.someFunc = functions.firestore.document('ABC/{xyz}').onCreate((snap, context) =>
{
const newNotifMap = snap.data();
const name = newNotifMap.name;
// get the 2 userIds first
const sentById = newNotifMap.sent_by_id;
const receivedById = newNotifMap.received_by_id;
let receivedUserRef = db.collection('TYU').doc(receivedById);
// get the device_token of the person who received the message
let getDoc = receivedUserRef.get()
.then(doc => {
if (!doc.exists) {
console.log('No such document!');
} else {
const data = doc.data();
const device_token = data.device_token;
console.log('Device Token:', device_token );
const payload = {
notification: {
title: 'ABBB',
body: 'Lorem ipsum',
icon: "default",
sound: "default"
}
};
const a3=admin.messaging().sendToDevice(device_token, payload)
.then(function (response) {
console.log("Successfully sent message:", response);
return ;
})
.catch(function (error) {
console.log("Error sending message:", error);
});
}
return;
})
.catch(err => {
console.log('Error getting document', err);
});
});
My FirebaseMessagingService Class"
public class MyFirebaseMessagingService extends FirebaseMessagingService {
FirebaseFirestore db;
private final static String TAG = "MyFirebaseMessaging";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
}
@Override
public void onNewToken(String token) {
Log.d(TAG, "Refreshed token: " + token);
// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
sendRegistrationToServer(token);
}
private void sendRegistrationToServer(String token) {
db= FirebaseFirestore.getInstance();
FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
if(firebaseUser!=null)
{
String firebaseUserId = firebaseUser.getUid();
Map deviceTokenMap = new HashMap<>();
deviceTokenMap.put("device_token",token);
db.collection("TYA").document(firebaseUserId).update(deviceTokenMap);
}
}
}