0

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);
        }

    }

}
Maverick7
  • 1,087
  • 12
  • 22

2 Answers2

1

Don't call super.onMessageReceived(remoteMessage);

and implement sendNotification(String messageBody) {}method from here. then change the onMessageReceived method like this

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    if (remoteMessage.getNotification() != null) {
        sendNotification(remoteMessage.getNotification().getBody());            
    }       
}

If it works, then you can customize the notification

have a look here, for proper implementation

majurageerthan
  • 2,169
  • 3
  • 17
  • 30
0

The icon size should be as per this stack overflow question Notification icon size in android. My icon size overshot the specs and I ended up getting black screens. Please fix notification icon size to fix this.

Maverick7
  • 1,087
  • 12
  • 22