gpt4 book ai didi

android - 关闭应用程序时单击 Firebase 通知后打开特定 Activity/fragment

转载 作者:搜寻专家 更新时间:2023-11-01 08:28:51 25 4
gpt4 key购买 nike

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

我的要求

我正在使用 Firebase 获取推送通知。当应用程序打开时意味着一切正常,但我的问题是,如果有任何推送通知出现,应用程序处于后台/关闭意味着我想在单击该通知时打开特定的 Activity 或 fragment ,但它总是打开启动器/主要 Activity 。

为此,我尝试了以下场景

场景 1

使用 bundle 将通知数据从 FirebaseMessagingService 传输到启动器/主 Activity ,并根据我重定向到特定 Activity/fragment 的 bundle 数据

场景 2

通过使用Sharedpreference

场景 3

通过使用 intent.putExtra

按照上述情况,当应用程序打开时一切正常,但如果我的应用程序关闭意味着它总是重定向到主/启动器 Activity

我的代码

    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 推送通知来打开特定的 Activity/fragment 。

最佳答案

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

关于android - 关闭应用程序时单击 Firebase 通知后打开特定 Activity/fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42268825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com