我知道这个问题似乎重复,但根据我的要求,我在网上搜索了很多帖子,但没有任何对我有用。
我的要求
我正在使用Firebase来获取推送通知。当应用程序被打开意味着一切正常但我的问题是,app是在后台/关闭如果任何推送通知来了意味着我想在点击该通知时打开特定活动或片段,但它总是打开启动器/主要活动。
为此,我尝试了以下方案
场景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推送通知时如何打开特定活动/片段。
答案 0 :(得分:1)
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())) {
答案 1 :(得分:1)
这与我在这里给出的答案非常相似:https://stackoverflow.com/a/41441631/1291714
总之,您需要使用Firebase云消息传递而不是Firebase通知,以便在后台接收消息并进行自定义处理。您需要发送"数据"向客户发送消息,然后在您的服务中,您将始终接收回调,无论应用是在前台还是后台。
使用Firebase通知,您只会在应用位于前台时收到回调。当应用程序在后台时,系统将处理并显示用户的通知。您无法自定义此通知以打开不同的意图。
在此处阅读更多内容:https://firebase.google.com/docs/notifications/android/console-audience