1

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.

*

Paul_D
  • 255
  • 2
  • 8
  • 21
  • Since you're setting `BigTextStyle()` to notification, it will be shown in multilines. If text does not appear in multiple lines you can expand notification by two fingers. And you have not set any action to your notification so when you click it disappears. – Apurva Sep 04 '16 at 15:58
  • I have edited my question. I appreciate the suggestion but the problem is the code BigTextStyle() is not getting called unless my app is in the foreground. – Paul_D Sep 05 '16 at 02:31
  • From your question its seems like you are using notification payload. you have to use data payload instead of notification payload because FCM's design is to automatically use its default notification method when you are using notification payload which will ignore your extra function once in background. Strange but yeah they intended to do so. try using data payload function and check the result. https://firebase.google.com/docs/cloud-messaging/concept-options check this link for how to send data payload – JamesYTL Sep 05 '16 at 02:40
  • Thanks to everyone for the input. Really helpful, I do appreciate it. JamesYTL you hit the nail on the head. I now pass a notification and data payload. – Paul_D Sep 05 '16 at 12:07

4 Answers4

1

The notification is getting disappeared when you click on it because you did not add any action when onClick is performed.

To give onClick functionality on notification, you have to use PendingIntent with the target activity. You can also add additional data into it if needed.

Before initializing notificationManager, add this code:

Intent openIntent = new Intent(mContext, TargetActivity.class);
// Falg to clear the stack. 
openIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0,
        openIntent, 0);
notificationBuilder.setContentIntent(pendingIntent);
Faraz
  • 2,144
  • 1
  • 18
  • 28
1

FCM displays my notification the way it wants to when the app is closed. That's ok with me. I just want to make sure the entire notification gets read and not lost. Here is my solution... When an app user clicks on the notification it opens the app and displays the notification in an alert Dialog.

c# server-side code to send the message

 var msg2send = new
            {
                to = deviceId,
                priority = 10,
                notification = new
                {
                    body = msg,
                    title = "Red Hill Car Wash",
                    icon = "myicon"
                },
                data = new 
                {
                    notice = msg
                }
            };

And in the onCreate of MainActivity

String notice = "";
try {
         notice = getIntent().getStringExtra("notice");
        if (notice != null)
        {
            Log.i("myStuff", notice);
            ///// DISPLAY NOTIFICATION IN AN ALERT DIAGLOG all good!
        }
    }
    catch (Exception ex) {
        Log.i("myStuff", ex.getMessage());
    }

FCM passes the message to my MainActivity, if a message exists then I grab it and can display it how ever I want. Thank for the comments and input to help solve this issue.

Paul_D
  • 255
  • 2
  • 8
  • 21
1

One way is to use data instead of notification to get onMessageReceived() callback both in app foreground or background. The json part is:

{   
    "data": {
      "title": "notification_title",
      "body": "notification_body"
    },
    "to" : "jh578_gsh....jhHGFJ76FH"
}

For detail, follow this link: https://stackoverflow.com/a/37845174/5829624

Edit:

Method 2:

Use a click_action payload in your notification payload. In json:

{   
    "notification": {
      "click_action": "OPEN_ACTIVITY_1",
    },
}

And add this intent filter in the Activity (which you want to load when you tap on the notification) in your Manifest:

<intent-filter>
  <action android:name="OPEN_ACTIVITY_1" />
  <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

Hope this one will solve your problem.

Community
  • 1
  • 1
Sudip Podder
  • 830
  • 11
  • 25
  • This is great for android but if I don't use priority = 10, notification = new then IOS devices do not get the push notification. For me it is critical that Android and iOS app users both get the notifications when the app is in the foreground and background. – Paul_D Sep 06 '16 at 10:53
1

When application in background "OnMessageReceived" overridden method wont get called in general scenario.

If you want to make "OnMessageReceived" method to call all the time. Ask your web service team to send only "DATA" payload. Dont use "Notify" or "Notification" payload.

If "DATA" payload is send properly from backend then Android application will work on "OnMessageReceived" with a List of Data from RemoteMessage.getDATA();

I struggled the same issue for nearly 2 days and finally got the above solution.

Raghul Sugathan
  • 350
  • 1
  • 7
  • 22