Android Question Android 9 and Firebase Messaging

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Hello,

I'm testing an app using Firebase Cloud Services on Android 9 and getting the follow error:

java.lang.RuntimeException: Unable to start service com.comten.nexphone.firebasemessaging@f3fffa5 with Intent { cmp=com.comten.nexphone/.firebasemessaging (has extras) }: java.lang.RuntimeException: java.lang.SecurityException: Permission Denial: startForeground from pid=12936, uid=10085 requires android.permission.FOREGROUND_SERVICE

The same app is running well in Android 8 (sdk 27). Is there any special permission to require in Android 9? I noticed that this error occurs only when the app is minimized what means that there is something blocking the Firebase service to start when the app in in foreground...

Any ideas?
 

Marcos Alves

Well-Known Member
Licensed User
Longtime User
No. As FCM is a Google service, there's absolutely no reason to add it. Maybe you want to show us your code?
Hello @KMatle . FCM Is as Google Service BUT the Firebase implementation in B4A starts a Foreground Service associated to the apk thread, which requires the Permission. Notice that there are specific conditions to get the error raised:

1. Use Firebase Service
2. Receive a FCM
3. The app is in background or closed when the message arrives
4. The Android MUST TO BE 9 and Target SDK 28

A more detailed explanation about this requirement can be read at: https://developer.android.com/about/versions/pie/android-9.0-migration#tya

Look that IF your target SDK is 27, your android is 8 or the app isn't in foreground the error will not raise. But if you use Firebase to start your app and the Android is 9 AND SDK is 28... you have a big problem.

That's it. The situation that I'm explaining is a real production case in a Google Play app used by more than 2K customers developed in B4A. We are using Firebase the last 5 years since the old GCM. If there is something different on Google documentation or someplace else, it's not according to the practical experience.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4A starts a Foreground Service associated to the apk thread, which requires the Permission. Notice that there are
specific conditions to get the error raised:
1. Note that it is not suggested to use TargetSDK 28. For now you should use TargetSDK 26. And wih targetsdk 26 everything is working just fine.

I do use FirebaseMessaging. I m using Android 9.

I do not need to have this permission as i am handling the Service Foregroundmode successfully.

Google requires the use of TargetSDK 28 by end of this year. No rush to do it now.
 
Last edited:
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
1. Note that it is not suggested to use TargetSDK 28. For now you should use TargetSDK 26. And wih targetsdk 26 everything is working just fine.

I do use FirebaseMessaging. I m using Android 9.

I do not need to have this permission as i am handling the Service Foregroundmode successfully.
Hello @DonManfred ,

you're right. If use sdk 26 everything will work fine. But Android 9 is the future and Sdk 28 will be mandatory. Then, as we have many products in Play Store used by our customers, we need to anticipate in order to fix a future possible problem before happening. In the case of Firebase Cloud Messaging this unique line in manifest fixed the problem without any additional issue... As sdk 28 will not change, let's register this orientation to the developers.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
But Android 9 is the future and Sdk 28 will be mandatory.
Yes. By end of this year! But no need to do it NOW.

I suggest to wait until Erel says that everything in B4A is ready for TargetSDK 28.

This will happen in the next months i´m sure. At least it will happen before the timeline reached....
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Yes. By end of this year! But no need to do it NOW.

I suggest to wait until Erel says that everything in B4A is ready for TargetSDK 28.

This will happen in the next months i´m sure. At least it will happen before the timeline reached....
We are really worried with this timeline ... Need to have time to change some native libs before and test... Let's see...
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
I ran into the same issue today in an app that starts a service to generate reminders as notifications. On my Android 9 device I got the FOREGROUND_SERVICE permission error. Adding the permission per Marcos' suggestion resolved the issue. I had previously set my targetSDKVersion to 28 & have published the app with this, so can I now change it back to 26 & publish an update? My understanding was that you can't do this, but I could be wrong.

- Colin.
 
Upvote 0
Top