So, I am trying to get notification when app is in background or foreground and trying to send data payload from firebase console to android app activity, but I am getting this
Please tell me where is the problem and how it will be solved.
private static final String TAG = "FirebaseMessagingServic";
public FirebaseMessagingService() {
}
String value = "0";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
value = remoteMessage.getData().get("str"); //key text in quotes
}
String title = remoteMessage.getNotification().getTitle(); //get title
String message = remoteMessage.getNotification().getBody(); //get message
sendNotification(title, message, value);
// Check if message contains a notification payload.
/* if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Title: " + title);
Log.d(TAG, "Message Notification Body: " + message);
}*/
}
@Override
public void onDeletedMessages() {
}
private void sendNotification(String title,String messageBody, String value) {
Intent intent = new Intent(this, CS101noti.class);
SharedPreferences prefs = getSharedPreferences("MyApp", MODE_PRIVATE);
prefs.edit().putString("body", value).apply();
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.mipmap.ic_launcher)
.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());
}
Well I have solved it :)
You just need to send data payload from REST API or Postman, and get data values in Java code, it will work like Charm :)