我将firebase通知集成到了我的应用程序中,但我想发送一个通知,打开一个特定的活动,按照我的计划做,而不仅仅是打开应用程序。比如通知会在用户点击它时访问Google Play商店。
我看到了我使用的代码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);
}
}
我有什么不对吗?我如何让它工作?
答案 0 :(得分:13)
如果您要打开应用并执行特定操作[在后台运行时],请在通知有效内容中设置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,如下所示
<intent-filter>
<action android:name="OPEN_ACTIVITY_1" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
当您点击通知时,它会打开应用并直接转到您在click_action中定义的活动,在本例中为&#34; OPEN_ACTIVTY_1&#34;。在该活动中,您可以通过以下方式获取数据:
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
答案 1 :(得分:1)
我知道这个问题已经存在了一段时间,但无论如何我想展示我的解决方案。它比那些简单得多。所以现在我徘徊,如果它的坏习惯。但它有效: 我使用有效负载json对象来存储整数:
JSONObject payload = data.getJSONObject("payload");
int destination = payload.getInt("click_action");
然后我只使用一个简单的switch语句来根据整数结果启动正确的活动:
Intent resultIntent;
switch(destination){
case 1:
resultIntent = new Intent(getApplicationContext(), UserProfileActivity.class);
resultIntent.putExtra("message", message);
break;
default:
resultIntent = new Intent(getApplicationContext(), MainActivity.class);
resultIntent.putExtra("message", message);
break;
}
简单。
答案 2 :(得分:0)
<activity android:name=".design.NotificationActivity"
android:launchMode="singleTask"
android:taskAffinity=""
android:excludeFromRecents="true"
android:parentActivityName=".MainActivity"
>
<intent-filter>
<action android:name="NotificationActivity" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
$ url = "https://fcm.googleapis.com/fcm/send";
$token = "";
$serverKey = '';
$title = "your title";
$body = "mesaage";
$notification = array('title' =>$title ,
'body' => $body,
'sound' => 'default',
'badge' => '1',
'click_action' => 'NotificationActivity'
);
$arrayToSend = array('to' => $token, 'notification' => $notification,'priority'=>'high');
$json = json_encode($arrayToSend);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: key='. $serverKey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
//Send the request
$response = curl_exec($ ch);
//Close request
if ($response === FALSE) {
die('FCM Send Error: ' . curl_error($ch));
}
curl_close($ch)