Android应用会在收到推送通知时自动导航到其他活动

时间:2018-07-04 14:59:26

标签: java android firebase firebase-cloud-messaging

我构建了一个具有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"

 }

0 个答案:

没有答案