Android Question Firebase doesn't work on all devices

Hello!
I am trying to create my chat. I use for this Firebase.
on my Honor it works, on emulators with google_apis and google_apis_playstore it works to, but on default android emulator and my One plus it doesn't (i have google services).

I have no idea what the problem is, I will be glad for any help)

My subs

fm_MessageArrived:
B4X:
Sub fm_MessageArrived (Message As RemoteMessage)
    Log("Message arrived")
    Log($"Message data: ${Message.GetData}"$)
    Dim n As Notification
    n.Initialize
    n.Icon = "icon"
    n.SetInfo(Message.GetData.Get("title"), Message.GetData.Get("body"), chatex)
    n.Notify(1)
  
    CallSub3(chatex,"writemessage",Message.GetData.Get("title"),Message.GetData.Get("body"))
End Sub

SendMessage:
B4X:
Private Sub SendMessage(Topic As String, Title As String, Body As String)
    Log("MyTopic ------------> "& Topic)
    Dim Job As HttpJob
    Job.Initialize("fcm", Me)
    Dim m As Map = CreateMap("to": $"/topics/${Topic}"$)
    Dim data As Map = CreateMap("title": Title, "body": Body)
    m.Put("data", data)
    Dim jg As JSONGenerator
    jg.Initialize(m)
    Job.PostString("https://fcm.googleapis.com/fcm/send", jg.ToString)
    Job.GetRequest.SetContentType("application/json;charset=UTF-8")
    Job.GetRequest.SetHeader("Authorization", "key=" & WebApiKey)
End Sub

and my manifest file
B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="23"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>
    )
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")

CreateResourceFromFile(Macro, Themes.LightTheme)
'End of default text.
CreateResourceFromFile(Macro, FirebaseAnalytics.GooglePlayBase)
CreateResourceFromFile(Macro, FirebaseAnalytics.Firebase)
CreateResourceFromFile(Macro, FirebaseNotifications.FirebaseNotifications)

'************ Firebase Base ************
CreateResourceFromFile("google-services", "google-services.json")

AddPermission(android.permission.WRITE_EXTERNAL_STORAGE)
AddPermission(android.permission.ACCESS_NETWORK_STATE)
AddPermission(android.permission.INTERNET)
AddPermission(android.permission.WAKE_LOCK)
AddPermission(com.google.android.c2dm.permission.RECEIVE)
AddPermission(${applicationId}.permission.C2D_MESSAGE)
AddManifestText( <permission android:name="${applicationId}.permission.C2D_MESSAGE"
  android:protectionLevel="signature" />)
AddApplicationText(
<receiver
  android:name="com.google.android.gms.measurement.AppMeasurementReceiver"
  android:enabled="true">
  <intent-filter>
  <action android:name="com.google.android.gms.measurement.UPLOAD"/>
  </intent-filter>
  </receiver>

  <service
  android:name="com.google.android.gms.measurement.AppMeasurementService"
  android:enabled="true"
  android:exported="false"/>
   <provider
  android:authorities="${applicationId}.firebaseinitprovider"
  android:name="com.google.firebase.provider.FirebaseInitProvider"
  android:exported="false"
  android:initOrder="100" />
    <receiver
  android:name="com.google.android.gms.measurement.AppMeasurementReceiver"
  android:enabled="true">
  <intent-filter>
  <action android:name="com.google.android.gms.measurement.UPLOAD"/>
  </intent-filter>
  </receiver>

  <service
  android:name="com.google.android.gms.measurement.AppMeasurementService"
  android:enabled="true"
  android:exported="false"/>
   <receiver
  android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver"
  android:exported="true"
  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>
    <receiver
  android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver"
  android:exported="false" />
</service>

  <service
  android:name="com.google.firebase.iid.FirebaseInstanceIdService"
  android:exported="true">
  <intent-filter android:priority="-500">
  <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
  </intent-filter>
  </service>
 
     <service android:name=".service.MyFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

    <service android:name=".service.MyFirebaseInstanceIDService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
        </intent-filter>
    </service>
)
 
Last edited:

Alex_197

Well-Known Member
Licensed User
Longtime User
Is notification enabled on the real phone? If not - that's the reason, Check the phone settings. Also - check the app settings, check the app info in applications list (just tap on the app name in the list) and check App Notification if it's On or Off.
 
Upvote 0

Hi, thanks for the reply. Notifications enabled
 
Upvote 0


Hello, Erel, thanks for reply)
Im testing my app in the release mode.

chatex its my activity module for printing messages on the screen and sending (like SendMessage sub)


B4X:
Sub writemessage(username As String,text As String)
    EditTextChatView.Text=EditTextChatView.Text & CRLF & username
    EditTextChatView.Text=EditTextChatView.Text & CRLF & text & CRLF
End Sub
 
Upvote 0
It will only work when a message arrives while the activity is in the foreground.

I checking on two phones at the same time, while my activity is in the foreground. From first (HONOR) I can send messages and see all messages (mine and from another phone), but from another one I can only send messages and dont see all messages (from HONOR and from another phone)
As i found out, sub fm_MessageArrived not called on second device
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
With newer Android versions came more possibilities to set options. Mostly there are config issues which cause messages to be not delivered like.

- energy savings
- app isn't allowed to run in the background = app is fully stopped when phone goes to sleep (older phones have a whitelist, on my Huawei P10 even WhatsApp wasn't allowed to run in the background first).
- app isn't allowed to show messages
- ...

If the phone is set up correctly, you can even swipe the app without problems.

Hint: Use DATA messages only. These do not throw a notification (= less problems due to settings). Throw a notification on you own.
 
Upvote 0

Hello, my two devices are set up the same, they have the same android version

If you mean https://www.b4x.com/android/forum/t...-messages-firebase-cloud-messaging-fcm.67716/ guide, then the message does not
аrrived the phone too
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
Try this (with high priority) and send a timestamp inside the message to check if the message IS delivered or just the notification isn't "fired":



B4X:
Private Sub SendMessageToSingleDevice(Devtoken As String, MyData As String)
 
   Dim Job As HttpJob
   Job.Initialize("SendMessage", Me)
  
   Dim m As Map = CreateMap("to": $"${Devtoken}"$)
   Dim data As Map = CreateMap("data": MyData)
    [B][COLOR=rgb(209, 72, 65)]Dim pri As Map=CreateMap("priority":"high") 'important [/COLOR][/B]
    m.Put("data", data)
    m.Put("android", pri)
 
   Dim jg As JSONGenerator
   jg.Initialize(m)
   Job.Tag=jg
   Log(jg.ToString)
   Job.PostString("https://fcm.googleapis.com/fcm/send", jg.ToString)
   Job.GetRequest.SetContentType("application/json;charset=UTF-8")
   Job.GetRequest.SetHeader("Authorization", "key=" & API_KEY)
  
  End Sub

B4X:
Sub fm_MessageArrived (Message As RemoteMessage)
    
    Log("Message arrived")
    
    Dim n As Notification
   [B][COLOR=rgb(209, 72, 65)] n.Initialize2(n.IMPORTANCE_HIGH) 'important otherwise the notification isn't "fired"[/COLOR][/B]
    
    n.Icon = "icon"
    n.OnGoingEvent=False
    n.Light=True
    n.Vibrate=False
    n.SetInfo2("FCM","The message...","This is the tag", "Main")
    
    n.Notify(1)


Works for me
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…