Android Firebase Messaging Changes for Android 12+ to prevent app crashes when receiving a push

This post should be turned into a KB article.

Environment:
Volt Iris 9.2.56
Windows 10
Galaxy Z Flip 4
Android 13 (error also occurs for Android 12)

I found that when trying to send a push notification via the Engagement Services console to my phone, the device would receive the push but would not display it in the notification area and the push would cause the targeted Android app to crash with the following error call stack:

12-23 15:48:02.786 14647 14780 W FirebaseMessaging: Error while parsing timestamp in GCM event
12-23 15:48:02.786 14647 14780 W FirebaseMessaging: java.lang.NumberFormatException: s == null
12-23 15:48:02.786 14647 14780 W FirebaseMessaging: at java.lang.Integer.parseInt(Integer.java:709)
12-23 15:48:02.786 14647 14780 W FirebaseMessaging: at java.lang.Integer.valueOf(Integer.java:1078)
12-23 15:48:02.786 14647 14780 W FirebaseMessaging: at com.google.firebase.messaging.zzd.zzb(Unknown Source:64)
12-23 15:48:02.786 14647 14780 W FirebaseMessaging: at com.google.firebase.messaging.zzd.zze(Unknown Source:26)
12-23 15:48:02.786 14647 14780 W FirebaseMessaging: at com.google.firebase.messaging.FirebaseMessagingService.handleIntent(Unknown Source:318)
12-23 15:48:02.786 14647 14780 W FirebaseMessaging: at com.google.firebase.iid.zzg.run(Unknown Source:24)
12-23 15:48:02.786 14647 14780 W FirebaseMessaging: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137)
12-23 15:48:02.786 14647 14780 W FirebaseMessaging: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)
12-23 15:48:02.786 14647 14780 W FirebaseMessaging: at java.lang.Thread.run(Thread.java:1012)
--------- beginning of crash
12-23 15:48:02.790 14647 14780 E AndroidRuntime: FATAL EXCEPTION: pool-4-thread-1
12-23 15:48:02.790 14647 14780 E AndroidRuntime: Process: com.hcltechsw.demo.ucx.assetmgmt, PID: 14647
12-23 15:48:02.790 14647 14780 E AndroidRuntime: java.lang.IllegalArgumentException: com.hcltechsw.demo.ucx.assetmgmt: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
12-23 15:48:02.790 14647 14780 E AndroidRuntime: Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.
12-23 15:48:02.790 14647 14780 E AndroidRuntime: at android.app.PendingIntent.checkFlags(PendingIntent.java:408)
12-23 15:48:02.790 14647 14780 E AndroidRuntime: at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:688)
12-23 15:48:02.790 14647 14780 E AndroidRuntime: at android.app.PendingIntent.getBroadcast(PendingIntent.java:675)
12-23 15:48:02.790 14647 14780 E AndroidRuntime: at com.google.android.gms.internal.zzcki.zzyu(Unknown Source:25)
12-23 15:48:02.790 14647 14780 E AndroidRuntime: at com.google.android.gms.internal.zzcki.zzaxo(Unknown Source:2)
12-23 15:48:02.790 14647 14780 E AndroidRuntime: at com.google.android.gms.internal.zzcii.initialize(Unknown Source:4)
12-23 15:48:02.790 14647 14780 E AndroidRuntime: at com.google.android.gms.internal.zzchj.<init>(Unknown Source:178)
12-23 15:48:02.790 14647 14780 E AndroidRuntime: at com.google.android.gms.internal.zzchj.zzdu(Unknown Source:28)
12-23 15:48:02.790 14647 14780 E AndroidRuntime: at com.google.android.gms.measurement.AppMeasurement.getInstance(Unknown Source:0)
12-23 15:48:02.790 14647 14780 E AndroidRuntime: at com.google.firebase.messaging.zzd.zzcz(Unknown Source:0)
12-23 15:48:02.790 14647 14780 E AndroidRuntime: at com.google.firebase.messaging.zzd.zzb(Unknown Source:174)
12-23 15:48:02.790 14647 14780 E AndroidRuntime: at com.google.firebase.messaging.zzd.zze(Unknown Source:26)
12-23 15:48:02.790 14647 14780 E AndroidRuntime: at com.google.firebase.messaging.FirebaseMessagingService.handleIntent(Unknown Source:318)
12-23 15:48:02.790 14647 14780 E AndroidRuntime: at com.google.firebase.iid.zzg.run(Unknown Source:24)
12-23 15:48:02.790 14647 14780 E AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137)
12-23 15:48:02.790 14647 14780 E AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)
12-23 15:48:02.790 14647 14780 E AndroidRuntime: at java.lang.Thread.run(Thread.java:1012)

Doing some investigating, I found a StackOverflow article with a similar error:
https://stackoverflow.com/questions/50795697/android-firebase-error-while-parsing-timestamp-in-gcm-event-null-timestamp

In the comments of the StackOverflow thread one person mentions they fixed it by updating the version of the com.google.firebase:firebase-messaging dependency in the build.gradle file.

I found that our default settings in the generated build.gradle file reference old versions that need to be updated. These default values are stored in the buildparams.properties file in the Android plugin. This file is embedded in the plugin and cannot be directly edited. The default values in the file are:

dependencies_google_fcm_messaging =implementation 'com.google.firebase:firebase-messaging:11.6.0'
classpath_google_services =classpath 'com.google.gms:google-services:3.1.0'

These values should be updated by specifying the correct values in the androidbuild.properties file in the project root folder. If this file does not exist, it needs to be created and have the following information added to it:

classpath_google_services=classpath 'com.google.gms:google-services:4.3.14'
dependencies_google_fcm_messaging=implementation 'com.google.firebase:firebase-messaging:23.1.1'

Once these are saved to the file, perform a clean rebuild of the application. The push will now be displayed in the notification area if the app is not in the foreground. If the app is in the foreground the push will be processed according to the online callback function in the app code.

Thanks @Gunndarr Dotter We have created below technote for the same and currently under review and will be published soon

https://support.hcltechsw.com/csm?id=kb_article&sysparm_article=KB0102295

@Murali Boddeda @Gunndarr Dotter can you please guide me on how to integrate FCM into an Android application?

@Vishal Vats Thanks for posting the question.

Please find the below Article for configuring FCM in your Android application

https://support.hcltechsw.com/csm?id=kb_article&sysparm_article=KB0112550

You can use below sample app for your reference-->

https://github.com/HCL-TECH-SOFTWARE/volt-mx-samples/tree/main/EngagementServices

FYI-->

https://opensource.hcltechsw.com/volt-mx-docs/docs/documentation/Iris/iris_api_dev_guide/content/push_notifications.html#enabling-push-notifications

Thanks, @Murali Boddeda for the reply. I have also set up the Engagement service for Android in the Foundry console. After reviewing the code from the GitHub repo that you provided, I tweaked my code and it is working fine.

But here is a catch, it only works for the preview version inside the Volt MX Iris app. The devices are registered inside the engagement service, and I can even see their details in the Engagement console.

But when I build the apk for the Android app, then I do not get any response after allowing the notification prompt. The code doesn't go in any of the specified callbacks. Why is that happening? I guess I have to do something to make it work in the apk.

While searching on my own, I stumbled upon this article and it says that we have to make a folder named fmc inside the project directory. Although, it never states that we have to put the google-services.json file inside the newly created folder, I have done that.

Despite all of these things, I am unable to register the device via an apk.

@Vishal Vats Please clarify whether your engagement server a onPremises server or Cloud Foundry. What is the Android device OS version which you are using.

I have just set up the Engagement service on Foundry. I have tested out it on multiple devices having OS v11, and 13. It doesn't work for any of those in the apk, but for the preview mode, it works.

By the way, do I have to provide the google-service.json file in my volt mx project directory? I am using the service in my project, not in the one that I downloaded from GitHub

@Vishal Vats For Android if Target SDK version is 33 and above, below permissions is required in IRIS project setting -> Native-> Android Mobile/Tablet -> Tags (Child tag entries under < manifest > tag) uses-permission android:name="android.permission.POST_NOTIFICATIONS" and then check the issue.

It's already there. The weird thing is that if there are some issues with the apk calling the register method, then why the onfailure callback is not called?

Also, seeing the same code work in Volt MX Iris app, and not in the apk is really weird.