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

4

我知道这个问题看起来很重复,但是根据我的需求,我已经在网上搜索了许多帖子,但没有一个能满足我的要求。

我的需求

我正在使用Firebase获取推送通知。当应用程序打开时一切正常,但我的问题是,如果应用程序在后台或关闭状态下收到任何推送通知,则单击通知时我希望打开特定的活动或片段,但它总是打开启动器/主要活动。

为此,我尝试了以下情况

情况1

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

情况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推送通知时打开特定的活动/片段吗?


请检查我的答案。如果您仍然有问题,请回复我,我会帮助您。 - Zaki Pathan
2个回答

2
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())) {

希望能有所帮助。你需要在这个函数中传递意图。 - Zaki Pathan
1
兄弟,我也面临着同样的问题 :-( - Bahu
我认为我们需要进入聊天。 - Zaki Pathan
当应用程序在前台运行时一切都正常,但是当应用程序关闭或在后台运行时,我无法打开特定的活动/片段。这是我正在使用的代码https://www.dropbox.com/s/6rphjkh6k6w27u8/FireBase.zip?dl=0。 - Bahu
让我们在聊天中继续这个讨论 - Zaki Pathan
@ZakiPathan 如果 (!NotificationUtils.isAppIsInBackground(getApplicationContext())) 不再起作用了,因为 GET TASK 权限已被弃用,或者还有其他解决方法吗? - Dilip Poudel

1

这与我在此处给出的答案非常相似:https://dev59.com/dJ3ha4cB1Zd3GeqPadjB#41441631

总之,您需要使用Firebase Cloud Messaging而不是Firebase Notifications才能在后台接收消息并进行自定义处理。您需要向客户端发送“数据”消息,然后在您的服务中,您将始终收到回调,无论应用程序是在前台还是后台。

使用Firebase Notifications时,仅当应用程序在前台时才会收到回调。当应用程序在后台时,系统将处理并显示通知。您将无法自定义此通知以打开不同的意图。

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接