I have implemented firebase admin SDK in NodeJS and trying to send push notification from server.My problem is I am not getting data part in my notification(account and balance).It is only showing notification part(title and body).
I have implemented server side code something like this:
const admin = require("firebase-admin");
const serviceAccount = require("./my_service_account.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://<myapp name>.firebaseio.com"
});
var payload = {
notification: {
title: "Hello",
body: "How are you."
},
data: {
account: "Savings",
balance: "$3020.25"
}
};
admin.messaging().sendToDevice(registrationToken,payload)
.then((response) =>{
console.log("Response", response);
}).catch((error) => {
console.log("Error", error);
});
On client side I am doing like this below:
public class MessagingReceive extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
if(remoteMessage.getData().size() > 0){
Map<String, String> data = remoteMessage.getData();
handleData(data);
}
}
private void handleData(Map<String,String> data){
String title = data.get("account");
String body = data.get("balance");
Intent intent = new Intent(MessagingReceive.this,MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setContentTitle(title);
notificationBuilder.setContentText(body);
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,notificationBuilder.build());
}
}
Someone let me know how can I get data part in my notification. Any help would be appreciated.
THANKS