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



我知道这个问题似乎是重复的,但是对于我的要求,我已经在线搜索了许多帖子,但是对我没有任何帮助。

我的要求

我正在使用firebase获取推送通知。当应用程序打开时,所有内容都可以正常工作,但是我的问题是,如果出现任何推动通知,则应用程序在后台/关闭,这意味着我想在单击该通知时打开特定的活动或片段,但是它总是打开启动器/主活动。

为此,我尝试了以下方案

方案1

使用bundle将通知数据从FirebaseMessagingService传输到启动器/主活动,并基于将我重定向到特定活动/片段的捆绑数据

方案2

使用Sharedpreference

方案3

使用intent.putExtra

按照上述方案打开应用程序时的所有功能正常,但是如果关闭我的应用程序,则意味着它总是重定向到主/发射器活动

我的代码

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

所以,当关闭应用程序时,单击firebase推送通知时,有人知道如何打开特定的活动/片段吗?

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

您可以检查封闭的应用程序或不喜欢此应用

if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {

这与我在此处给出的答案非常相似:https://stackoverflow.com/a/414441631/1291714

总而言之,您需要使用Firebase云消息传递而不是Firebase通知,以便在后台接收消息并进行自定义处理。您需要向客户端发送"数据"消息,然后在您的服务中,您将始终接收回调,无论该应用在前景还是背景中。

使用Firebase通知,您只有在应用在前景中时才会收到回调。当应用在后台时,系统将处理并显示用户的通知。您将无法自定义此通知以打开其他意图。

在此处阅读更多:https://firebase.google.com/docs/notifications/android/console-audience

相关内容

  • 没有找到相关文章

最新更新