Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FirebaseMessagingService in not running

i am working on android app which receive FCM (fire base messaging clouding), when the app is in the background or it is off android system recieve the notification and display it correctly, but the notification is not displayed when the app become in the foreground, i have checked the service which extends FirebaseMessagingService, it is not running and i dont know how to make it run.

the service code:

public class MyService extends FirebaseMessagingService {
    public MyService() {
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
    }
}

the manifest code:

<service android:name=".MyService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

the main activity code:

startService(new Intent(this,MyService.class));
like image 919
omar shady Avatar asked Oct 15 '22 10:10

omar shady


1 Answers

Firebase Cloud Messaging offers different message types to use. These are:

  • Notification message. It's automatically displayed to user as a push notification both in foreground and background states.
  • Data message. This message is not about showing pushes, but about delivering information from server to mobile apps. You can handle any data notification using custom FirebaseMessagingService. If you handle a data message, you're still able to manually show a notification
  • Notification message with optional data payload. Kinda combination of the 1st and the 2nd types. While app is in background, api will automatically show the notification. If you're running foreground, you're able to handle this message using your service.

Now more specifically

The type of the message depends on what's inside of the object of your message. you should care about notification and data fields inside. If you specify the notification object (it's a title and a body of your push notification) and don't include the data object - then your message type is notification If you do vice versa, specify only data object, then the type is data And combining both types leads to the last type

So if you you want to simply show some information using push notification, consider using the notification type.

You can find more information on the official documentation page

like image 58
Andriy Shpek Avatar answered Oct 31 '22 12:10

Andriy Shpek