当用户在后台点击通知时,我正在尝试打开特定活动。从Docs中,我已经知道必须在有效负载中添加click_action,并在App中使用intent过滤来处理它。但是,如何通过Firebase控制台在Firebase通知中添加click_action?我也对任何其他工作开放。提前致谢。
如果您的应用处于后台,Firebase将不会触发onMessageReceived()。为什么.....?我不知道。在这种情况下,我认为实施FirebaseMessagingService没有任何意义。
根据文档,如果您要处理后台消息到达,您必须发送带有消息的“click_action”。但是,如果您仅通过Firebase API从Firebase控制台发送消息,则无法进行此操作。这意味着您必须构建自己的“控制台”才能使营销人员使用它。所以,这使得Firebase控制台也毫无用处!
这个新工具背后有非常好的,有希望的想法,但执行得很糟糕。
我想我们将不得不等待新版本和改进/修复!
据我所知,此时无法在控制台中设置click_action。
虽然不是如何在控制台中设置click_action的严格答案,但您可以使用curl作为替代:
curl --header "Authorization: key=<YOUR_KEY_GOES_HERE>" --header Content-Type:"application/json" https://fcm.googleapis.com/fcm/send -d "{\"to\":\"/topics/news\",\"notification\": {\"title\": \"Click Action Message\",\"text\": \"Sample message\",\"click_action\":\"OPEN_ACTIVITY_1\"}}"
这是测试click_action映射的简便方法。它需要像FCM文档中指定的那样的intent过滤器:
<intent-filter>
<action android:name="OPEN_ACTIVITY_1" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
这也利用主题来设置受众。为了使其工作,您需要订阅一个名为“新闻”的主题。
FirebaseMessaging.getInstance().subscribeToTopic("news");
即使在控制台中查看新创建的主题需要几个小时,您仍然可以通过FCM apis向其发送消息。
此外,请记住,这只适用于应用程序在后台的情况。如果它在前台,您将需要实现FirebaseMessagingService的扩展。在onMessageReceived方法中,您需要手动导航到click_action目标:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//This will give you the topic string from curl request (/topics/news)
Log.d(TAG, "From: " + remoteMessage.getFrom());
//This will give you the Text property in the curl request(Sample Message):
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
//This is where you get your click_action
Log.d(TAG, "Notification Click Action: " + remoteMessage.getNotification().getClickAction());
//put code here to navigate based on click_action
}
正如我所说,目前我找不到通过控制台访问通知有效负载属性的方法,但我认为这种方法可能会有所帮助。
您可以在扩展FirebaseMessagingService的服务中的onMessageReceived()中处理消息功能的所有操作。为此,您必须使用例如Advanced REST client in Chrome发送仅包含数据的消息。然后使用“原始标题”向https://fcm.googleapis.com/fcm/send发送POST:
Content-Type:application / json授权:key = YOUR_PERSONAL_FIREBASE_WEB_API_KEY
和“原始有效载荷”字段中的json消息。
警告,如果你的json中有“通知”字段,那么当onMessageReceived()中的应用程序在后台时,即使有数据字段,也不会收到你的消息!例如,这样做,消息就像前台应用程序一样工作:
{
"condition": " 'Symulti' in topics || 'SymultiLite' in topics",
"priority" : "normal",
"time_to_live" : 0,
"notification" : {
"body" : "new Symulti update !",
"title" : "new Symulti update !",
"icon" : "ic_notif_symulti"
},
"data" : {
"id" : 1,
"text" : "new Symulti update !"
}
}
要在onMessageReceived()的所有情况下接收您的消息,只需从您的json中删除“通知”字段!
例:
{
"condition": " 'Symulti' in topics || 'SymultiLite' in topics",
"priority" : "normal",
"time_to_live" : 0,,
"data" : {
"id" : 1,
"text" : "new Symulti update !",
"link" : "href://www.symulti.com"
}
}
并在您的FirebaseMessagingService中:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String message = "";
obj = remoteMessage.getData().get("text");
if (obj != null) {
try {
message = obj.toString();
} catch (Exception e) {
message = "";
e.printStackTrace();
}
}
String link = "";
obj = remoteMessage.getData().get("link");
if (obj != null) {
try {
link = (String) obj;
} catch (Exception e) {
link = "";
e.printStackTrace();
}
}
Intent intent;
PendingIntent pendingIntent;
if (link.equals("")) { // Simply run your activity
intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
} else { // open a link
String url = "";
if (!link.equals("")) {
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(link));
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
}
pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = null;
try {
notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notif_symulti) // don't need to pass icon with your message if it's already in your app !
.setContentTitle(URLDecoder.decode(getString(R.string.app_name), "UTF-8"))
.setContentText(URLDecoder.decode(message, "UTF-8"))
.setAutoCancel(true)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(pendingIntent);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (notificationBuilder != null) {
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(id, notificationBuilder.build());
} else {
Log.d(TAG, "error NotificationManager");
}
}
}
}
请享用 !
这属于变通方法类别,也包含一些额外信息:
由于通知的处理方式不同,具体取决于应用程序的状态(前景/后台/未启动),我已经看到了实现辅助类的最佳方法,其中根据通知消息中发送的自定义数据启动所选活动。
这样您就不需要特定于它的click_action或intent过滤器。您也只需编写一次代码,就可以轻松地开始任何活动。
所以最小的自定义数据看起来像这样:
Key: run_activity
Value: com.mypackage.myactivity
以及处理它的代码:
if (intent.hasExtra("run_activity")) {
handleFirebaseNotificationIntent(intent);
}
private void handleFirebaseNotificationIntent(Intent intent){
String className = intent.getStringExtra("run_activity");
startSelectedActivity(className, intent.getExtras());
}
private void startSelectedActivity(String className, Bundle extras){
Class cls;
try {
cls = Class.forName(className);
}catch(ClassNotFoundException e){
...
}
Intent i = new Intent(context, cls);
if (i != null) {
i.putExtras(extras);
this.startActivity(i);
}
}
这是最后两种情况的代码,startSelectedActivity也将从onMessageReceived(第一种情况)调用。
限制是intent附加内容中的所有数据都是字符串,因此您可能需要在活动本身中以某种方式处理它。此外,这是简化的,您可能无法在不警告用户的情况下更改前台应用上的活动/视图。
从firebase文档中可以清楚地看出,当应用程序处于后台时,你的onMessageReceived
将无效。
当您的应用处于后台并点击通知时,您的默认启动器将会启动。要启动所需的活动,您需要在通知有效负载中指定click_action
。
$noti = array
(
'icon' => 'new',
'title' => 'title',
'body' => 'new msg',
'click_action' => 'your activity name comes here'
);
并在您的android.manifest
文件中
在您注册活动的位置添加以下代码
<activity
android:name="your activity name">
<intent-filter>
<action android:name="your activity name" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
如果您的应用处于后台,Firebase将不会触发onMessageReceived()。当app在前台时调用onMessageReceived()。当app在后台时,只有当https://fcm.googleapis.com/fcm/send的主体仅包含数据有效负载时才会调用onMessageReceived()方法。在这里,我刚刚创建了一个方法来构建自定义通知,意图具有您所需的活动。并在onMessageRecevied()中调用此方法。
在PostMan中:
:https://fcm.googleapis.com/fcm/send
header:授权:key = ur key
身体--- >>
{ "data" : {
"Nick" : "Mario",
"Room" : "PoSDenmark",
},
"to" : "xxxxxxxxx"
}
在你的申请中。
class MyFirebaseMessagingService extends FirebaseMessagingService {
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData().size() > 0) {
sendNotification("ur message body") ;
}
}
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, Main2Activity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_ic_notification)
.setContentTitle("FCM Message")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
当数据有效负载进入移动设备时,将调用onMessageReceived()方法..在该方法中,我只是做了一个自定义通知。即使你的应用程序是背景或前景,这将工作。
更新:
因此,只是为了验证,目前无法通过Firebase控制台设置click_action
参数。
所以我一直试图在Firebase通知控制台中这样做,没有运气。由于我似乎找不到任何地方将click_action
值放在控制台中,我主要测试的是在通知(高级选项>自定义数据)中添加自定义键/值对:
Key: click_action
Value: <your_preferred_value>
然后尝试在RemoteMessage.getNotification().getClickAction()中调用onMessageReceived()
来查看它是否正在检索正确的值,但它总是返回null
。接下来我尝试调用RemoteMessage.getData().get(< specified_key >)并能够检索我添加的值。
注意:我不完全确定是否可以将其用作解决方法,或者是否违反最佳做法。我建议使用您自己的应用服务器,但您的帖子特定于Firebase控制台。
客户端应用程序和通知的行为方式仍然取决于您对其进行编程的方式。话虽如此,我认为您可以使用上面的解决方法,使用从getData()
检索到的值,然后通过Notification调用this或that。希望这会有所帮助。干杯! :d
现在可以在Firebase控制台中设置click_action。您只需转到通知 - 发送消息高级选项,您将有两个字段用于键和值。在第一个字段中,您放置click_action,然后在第二个字段中放置一些表示该操作值的文本。然后在你的Manifest中添加intent-filter并给他与你在控制台中写的相同的值。这就是模拟真正的click_action。
我正在看这个Firebase,从谷歌云消息推送通知基础开发,所以我们可以在firebase中使用gcm教程,功能和实现,我使用gcm推送通知函数的功能来解决这个click_action问题我使用gcm函数**
'notificationclick'
**在变量url中尝试这个保存url click_action,这是在server-worker.js中
var url = "";
messaging.setBackgroundMessageHandler(function(payload) {
console.log('[firebase-messaging-sw.js] Received background message ', payload);
// Customize notification here
url = payload.data.click_action;
const notificationTitle = payload.data.title;
const notificationOptions = {
body: payload.data.body ,
icon: 'firebase-logo.png'
};
return self.registration.showNotification(notificationTitle,
notificationOptions);
});
//i got this in google cloud messaging push notification
self.addEventListener('notificationclick', function (event) {
event.notification.close();
var clickResponsePromise = Promise.resolve();
clickResponsePromise = clients.openWindow(url);
event.waitUntil(Promise.all([clickResponsePromise, self.analytics.trackEvent('notification-click')]));
});