public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService{
String TAG = "FirebaseMessagingService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage){
Log.i(TAG," input string :"+ String.valueOf(remoteMessage.getData()));
/**
* Parse remote input and pass id, action, frequency and payload to eventHandler.
* **/
String messageType = remoteMessage.getMessageType();// messageType is null always
if(messageType.equalsIgnoreCase("Data")){
Log.i(TAG, " Data notification received");
}
else if(messageType.equalsIgnoreCase("Notification")){
Log.i(TAG, " Push notification received");
}
}
我不知道什么getMessageType()
应该返回。但它似乎不太可能,它返回要么Data
或Notification
,因为a single message can contain both notification and data information。
谢谢。其实,我想出了一个办法来检查的消息类型本身。以下为我工作,而无需使用的getMessage类型。
地图数据= remoteMessage.getData(); RemoteMessage.Notification通知= remoteMessage.getNotification();
if (data.isEmpty()) { // message type is notification.
parseDataMessage(remoteMessage);
} else { // message type is data.
parseNotificationMessage(remoteMessage);
}
从文档看起来它是你可以发送消息和使用SDK here时设置的参数。
public RemoteMessage.Builder setMessageType (String messageType)
然而,发送使用火力地堡REST API消息时,我还没有看到指定消息类型的能力。
你应该尝试像这样...
if(getIntent().getExtras() != null && getIntent().getExtras().get("your_data_key") != null) {
String strNotificaiton = getIntent().getExtras().get("your_data_key").toString(); }
使用此代码onMessageReceived
它为我工作:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.e(TAG, "From: " + remoteMessage.getFrom());
if (remoteMessage == null)
return;
//if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody());
handleNotification(remoteMessage.getNotification().getBody());
}
//if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
try {
JSONObject json = new JSONObject(remoteMessage.getData().toString());
handleDataMessage(json);
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
}