2

I had problems with FCM that i want to send notification from Notifications composer (Firebase web). and somewhat modify how it looks in system notifications center (system tray). And also on click open full notification window activity.

That was nothing shiny just adding Action to open link i defined in custom data.

Using FirebaseMessagingService i coudn't acheive nor action button nor showing full notification info.

Action button you might understand why can't be acheived, but full notification info? Not so obvious.

Let me explain. When app is in background you FirebaseMessagingService.onMessageReceived isn't triggered and thus you can't customize how notification looks like or what activity it shows.

By default it starts default starting activity and adds custom data to it.

And here's the problem

It only adds custom data that i defined in "Additional Options" part of notifications composer. Nor notification title, nor body is added to intent extras.

Trying to solve this problem by adding these things in custom data, doesn't work if you have to say something more than 200 symbols (Custom data max input length).

So i override FCM broadcast receiver by declaring folowing in android manifest:

        <receiver
            android:name=".notifications.FCMOverlordBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
                <action android:name="com.google.android.c2dm.intent.REGISTRATION"/>
                <category android:name="${applicationId}"/>
            </intent-filter>
        </receiver>

and also implementing my flawlessly named Receiver.

class FCMOverlordBroadcastReceiver: BroadcastReceiver(){

    override fun onReceive(context: Context?, intent: Intent) {
        NotificationHandler(context!!).onMessageReceived(RemoteMessage(intent.extras))
        abortBroadcast()
    }
}

I'm aware that this would probably cause crash if received event is com.google.android.c2dm.intent.REGISTRATION received.

And as far as i tested this logic works on Nokia 8 (android 9), android 10 emulator and android 11 emulator (app is killed).

I use Glide to asynchronously load notification image for BigPictureStyle and it still works.

My main question

Could this way of doing things could somehow cause crashes or just doesn't work in some cases, Except that Registration event of course?

p.s. I use Firebase-messaging RemoteMessage class to parse intent.extras.

Alpha
  • 1,754
  • 3
  • 21
  • 39

0 Answers0