从Firebase通知中打开特定的活动 [英] open a specific activity from firebase notification

查看:237
本文介绍了从Firebase通知中打开特定的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Firebase通知集成到了我的应用程序中,但是我想发送一个通知来打开一个特定的活动,并按照我的计划去做,而不仅仅是打开应用程序。就像通知会推动用户访问Google Play商店一样。

我看到一个代码 Firebase控制台:如何为通知指定click_action ,但是我正在获取初始化变量cls的错误。我试图通过定义cls = null来解决,以清除错误。它无法打开我指定的活动,使用click_action

  public class ClickActionHelper {
public static void startActivity(String className, Bundle extras,Context context){
Class cls = null;
尝试{cls = Class.forName(className);

catch(ClassNotFoundException e){
//表示您在Firebase控制台中输入了错误信息
}
Intent i = new Intent(context,cls);
i.putExtras(extras); context.startActivity(ⅰ);
}
}

请问我有什么问题吗?如何获得此功能?

解决方案

如果要打开应用程序并执行特定操作在通知负载中设置click_action,并将其映射到您要启动的Activity中的intent过滤器。例如,将click_action设置为OPEN_ACTIVITY_1以触发意向过滤器,如下所示:

正如react-native-fcm文档中所述,请求后端发送json数据在这样的形式,

  {

to:some_device_token,
$ bcontent_available:true,
notification:{
title:hello,
body:yo,
click_action:OPEN_ACTIVITY_1//用于您的活动中的意图过滤
},

data:{
extra:juice
}





在你的mainfest文件中为你的活动添加intent-filter如下

 < intent-filter> 
< category android:name =android.intent.category.DEFAULT/>
< / intent-filter>

当您点击通知时,它会打开应用程序并直接转到您在click_action中定义的活动,在这种情况下是OPEN_ACTIVTY_1。在这个活动中,你可以通过以下方式获得数据:
$ b $ pre $ b $ getIntent getExtras(); // add these从通知中获取数据的代码行
String someData = b.getString(someData);

查看下面的链接获取更多帮助:

Firebase FCM通知click_action有效内容



​​ Firebase onMessageReceived不会在后台应用程序中调用 a>

Firebase控制台:如何为通知指定click_action


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.

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?

解决方案

If you want to open your app and perform a specific action [while backgrounded], set click_action in the notification payload and map it to an intent filter in the Activity you want to launch. For example, set click_action to OPEN_ACTIVITY_1 to trigger an intent filter like the following:

As suggested in react-native-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"
}
}

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>

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 helps:

Firebase FCM notifications click_action payload

Firebase onMessageReceived not called when app in background

Firebase console: How to specify click_action for notifications

这篇关于从Firebase通知中打开特定的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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