Search code examples
androidfirebasefirebase-cloud-messagingandroid-servicecapacitor

FirebaseMessagingService fails to start / be called in conjunction with Capacitor


So I'm trying to build an app using CapacitorJS and Ionic. In case you haven't hear of it, Capacitor is a JS runtime, providing native app capabilities to JavaScript apps. Capacitor provides a PushNotification plugin, but it doesn't handle data-only notifications when the app is closed like you'd normally do (Capacitor would have to call JavaScript, which it can't, that is okay). So the documentation states:

This plugin does support data-only notifications, but will NOT call pushNotificationReceived if the app has been killed. To handle this scenario, you will need to create a service that extends FirebaseMessagingService.

So I tried to be a good citizen and do what I'm told, but the issue is, that my Service doesn't get called. It's always the Capacitor-provided service that gets a call, while mine stays lonely and unloved. I have tried adding my own code to the Capacitor service and indeed, it does run. However, I can't post local notifications, because the plugin is a different package.

So I'm in a bit of a pickle here. On the one hand, I need the Capacitor plugin to provide calls to JavaScript when the app is open to responsively update my UI with new messages. On the other hand, I need a way to handle messages when the app isn't open and Capacitor's service seems to be blocking mine ... somehow.

I have implemented every change suggested when Googling for the issue, to no avail.

Android Manifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

<application
    android:allowBackup="true"
    android:label="@string/app_name"
    android:icon="@mipmap/ic_launcher"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <meta-data android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/ic_notification_icon" />
    <meta-data ...
    ...
    <service
        android:name="de.myDomainHere.ShufflechatFirebaseMessagingService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>
    ...

My Service

public class ShufflechatFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG = "shuffleNotifServ";
    private int numMessages = 0;

    @Override
    public void onCreate(){
        Log.i(TAG, "Firebase Messaging Service started");
        // this /\ never gets called
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage){
        super.onMessageReceived(remoteMessage);
        Log.i(TAG, "From: " + remoteMessage.getFrom());
        // this /\ also never gets called
   
        //...do things to handle stuff
    }
}

.

For the sake of convenience of anyone here willing to try and help me, I will include the respective parts of Capacitor's plugin:

AndroidManifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.capacitorjs.plugins.pushnotifications">

<application>
    <service android:name="com.capacitorjs.plugins.pushnotifications.MessagingService" android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>
</application>
</manifest>

MessagingService

public class MessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        PushNotificationsPlugin.sendRemoteMessage(remoteMessage);

        //If I add code here, it runs. I've for example tried writing a file.
        //I shouldn't modify this however, because it will break on Capacitor updates.
    }

    @Override
    public void onNewToken(@NonNull String s) {
        super.onNewToken(s);
        PushNotificationsPlugin.onNewToken(s);
    }
}

Excerpt from PushNotificationsPlugin

public static void sendRemoteMessage(RemoteMessage remoteMessage) {
    PushNotificationsPlugin pushPlugin = 
        PushNotificationsPlugin.getPushNotificationsInstance();
    if (pushPlugin != null) {
        pushPlugin.fireNotification(remoteMessage);
        //This function /\ is really just an interface to the WebView running JS.
    } else {
        lastMessage = remoteMessage;
    }
}

Solution

  • The issue was in the Firebase documentation. The service should list com.google.firebase.MESSAGING_EVENT in its intent-filter, not com.firebase.MESSAGING_EVENT.

    The Firebase team was notified and the documentation has been updated.