当前位置: 首页 > 知识库问答 >
问题:

应用关闭时,点击Firebase通知后打开特定活动/片段

微生善
2023-03-14

我知道这个问题似乎重复,但为了我的要求,我已经在网上搜索了很多帖子,但没有一个对我有效。

我的要求

为此,我尝试了以下场景

情景1

使用bundle将通知数据从FireBaseMessagingService传输到Launcher/Main活动,并根据bundle数据重定向到特定活动/片段

情景3

通过使用intent.putextra

按照上面的场景,当应用程序打开时,一切工作都很好,但如果我的应用程序关闭了,则意味着它总是重定向到main/launcher活动

    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra("reqTo", messageBody);

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

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

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

    notificationManager.notify(0, notificationBuilder.build());

共有1个答案

赫连法
2023-03-14
public void showNotificationMessage(final String title, final String message, Intent intent) {
    // Check for empty push message
    if (TextUtils.isEmpty(message))
        return;


// notification icon
final int icon = R.drawable.logo;

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
final PendingIntent resultPendingIntent =
        PendingIntent.getActivity(
                mContext,
                0,
                intent,
                PendingIntent.FLAG_CANCEL_CURRENT
        );

final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
        mContext);

final Uri alarmSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
        + "://" + mContext.getPackageName() + "/raw/notification");


    showSmallNotification(mBuilder, icon, title, message, resultPendingIntent, alarmSound);
    playNotificationSound();

}



private void showSmallNotification(NotificationCompat.Builder mBuilder, int icon, String title, String message, PendingIntent resultPendingIntent, Uri alarmSound) {

            NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();

            inboxStyle.addLine(message);

            Notification notification;
            notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
                    .setAutoCancel(true)
                    .setContentTitle(title)
                    .setContentIntent(resultPendingIntent)
                    .setSound(alarmSound)
                    .setStyle(inboxStyle)
                    .setSmallIcon(R.drawable.logo)
                    .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
                    .setContentText(message)
                    .build();

            NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(Config.NOTIFICATION_ID, notification);
        }


private void showBigNotification(Bitmap bitmap, NotificationCompat.Builder mBuilder, int icon, String title, String message, PendingIntent resultPendingIntent, Uri alarmSound) {
        NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();
        bigPictureStyle.setBigContentTitle(title);
        bigPictureStyle.setSummaryText(Html.fromHtml(message).toString());
        bigPictureStyle.bigPicture(bitmap);
        Notification notification;
        notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
                .setAutoCancel(true)
                .setContentTitle(title)
                .setContentIntent(resultPendingIntent)
                .setSound(alarmSound)
                .setStyle(bigPictureStyle)
                .setSmallIcon(R.drawable.logo)
                .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
                .setContentText(message)
                .build();

        NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(Config.NOTIFICATION_ID_BIG_IMAGE, notification);
    }

您可以这样检查app是否关闭

if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
 类似资料:
  • 我正在我的应用程序中实施谷歌云消息服务(GCM)。我使用gcmIntent服务创建了一个挂起的意图并打开了一个不是启动活动的活动。当应用程序打开时,它工作正常。但当应用程序关闭时,它会打开启动活动而不是所需的活动。我尝试了我能找到的所有解决方案,但都不起作用。我挣扎了一个多星期。任何帮助都将不胜感激。 我的代码 我得到下面的堆栈跟踪

  • 我看到了一个Firebase控制台的代码:如何为通知指定click_action,我使用了该通知,但在初始化变量CLS时出现了一个错误。我试图通过定义CLS=NULL来解析,以清除错误。它无法使用click_action打开我指定的活动 拜托,我有没有搞错什么?我怎么才能让它起作用?

  • 当用户在我的通知中单击一个按钮时,我正试图打开,而该应用程序仅在后台运行并提供服务。单击按钮时,这些行在类中被触发: 我已经检查过了,这些行被触发了,所以对按钮的点击做出反应没有问题,但是不会打开。 有什么建议吗?为什么这对我不起作用,我怎么能让它起作用? 我被要求提供更多的代码,因此在我的

  • 我正在工作的应用程序中,我被要求显示通知。对于通知,我使用的是FireBase云消息(FCM)。我能够得到通知,当应用程序在后台。 2.)MyFirebaseInstanceIDService 这是我在MyFirebaseMessagingService类中为onMessageReceived()方法编写的代码示例。

  • 仅当打开应用并执行通知单击时,通知单击才会启动指定的活动。如果应用程序处于后台/未运行,并且执行了通知单击,则会打开应用程序的 MainActivity。简而言之,这就像应用程序在活动堆栈之后正常打开,而不是在 PendingIntent 中打开指定的活动。 我想根据它们的类型将通知单击重定向到两个不同的活动(批准详细活动和对话详细活动)。 我使用FCM进行推送通知。我在这里粘贴我的Manifes