2

can i intercept notifications when my app is closed?
I need for set badge with this library ShortcutBadger

Thanks.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
alexma01
  • 85
  • 6
  • 2
    We are always glad to help and support new coders but you need to help yourself first. After [doing more research](https://meta.stackoverflow.com/q/261592/1011527), if you have a problem, please post what you've tried with a clear explanation of what isn't working and provide [a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Read the ['How to Ask a good question' guide](http://stackoverflow.com/help/how-to-ask). Also, be sure to [take the tour](http://stackoverflow.com/tour) and read [this](https://meta.stackoverflow.com/q/347937/1011527) – Mathieu de Lorimier Mar 16 '18 at 14:50
  • I apologize if I have not asked the question correctly. I will follow his advice, thank you. – alexma01 Mar 18 '18 at 13:29

3 Answers3

0

There are 3 types of notifications:

notification: Can be send from the web console or any backend, it has predefines values. If the app is open the behaviour is customizable on onMessageRecieve if the app is closed triggers a default notification. data: a key value pair, only Strings. Can be send from any backend. The behaviour is always defined in onMessageReceived method. notification and data: Combination of previous it will have the behaviour of a notification, the data will be available as extras once the notification is clicked in the default launcher activity. Can be send from the web console or any backend.

A push is a json called Payload which contains those objects:

payload: { data: {...} }

Yes, you can send yourself a data type notification it will always do what you write in the onMessageReceived method inside the MessagingService.

This doc should help you https://firebase.google.com/docs/cloud-messaging/concept-options?hl=es-419

If you dont have a server use Functions.

Since the default notification wont be shown, you will probably want to show your own. If you want to also show a notification then the NotificationCompat class must be called from inside onMessageReceived. The visual notification is not related to the push message, in fact, a visual notification can be triggered by pressing a button.

For creating a visual notification, the best approach is to let Android Studio do it for you. Second click on the packages where your activities .java are, new, then selecet ui-component and there is the notification. It will create a basic template of a notification. Then use those methods inside onMessaReceived passing the info that has to be show to the user.

The docs about the class https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html

And you will probably find this error NotificationCompat.Builder deprecated in Android O

cutiko
  • 9,887
  • 3
  • 45
  • 59
  • yes, but if I do not send the payload, the notification is not shown in the notification bar. I know that if I send only the data onMessageReceived it is called but I also need it to be shown notification. – alexma01 Mar 16 '18 at 13:38
  • @alexma01 I have updated the answer with what you need – cutiko Mar 16 '18 at 14:43
  • so I should make sure to change the backend part that sends the notification. I wish I did not have to touch that part. Is there no way to intercept the notification in the FirebaseMessagingService class? Is there no method that I can extend? I can not intercept the intent in any way? – alexma01 Mar 18 '18 at 13:52
  • I don't know what kind of notification your server is sending. I can only guess. If your server is sending notification or notification with data, then that is the problem. The defined behaviour for those when the app is closed is to trigger a default Notification. if you want completely custom behaviour then the server has to send only data push message. You are trying to solve something with code that should be solved by implementing the correct use case. – cutiko Mar 18 '18 at 19:22
0

In case you never solved this, the problem is not how you are implementing it within your app, but how the JSON data payload is being sent. See this question and the respective answers for why you are not receiving the messages while they are in the background.

Very short summary is, if you are receiving the notification payload, it will never trigger in the background. If you receive the data payload without notification, you can parse and perform actions while the app is in the background.

PGMacDesign
  • 6,092
  • 8
  • 41
  • 78
-1

do you mean it?

public class AppFcmMessagingsService extends FirebaseMessagingService {

private static final String TAG = "FirebaseMessageService";

@Override
public void onCreate() {
    super.onCreate();
}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    try {
        if(remoteMessage.getData().size() > 0) {

            final JSONObject jsonObject = new JSONObject(remoteMessage.getData().toString());
            Log.d(TAG,"remoteMessage = " + jsonObject.toString());
            int badgeCount = 1;
            ShortcutBadger.applyCount(getApplicationContext(), badgeCount); 
        }
    } catch (Exception e) {
        Log.e(TAG, "onMessageReceived: ", e);
    }
    if(remoteMessage.getNotification() != null) {
            int badgeCount = 1;
            ShortcutBadger.applyCount(getApplicationContext(), badgeCount); 
             Log.d(TAG, "notification body : " + remoteMessage.getNotification().getBody());
    }
}
}
KMI
  • 258
  • 1
  • 10
  • 1
    is it called even if the app is closed? Obviously I want to send both the payload and the data in the notification. – alexma01 Mar 16 '18 at 13:42
  • Yes. you can send json or key value map from your server – KMI Mar 16 '18 at 13:46
  • Thanks for the reply. I tried but onMessageReceived is not called when the app is closed unless I send the payload. – alexma01 Mar 18 '18 at 13:49
  • yes you have to send payload otherwise onMessagereceived will not called – KMI Mar 18 '18 at 13:56
  • If you send notification android show notification in statusbar with your title and body as Notification – KMI Mar 18 '18 at 13:58
  • 1
    if i send {"notification":{ "title":"bbbbbbbb", "body":"aaaaaa", }, "data":"gggg" } onMessageReceived not called when app is killed – alexma01 Mar 18 '18 at 14:22
  • how do you close application? – KMI Mar 18 '18 at 14:39
  • not 100% guarantee that all your messages will be delivered. You can add priority to your messages. About priority you can read fcm docs https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages – KMI Mar 18 '18 at 14:46