0

I'm using Android Studio and Firebase. I want to send to a specific user a notification when other users sends him messages, and I want his phone to be able to get these notifications, even when his app is not running at all.

I've tried to use some guilds by firebase themselves, youtube tutorials and google. But I failed. Can someone please help me with this?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Dor Dahan
  • 391
  • 2
  • 5
  • 10
  • See my answer [here](https://stackoverflow.com/a/39505298/4625829) – AL. Jul 01 '17 at 01:50
  • @AL. I've read all your linked comments inside that^ link. Thank you. Please tell me if I'm miss understanding this- what I figured from your comments is that it's not possible to send notification to a specific phone while the app in that phone is closed, even tho the FirebaseMessagingService is always running in the background with Google's service? I don't understand how it is possible that in our time, there is no solution for this annoying problem. Btw I thought about your theory about whitelisting WhatsApp- how did they handle this issue in the early days as a new and unknown company? – Dor Dahan Jul 01 '17 at 03:11

1 Answers1

2

FCM have two kinds of messages: downstream and notification. Each of this types behave differently depending on the app state.

1. Notification

Notification will be received by device only if app is in background. It'll be displayed or in notifications tray, or as Heads Up notification. You can't receive Notification kind of message when your app is in foreground.

2. Downstream

Downstream message will be received by your app in any state. You'll receive it in your FirebaseMessagingService implementation (onMessageReceived).

3. Both worlds

If you combine both, 1 and 2, you'll get downstream-notification message. So it'll take behavior from both "worlds". You'll receive Notification message if your app is in background and Downstream if your app is in foreground. Also if you'll click on notification where click_action is specified and you have Intent Filter for this action - it'll lead you to that Activity and call onNewIntent or onCreate depending on backstack.

So, answering your question - if you want to always receive new message notification - send only Downstream message. Even if app is killed it'll receive it or when app will become alive (if messaging service was enable to restart), or right away. But there is one drawback in this way: you'll need to show notification (HeadsUp or regular) to user by yourself.

Update

Here is common Firebase message body:

 {
    "to" : "FIREBASE_TOKEN",
    "notification" : {  // Responsible for Notification kind of messages
      "body" : "great match!",
      "title" : "Portugal vs. Denmark",
      "icon" : "myicon"
    },
    "data" : { // Responsible for Downstream kind of messages
      "Nick" : "Mario",
      "Room" : "PortugalVSDenmark"
    }
  }

So what do you need - it's in some way (as json, or so) send this data to specified firebase url with some headers. Here is link with details how to send messages. Note that there is info how to send message top multiple devices/groups and you need to specify only your partner FirebaseToken in to: field.

Ekalips
  • 1,473
  • 11
  • 20
  • Thanks ! what code I actually need to use in order to send the notification? – Dor Dahan Jun 30 '17 at 14:47
  • Thanks again. Now im reciving notification only when the app is running in the backgroud. But I want my users to get notifications even when the app is closed. Like WhatsApp for example. Is that possible using Android Studio and Firebase? – Dor Dahan Jun 30 '17 at 15:21
  • @DorDavid Yes it is, but that easy. You need to manually show notifications when your app is in foreground (with NotificationManager). But for real - showing HeadsUp notifications when user is in app - bad practice. Try to implement SnackBar as your in-app notifications. –  Ekalips Jun 30 '17 at 15:23
  • I didn't understand that sentence: "Try to implement SnackBar as your in-app notifications." Can you explain it a bit more? – Dor Dahan Jun 30 '17 at 16:10
  • @DorDavid Good practice will be showing to user a SnackBar about message. It can have text like this "New message: %message%" and "View" as action. And when you'll receive firebase message when user in app show him this SnackBar and not HeadsUp notification, it's much more pleasant. –  Ekalips Jun 30 '17 at 16:18
  • Ok. "You need to manually show notifications when your app is in foreground (with NotificationManager)." Sorry for a lot of questions.. but how do I do that? – Dor Dahan Jun 30 '17 at 16:21
  • @DorDavid You need to register a service that extends `FirebaseMessagingService`, it'll receive messages when app is in foreground (this is described in firebase tutorial). Then when this service receives message - `onMessageReceived` will be called. You need to find way to parse received message and then notify user in some way. You can simply show notification with notification manager (guide : https://developer.android.com/training/notify-user/build-notification.html), or do stuff with `SnackBar` –  Ekalips Jun 30 '17 at 16:27
  • For showing `SnackBar` you need some root View. So you need to pass info from your `Service` to current active `Activity`. This can be done with `LocalBroadcastManager` (guide : https://stackoverflow.com/questions/8802157/how-to-use-localbroadcastmanager). Or with `EventBus` from `Greenrobot` https://github.com/greenrobot/EventBus. I personally recommend second one. –  Ekalips Jun 30 '17 at 16:29
  • "You need to register a service that extends FirebaseMessagingService" I've managed to do so with that code: [link](https://github.com/firebase/quickstart-android/blob/master/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/MyFirebaseMessagingService.java#L45-L82) I've built a notification builder, but what I don't understand is what code should I add to the builder, in order for the service to show the user a notification when the app is closed. That's all. – Dor Dahan Jun 30 '17 at 16:43
  • @DorDavid No, you can't show notification to user when app will be closed. At least in easy way. –  Ekalips Jun 30 '17 at 16:44
  • I see. Facebook, WhatsApp and almost any game apps are doing this- which means it's possible. What is the not easy way? – Dor Dahan Jun 30 '17 at 16:46
  • And actually almost every app on the market, small or big, does that too. – Dor Dahan Jun 30 '17 at 16:52
  • No, they don't wait until app goes background. They notify user somehow right away, or by notifications (NotificationManager), or with Toasts, SnackBars or so –  Ekalips Jun 30 '17 at 20:43
  • Notifying the user right away that's exactlly what I was looking for all along here. I'm sorry if I didn't explain my self clearly enough.. what code should I write in order to do that? or what specific change should I make in my NotificationManager in order to do that? – Dor Dahan Jul 01 '17 at 01:38
  • @DorDavid I already shared with you link about notifying with NotificationManager. Here it's again https://developer.android.com/training/notify-user/build-notification.html –  Ekalips Jul 03 '17 at 08:26