2016-09-23 90 views
2

我将Firebase通知集成到了我的应用程序中,但我想发送一个通知,以打开特定活动并执行我计划要做的事情,而不仅仅是打开该应用程序。就像通知会推动用户点击Google Play商店一样。从Firebase通知中打开特定活动

我看到了一个代码Firebase console: How to specify click_action for notifications,但我在初始化变量cls时出错。我试图通过定义cls = null来解决,以清除错误。它无法打开我指定的活动使用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); 
}  
} 

请问我有什么问题吗?我如何得到这个工作?

回答

5

如果您想要打开应用并执行特定操作[同时背景],请在通知有效内容中设置click_action并将其映射到要启动的活动中的意图过滤器。例如,设置click_action到OPEN_ACTIVITY_1触发像一个意图过滤以下:

正如FCM文件建议,要求后端在这样的形式发送JSON数据,

{ 

    "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文件为您的活动添加意图过滤器,如下

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

当您单击该通知,它会打开应用程序,直接进入您在click_action定义,在这种情况下,“OPEN_ACTIVTY_1”活动。而这一活动中,你可以得到的数据是:

Bundle b = getIntent().getExtras();// add these lines of code to get data from notification 
String someData = b.getString("someData"); 

退房下面的链接以获得更多帮助:

Firebase FCM notifications click_action payload

Firebase onMessageReceived not called when app in background

Firebase console: How to specify click_action for notifications