首页 > 解决方案 > Firebase 消息传递仅适用于单独的设备

问题描述

我目前正在为我的应用程序编写一个功能,该功能允许用户向工人提出请求,他们可以接受或拒绝工作机会。我正在使用firebase消息传递系统。当我有两部手机时,通知有效,工作人员可以接受或拒绝,如下所述:

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

但是,我尝试在我的手机上运行涉及这两个应用程序的演示,我收到已发出请求的通知,但是当单击通知时,应用程序打开时不记得数据。我的通知助手如下所示:

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

有谁知道为什么通知会丢失其所有数据,或者我如何使用通知消息中的新信息打开应用程序以进行特定活动?谢谢

标签: javaandroidfirebase-cloud-messaging

解决方案


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

getIntent().getExtras();

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

检查这个文档


推荐阅读