在应用关闭时,点击Firebase通知后打开特定活动/片段 [英] Open particular activity/fragment after clicking on Firebase notification when app is closed

查看:218
本文介绍了在应用关闭时,点击Firebase通知后打开特定活动/片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



strong>



我正在使用Firebase获取推送通知。当应用程序打开意味着一切工作正常,但我的问题是,应用程序在后台/关闭,如果任何推送通知来意味着我想打开特定的活动或片段时,点击通知,但它总是打开启动器/主要活动。



为此,我尝试了以下方案:
$ b

情景1



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

情景2



使用 Sharedpreference



情景3



通过使用 intent.putExtra



场景一切工作正常,当应用程序被打开,但如果我的应用程序被关闭意味着它总是重定向到主/发射活动


$ b

我的代码

  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推送通知时打开特定活动/ Fragment?/ b





应用程序已关闭。

解决方案

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



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



借助Firebase通知,您只会在应用处于前台时收到回叫。当应用程序在后台时,系统将处理并显示用户的通知。您将无法自定义此通知来打开不同的意图。

更多信息: https://firebase.google.com/docs/notifications/android/console-audience


I know this question seems duplicate, but for my requirement, I have searched many posts online, but nothing worked for me.

My requirement

I'm using the Firebase to get the push notifications. When app was opened means everything working fine but my problem is, app is in background/closed if any push notification came means i want to open the particular activity or fragment when clicking on that notification, but it always opening the launcher/main activity.

For this, I have tried the following scenarios

Scenario 1

Used the bundle to transfer notification data from FirebaseMessagingService to the launcher/Main activity and based on the bundle data i'm redirecting to the particular activity/fragment

Scenario 2

By using Sharedpreference

Scenario 3

By using intent.putExtra

following the above scenarios everything working fine when app was opened but if my app was closed means it always redirecting to the main/launcher activity

My code

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

So, does anyone know how to open the particular activity/Fragment when clicking on the Firebase push notification when the app is closed.

解决方案

This is very similar to my answer given here: https://stackoverflow.com/a/41441631/1291714

In summary though, you need to use Firebase Cloud Messaging and not Firebase Notifications in order to receive a message in the background and do custom processing. You need to send "data" messages to the client and then in your service, you will then always receive the callback, whether the app is in the foreground or background.

With Firebase Notifications, you will only receive the callback when the app is in the Foreground. When the app is in the background, the system will handle and display the notification for a user. You won't be able to customise this notification to open up a different intent.

Read more here: https://firebase.google.com/docs/notifications/android/console-audience

这篇关于在应用关闭时,点击Firebase通知后打开特定活动/片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆