从 Firebase 通知中打开特定活动 [英] open a specific activity from firebase notification
问题描述
我将 firebase 通知集成到我的应用程序中,但我想发送一个通知来打开特定活动并执行我计划执行的操作,而不仅仅是打开应用程序.就像一个通知,它会促使用户点击它访问 Google Play 商店.
I integrated firebase notification to my application but I would like to send a notification that opens a specific activity and does what I schedule to do, not just opening the App. like a notification that will push the user to visit Google play store on clicking it.
我看到了一个代码 Firebase 控制台:如何指定 click_action对于我使用过的通知,但在初始化变量 cls 时出错.我试图通过定义 cls=null 来解决,以清除错误.使用 click_action 无法打开我指定的活动
I saw a code Firebase console: How to specify click_action for notifications which I used but am getting an error to initialize variable cls. I tried to resolve by defining cls=null, to clear error. It fails to open my specified activity using the click_action
public class ClickActionHelper {
public static void startActivity(String className, Bundle extras, Context context){
Class cls=null;
try {
cls = Class.forName(className);
} catch (ClassNotFoundException e) {
//means you made a wrong input in firebase console
}
Intent i = new Intent(context, cls);
i.putExtras(extras); context.startActivity(i);
}
}
请问我有什么问题吗?我如何让它发挥作用?
please am I getting anything wrong? How do I get this to work?
推荐答案
如果你想打开你的应用程序并执行一个特定的操作 [while backgrounded],在通知负载中设置 click_action 并将其映射到一个意图过滤器在您要启动的活动中.例如,将 click_action 设置为 OPEN_ACTIVITY_1 以触发如下所示的意图过滤器:
按照 FCM 文档中的建议,要求后端以这样的形式发送 JSON 数据,
As suggested in FCM docs, ask backend to send JSON data in the form like this,
{
"to": "some_device_token",
"content_available": true,
"notification": {
"title": "hello",
"body": "yo",
"click_action": "OPEN_ACTIVITY_1" // for intent filter in your activity
},
"data": {
"extra": "juice"
}
}
并在您的 mainfest 文件中为您的活动添加意图过滤器,如下所示
and in your mainfest file add intent-filter for your activity as below
<intent-filter>
<action android:name="OPEN_ACTIVITY_1" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
当您点击通知时,它会打开应用并直接进入您在 click_action 中定义的活动,在本例中为OPEN_ACTIVTY_1".在该活动中,您可以通过以下方式获取数据:
When you click the notification, it will open the app and go straight to activity that you define in click_action, in this case "OPEN_ACTIVTY_1". And inside that activity you can get the data by :
Bundle b = getIntent().getExtras();// add these lines of code to get data from notification
String someData = b.getString("someData");
查看以下链接以获得更多帮助:
Check out below links for more help:
Firebase FCM 通知 click_action 负载
当应用在后台时未调用 Firebase onMessageReceived
Firebase 控制台:如何为通知指定 click_action一个>
这篇关于从 Firebase 通知中打开特定活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!