I am struggling with FCM notifications on Android. The issue is only when the app is closed and not running. I am trying to achieve no click activity I don't want the app to open and I do not want the message to disappear. It works perfectly when the app is open and running. When the app is not open I still get the notification but it is not in multi-line so I can only see the beginning of the notification. Then clicking on the notification makes it disappear. I have spent hours trying to find a solution that works. Here is the code i have so far:
private void sendNotification(String messageBody) { //Intent intent = new Intent(this, MainActivity.class); // intent.setFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION); //PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, // PendingIntent.FLAG_UPDATE_CURRENT); Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("My Car Wash App") .setAutoCancel(true) .setSound(defaultSoundUri) .setContentText(messageBody) .setDefaults(NotificationCompat.DEFAULT_ALL) .setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody)) .setPriority(NotificationCompat.PRIORITY_MAX); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0, notificationBuilder.build()); }
Any help or suggestions would be greatly appreciated.
EDIT *******
The problem is I asked the question poorly after more research I now realize the code above is only being called when my app is open.
@Override public void onMessageReceived(RemoteMessage remoteMessage) { //Displaying data in log //It is optional Log.i("myStuff", "From: " + remoteMessage.getFrom()); Log.i("myStuff", "Notification Message Body: " + remoteMessage.getNotification().getBody()); //Calling method to generate notification sendNotification(remoteMessage.getNotification().getBody()); }
onMessageReceived is not called when the app is not in the foreground that is why the notifications are not showing multi-line unless the app is running. There is a lot of information posted on this subject but I still can not seem to figure it out.
Here is my c# server side code *
var data = new { to = deviceId, notification = new { body = msg, title = "My Car Wash", icon = "myicon" }, priority = 10 };
Notifications work perfect on iOS and perfect on Android when the app is running. I just can not get the message to show in multi-line correctly and I am not sure what to do.
*