Firebase 消息传递仅适用于单独的设备

Firebase Messaging only works on separate devices

我目前正在为我的应用程序编写一项功能,允许用户向工作人员发出请求,他们可以在其中接受或拒绝工作机会。我正在使用 firebase 消息系统。当我有两个 phone 时,通知有效,工作人员可以接受或拒绝,在此处说明:

public class MyFirebaseMessaging extends FirebaseMessagingService {
    private static final String COLE_CHANNEL_ID = "com.example.usub.COLE";
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage){
        LatLng customer_location = new Gson().fromJson(remoteMessage.getNotification().getBody(),LatLng.class);
        Intent intent = new Intent(getBaseContext(), CustomerCall.class);


        intent.putExtra("lat", customer_location.latitude);
        intent.putExtra("lng", customer_location.longitude);
        intent.putExtra("customer", remoteMessage.getNotification().getTitle());
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
}

但是,我尝试 运行 我的 phone 上的演示涉及两个应用程序,我收到了已发出请求的通知,但是当单击通知时,应用程序打开没有数据的回忆。我的通知助手看起来像这样:

public class NotificationHelper extends ContextWrapper {

    private static final String COLE_CHANNEL_ID = "com.example.usub.COLE";
    private static final String COLE_CHANNEL_NAME = "STRADTMANNSOLUTIONS Usub";

    private NotificationManager manager;

    public NotificationHelper(Context base) {
        super(base);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
            createChannels();
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private void createChannels() {
        NotificationChannel coleChannels = new NotificationChannel(COLE_CHANNEL_ID,
                COLE_CHANNEL_NAME,
                NotificationManager.IMPORTANCE_DEFAULT);
        coleChannels.enableLights(true);
        coleChannels.enableVibration(true);
        coleChannels.setLightColor(Color.GRAY);
        coleChannels.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);

        getManager().createNotificationChannel(coleChannels);
    }

    public NotificationManager getManager() {
        if(manager == null)
            manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        return manager;
    }
    @RequiresApi(api = Build.VERSION_CODES.O)
    public Notification.Builder getUsubNotification(String title, String content, PendingIntent contentIntent,
                                                    Uri soundUri)
    {
        return new Notification.Builder(getApplicationContext(),COLE_CHANNEL_ID)
                .setContentText(content)
                .setContentTitle(title)
                .setAutoCancel(true)
                .setSound(soundUri)
                .setContentIntent(contentIntent)
                .setSmallIcon(R.drawable.ic_notify);
    }
}

有谁知道为什么通知会丢失所有数据,或者我如何打开应用程序以显示某个 activity 并显示按下通知后的新信息?谢谢

只有当您的应用程序在前台时,通知消息才会传送到您的 onMessageReceived 回调。如果应用程序在后台,则会显示通知,但不会触发 onMessageRecieved,并且来自该消息的数据会传递给启动的意图。 你可以使用:-

getIntent().getExtras();

检索通过通知发送的数据。

检查文档 this