首页 > 解决方案 > Android 应用程序在收到推送通知后自动导航到不同的活动

问题描述

我用 Firebase 功能和 firebase 云消息构建了一个 android 聊天应用程序。但我意识到一些很奇怪的事情。在通知显示之前,应用程序将自动导航到主要活动。可能是什么问题呢?我根本不知道为什么会发生这种情况。我猜它来自我的 OnMessageReceived 但我不确定。我将在下面发布它,以便更容易跟踪错误。

我的 OnMessage 已收到

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            showNotification(remoteMessage.getData().get("name"), (remoteMessage.getData().get("click_action")), remoteMessage.getData().get("title"));
        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {

        }


    }

    private void showNotification(String name, String click_action, String title) {
        Intent intent;
        switch (click_action) {
            case "Download":
                intent = new Intent(this, Download.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                break;
            case "Student_SystemsDevt":
                intent = new Intent(this, Student_SystemsDevt.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                break;
            case "Chats":
                intent = new Intent(this, LoginActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                break;
            default:
                intent = new Intent(this, LoginActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                break;
        }

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setContentTitle(title)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentText(name)
                .setAutoCancel(true)
                .setStyle(new NotificationCompat.BigTextStyle())
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
}

下面是结构;

"data" : {

 "name" : "What's up", 
"title" : "Upward Studios", 
"click_action" : "chats"

 }

标签: javaandroidfirebasefirebase-cloud-messaging

解决方案


推荐阅读