4

In the Firebase docs, it is safe to subscribe to a topic when your app is opened like in the MainActivity's onCreate.

MainActivity.java

FirebaseMessaging.getInstance().subscribeToTopic("announcement");

Most likely you'll receive your notification when the application is in background(Pressed the Home Button)/ Foreground.

Problem is when I closed the application (app is destroyed) or I've rebooted my phone, my app doesn't seem to receive the notification from Firebase Console(send via Topic). Though I think I need to implement a broadcast receiver when phone reboots.

Anyway I'm subscribed to the Topic from onCreate of my extended FirebaseMessagingService but it always calls onDestroy though. From debugging it seems that when a notification is received from a FirebaseMessagingService it goes to onCreate -> onMessage -> onDestroy for EACH notification.

As I understand my extended FirebaseMessagingService should live even if the app is destroyed so it shouldn't call onCreate every time.

Since a service should work even if the app is destroyed

So I'm wondering where should I placed my subscription to topic in my Android Code such that even if the app is destroyed it will still receive notifications like announcements.

Thank you in advance.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
bumblebeen
  • 642
  • 8
  • 21
  • Possible duplicate of [Android: Subscribe to Firebase Cloud Messaging(FCM) Topic](http://stackoverflow.com/questions/40055051/android-subscribe-to-firebase-cloud-messagingfcm-topic) – AL. Feb 23 '17 at 12:59
  • Also see the *Linked* posts (mid-right side) of the link above. – AL. Feb 23 '17 at 13:00
  • Thanks for the info but actually I already read that link before asking the question. Yes it is fine to subscribe on every app startup but the problem is im not subscribed anymore when app is killed even if i didnt call unsubscribeToTopic. So question is where should I put my subscription such that I will still receive announcements even if app is killed. Because Im assuming FirebaseMessagingService should take care of that. – bumblebeen Feb 23 '17 at 15:47
  • "*the problem is im not subscribed anymore when app is killed even if i didnt call unsubscribeToTopic*" doesn't really make sense. You remain subscribed until you call `unsubscribeToTopic()` (see Diego's answer below). There must be some other reason why you're not receiving any messages when your app is killed. – AL. Feb 24 '17 at 01:54

2 Answers2

3

FirebaseMessagingService doesn't need to be active for your app to receive the notifications.

The service is executed only to process incoming messages or token requests.

Don't worry if the service is stopped. Android will restart it when a new notification arrives.


You can subscribe to topics in any place of your app. Subscriptions are stored until you unsubscribe.

Diego Giorgini
  • 12,489
  • 1
  • 47
  • 50
  • Okay so the behavior explains the restart cycle of the service. It seems that I can receive topics now even if app is destroyed. But for some devices only. I'm not sure what is causing some devices to not receive the announcement. Thanks – bumblebeen Feb 24 '17 at 06:40
  • a subset of devices implement non-standard battery-saving techniques that break many Android API (FCM included). You might be affected by this. If that is the case you should inform the manufacturer that [device & build version] contains bugs in regards of push notifications. – Diego Giorgini Feb 24 '17 at 15:26
0

Use a STIKY service.

Override this method:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return START_STICKY;
}

This should make your service restart after app is destroied. Alse subscribe in this method you topic because this method is called every time after destroy.

Cătălin Florescu
  • 5,012
  • 1
  • 25
  • 36
  • So I should create another Service? since when I extend FirebaseMessagingService I cannot override that method "cannot override onStartCommand (Intent, int, int) since overriden method is final" – bumblebeen Feb 23 '17 at 11:43
  • Try making your own service and add a value event listener to `announcement` topic. – Cătălin Florescu Feb 23 '17 at 11:45