【发布时间】:2018-10-02 18:42:47
【问题描述】:
我有一个 Cordova/Angular 应用程序,它使用 firebase 通过 cordova-plugin-fcm 插件进行推送通知。当应用程序关闭/在后台时,通知会正确显示在栏中,但是当通过点击栏通知(或已经打开)打开应用程序时,通知本身只是一个包含 JSON 警报对象的警报框,而不是格式化的通知。
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FCMPlugin";
String message = "";
String title = "";
/**
* Called when message is received.
*
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
*/
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// If the application is in the foreground handle both data and notification messages here.
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
Log.d(TAG, "==> MyFirebaseMessagingService onMessageReceived");
if (remoteMessage.getData().size() > 0) {
message = getDataWithKey(remoteMessage.getData(), "message");
title = getDataWithKey(remoteMessage.getData(), "title");
}
if( remoteMessage.getNotification() != null){
Log.d(TAG, "\tNotification Title: " + remoteMessage.getNotification().getTitle());
Log.d(TAG, "\tNotification Message: " + remoteMessage.getNotification().getBody());
};
Map<String, Object> data = new HashMap<String, Object>();
data.put("wasTapped", false);
for (String key : remoteMessage.getData().keySet()) {
Object value = remoteMessage.getData().get(key);
Log.d(TAG, "\tKey: " + key + " Value: " + value);
data.put(key, value);
};
Log.d(TAG, "\tNotification Data: " + data.toString());
FCMPlugin.sendPushPayload( data );
sendNotification(title, message, data);
}
// [END receive_message]
private String getDataWithKey(Map<String, String> params, String fieldKey) {
String data = "";
try {
for (Map.Entry<String, String> param : params.entrySet()) {
String key = param.getKey();
String value = param.getValue();
if(key.contentEquals(fieldKey)){
if(!value.isEmpty()) {
data = value;
}
}
}
}
catch (Exception ex){
Log.e(TAG, " getDataWithKey -- " + ex.getMessage());
}
return data;
}
/**
* Create and show a simple notification containing the received FCM message.
*
* @param messageBody FCM message body received.
*/
private void sendNotification(String title, String messageBody, Map<String, Object> data) {
Intent intent = new Intent(this, FCMPluginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
for (String key : data.keySet()) {
intent.putExtra(key, data.get(key).toString());
}
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(getApplicationInfo().icon)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
根据一切,我可以发现我已经正确实现了插件,但是我的通知是丑陋的 AF,因为一个充满 JSON 的警报框是人类不可读的。
这是通过 REST API 发送的 JSON 对象:
{
"to":"/topics/all",
"priority":"high",
"notification":{
"title":"App in Foreground Test",
"body":"Test App in Foreground",
"sound":"default",
"click_action":"FCM_PLUGIN_ACTIVITY",
"icon":"fcm_push_icon"
},
"data":{
"title":"App in Foreground Test",
"message":"Test with app in Foreground",
"param1":"value1",
"param2":"value2"
}
}
同时发送通知和数据负载,以便我在应用处于后台和前台时收到通知。
public static void sendPushPayload(Map<String, Object> payload) {
Log.d(TAG, "==> FCMPlugin sendPushPayload");
Log.d(TAG, "\tnotificationCallBackReady: " + notificationCallBackReady);
Log.d(TAG, "\tgWebView: " + gWebView);
try {
JSONObject jo = new JSONObject();
for (String key : payload.keySet()) {
jo.put(key, payload.get(key));
Log.d(TAG, "\tpayload: " + key + " => " + payload.get(key));
}
String callBack = "javascript:" + notificationCallBack + "(" + jo.toString() + ")";
if(notificationCallBackReady && gWebView != null){
Log.d(TAG, "\tSent PUSH to view: " + callBack);
gWebView.sendJavascript(callBack);
}else {
Log.d(TAG, "\tView not ready. SAVED NOTIFICATION: " + callBack);
lastPush = payload;
}
} catch (Exception e) {
Log.d(TAG, "\tERROR sendPushToView. SAVED NOTIFICATION: " + e.getMessage());
lastPush = payload;
}
}
【问题讨论】:
-
Jon:: 当然这不可能是您正在使用的代码,因为它甚至不应该编译。没有声明变量“消息”或“标题”。您还更改了
getDataWithKey(),使其返回与方法返回类型相矛盾的Map。 -
对不起,错误的代码,我已经修改了一些代码以尝试将对象(数据)传递给请求
private void sendNotification(String title, String messageBody, Map<String, Object> data) {...的sendNotification方法 -
@Barns 进行澄清,我希望警报具有标题(在前台使用应用程序测试)而不是 ALERT,并且消息当然应该是文本的“正文”部分它目前拥有的 JSON ......(我删除了其他 2 个参数,因为它们是不需要的。
-
@Barns:: 在原始帖子中添加了 sendPushPayload 的代码
-
我应该早点问的,但是您是在真实设备还是模拟器上进行测试?您的测试设备是什么 API 级别?以及问题顶部的图像。你说的“通知”是“丑陋的json”吗?
标签: android firebase push-notification angular6 cordova-plugin-fcm