Firebase 消息传递 Android 设备屏幕变黑

Firebase Messaging turning Android Device Screen Black

我写了一个 Firebase Cloud Function 来发送通知。成功发送通知后(根据日志),当我向下滑动通知栏以查看 Android Phone 中的通知时,屏幕变黑了。这是一些奇怪的行为,我尝试过的两种设备都发生过这种情况。

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


});

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

    }

}

不要打电话给super.onMessageReceived(remoteMessage);

并实施 sendNotification(String messageBody) {} 来自 here 的方法。然后像这样更改 onMessageReceived 方法

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

如果可行,那么您可以自定义通知

看看here,以便正确实施

图标大小应符合此堆栈溢出问题 Notification icon size in android。我的图标大小超过了规格,最后我得到了黑屏。请修正通知图标大小以解决此问题。