打开特定活动并在杀死应用时单击通知时发送数据

发布于 2025-02-09 08:15:36 字数 2388 浏览 2 评论 0原文

我有一个使用Firebase推送通知实施的应用程序。 我有一个场景,我可以单击通知,打开一个notiticatiendetails活动,并在文本视图中显示通知消息。当应用在前景时,它可以正常工作。 但是,当该应用程序在接收通知时处于杀死状态时,它不会导航到NotiticatIndetails活动。它仅发射主要活动。取而代之的是,我想启动NotiticatIndetails活动,并显示通知消息,就像我在前景情况下的操作一样。 而且,当我在NotiticatIndetails活动中单击“返回/关闭”按钮时,它必须导航到先前的活动(APP杀死状态下的主动脉),而不是关闭应用程序。

这就是我向Firebaseservice类的NotiticAttientails活动发送通知的方式。在OnMessagereceived方法中调用以下方法。

private void sendNotification(String msg) {

        Intent notifyIntent = new Intent(ctx, NotificationDetailsActivity.class);
        notifyIntent.putExtra("NotificationMessage", msg);
        notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

        mNotificationManager = (NotificationManager)
                ctx.getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent notifyPendingIntent = PendingIntent.getActivity(ctx, 0,
                notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
                ctx,
                NOTIFICATION_CHANNEL_ID)
                .setContentText(msg)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setSmallIcon(android.R.drawable.ic_popup_reminder)
                .setAutoCancel(true)
                .setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL);

        notificationBuilder.setContentIntent(notifyPendingIntent);
        mNotificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
        if (NotificationDetailsActivity.isVisible) {
            NotificationDetailsActivity.notificationDetailsActivity.ToastNotify(msg);
        }
    }

在以下内容中调用Newnewintent方法。

protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Bundle extras = intent.getExtras();
        if(extras != null){
            if(extras.containsKey("NotificationMessage")){
                String msg = extras.getString("NotificationMessage");
                TextView helloText = (TextView) findViewById(R.id.text_hello);
                helloText.setText(msg);
            }
        }
    }

我在MainActivity中也添加了同样的Onnewintent方法,但没有接收数据。 请帮助我导航到通知攻击,并在应用程序处于杀戮状态时显示数据。

I have an application implemented with firebase push notifications.
I have a scenario where I open a NotiticatinDetails activity on click of the notification and show the notification message in a textview. It works fine when the app is in foreground.
But it doesn't navigate to NotiticatinDetails activity when the app is in kill state at the time of receiving notification. It launches MainActivity only. Instead, I want to launch NotiticatinDetails activity and show the notification message just like how I am doing it in the foreground case.
And when I click on back/close button in NotiticatinDetails activity it has to navigate to previous activity (MainActivity in app killed state) instead of closing the application.

This is how I am sending notification to NotiticatinDetails activity from my FirebaseService class. Calling below method in onMessageReceived method.

private void sendNotification(String msg) {

        Intent notifyIntent = new Intent(ctx, NotificationDetailsActivity.class);
        notifyIntent.putExtra("NotificationMessage", msg);
        notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

        mNotificationManager = (NotificationManager)
                ctx.getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent notifyPendingIntent = PendingIntent.getActivity(ctx, 0,
                notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
                ctx,
                NOTIFICATION_CHANNEL_ID)
                .setContentText(msg)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setSmallIcon(android.R.drawable.ic_popup_reminder)
                .setAutoCancel(true)
                .setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL);

        notificationBuilder.setContentIntent(notifyPendingIntent);
        mNotificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
        if (NotificationDetailsActivity.isVisible) {
            NotificationDetailsActivity.notificationDetailsActivity.ToastNotify(msg);
        }
    }

Calling onNewIntent method in NotificationDetails activity as below.

protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Bundle extras = intent.getExtras();
        if(extras != null){
            if(extras.containsKey("NotificationMessage")){
                String msg = extras.getString("NotificationMessage");
                TextView helloText = (TextView) findViewById(R.id.text_hello);
                helloText.setText(msg);
            }
        }
    }

I have added same onNewIntent method in MainActivity also but it doesn't receive the data.
Please help me out to navigate to NotificationActivity and show data when app is in killed state.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文