Android Question Firebase Push Notifications 2023+ works with IOS but not Android

Kevin Hartin

Active Member
Licensed User
I note that there was an unanswered post about this very same question back in August.

I have both the PaushExample and my own App working fine in B4i, but both resuse to receive any messages in B4a.

It has the correct json file, as it wouldnt compile until i setup on the firebase site and downloaded. It seems to subscribe correctly, but when I send using the B4j app, I receive on the iPhone, but not the Android
B4X:
Sub Process_Globals
    Private fm As FirebaseMessaging
End Sub

Private Sub Receiver_Receive (FirstTime As Boolean, StartingIntent As Intent)
    If FirstTime Then
        fm.Initialize("fm")
    End If
    fm.HandleIntent(StartingIntent)
End Sub

Public Sub SubscribeToTopics (Topics() As Object)
    For Each topic As String In Topics
        fm.SubscribeToTopic(topic)
        Log("SUBSCRIBED: "&topic)
    Next
End Sub

Sub fm_MessageArrived (Message As RemoteMessage)
    Log("Message arrived")
    Log($"Message data: ${Message.GetData}"$)
    If B4XPages.IsInitialized And B4XPages.GetManager.IsForeground Then
        Log("App is in the foreground. In iOS a notification will not appear while the app is in the foreground (unless UserNotificationCenter is used).")
    End If
    Dim n2 As Notification
    n2.Initialize2(n2.IMPORTANCE_HIGH)
    n2.Icon = "icon"
    n2.SetInfo(Message.GetData.Get("title"), Message.GetData.Get("body"), Main)
    n2.Notify(1)
End Sub

Sub fm_TokenRefresh (Token As String)
    Log("TokenRefresh: " & Token)
End Sub

Previously (more than a year ago) I had the old messaging working fine on a number of Android apps, but did have some difficulties with IOS.

Any help is much appreciated.

Kev
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
The code is for B4i

Where is your B4A Code? This is the B4A Forum
 
Upvote 0

Kevin Hartin

Active Member
Licensed User
The code is for B4i

Where is your B4A Code? This is the B4A Forum
Oops, to many windows open...

B4X:
Sub Process_Globals
    Private fm As FirebaseMessaging
End Sub

Private Sub Receiver_Receive (FirstTime As Boolean, StartingIntent As Intent)
    If FirstTime Then
        fm.Initialize("fm")
    End If
    fm.HandleIntent(StartingIntent)
End Sub

Public Sub SubscribeToTopics (Topics() As Object)
    For Each topic As String In Topics
        fm.SubscribeToTopic(topic)
        Log("SUBSCRIBED: "&topic)
    Next
End Sub

Sub fm_MessageArrived (Message As RemoteMessage)
    Log("Message arrived")
    Log($"Message data: ${Message.GetData}"$)
    If B4XPages.IsInitialized And B4XPages.GetManager.IsForeground Then
        Log("App is in the foreground. In iOS a notification will not appear while the app is in the foreground (unless UserNotificationCenter is used).")
    End If
    Dim n2 As Notification
    n2.Initialize2(n2.IMPORTANCE_HIGH)
    n2.Icon = "icon"
    n2.SetInfo(Message.GetData.Get("title"), Message.GetData.Get("body"), Main)
    n2.Notify(1)
End Sub

Sub fm_TokenRefresh (Token As String)
    Log("TokenRefresh: " & Token)
End Sub
 
Upvote 0

Kevin Hartin

Active Member
Licensed User
The code here looks correct. Start with the example project: https://www.b4x.com/android/forum/threads/b4x-firebase-push-notifications-2023.148715/
You are probably missing something.

Once you get it working you can continue with your project.
That is the PushExample. I reverted to that after experiencing the difficulties in my app, but I experience the very same behavior. B4i works B4a does all the right things but does not receive anything.

Is it the B4a apps json that is the key to subscribing and subsequently receiving? My first compile had the demo json with the new app string com.motosamoa.pushtest and failed on compile but was fine once I downloaded the json.
 
Upvote 0

Kevin Hartin

Active Member
Licensed User
I am wondering if it is my aps.cer. In the key folder there is an aps.cer which was creted first with the app that works.

The Push Example has its own aps called aps_pushtest.cer and the third app has an aps called aps_rvas.cer

The first app that works had the firebase_push.p12 made from aps.cer.

How do we manage all the aps and p12 files for different apps in the one directory?
 
Upvote 0

Kevin Hartin

Active Member
Licensed User
I've cleared out the aps.cer and gone through the process of building the push store, but it makes no difference. the Push example and another push app I've built all crash when the GetToken is called with the following;
ERROR LOG:
Application_Start
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
Application_Active
FCMConnected
Subscribed to ios_statistics
Error occurred on line: 27 (FirebaseMessaging)
Object was not initialized (NSObject)
Stack Trace: (
  CoreFoundation       4250D45C-51A9-3AC5-8C3D-69DE936FAD46 + 540824
  libobjc.A.dylib      objc_exception_throw + 88
  CoreFoundation       4250D45C-51A9-3AC5-8C3D-69DE936FAD46 + 1570376
  Push Test            -[B4IObjectWrapper object] + 116
  Push Test            -[B4INativeObject GetField:] + 96
  Push Test            -[b4i_firebasemessaging _gettoken] + 440
  Push Test            -[ResumableSub_firebasemessaging_SubscribeToTopics resume::] + 1916
  CoreFoundation       4250D45C-51A9-3AC5-8C3D-69DE936FAD46 + 135412
  CoreFoundation       4250D45C-51A9-3AC5-8C3D-69DE936FAD46 + 131396
  Push Test            +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1348
 Push Test            -[B4IShell runMethod:] + 420
 Push Test            -[B4IShell raiseEventImpl:method:args::] + 1472
 Push Test            -[B4IShellBI raiseEvent:event:params:] + 1360
 Push Test            -[B4IDelegatableResumableSub resume::] + 376
 Push Test            -[B4I checkAndRunWaitForEvent:event:params:] + 468
 Push Test            -[B4IShellBI raiseEvent:event:params:] + 1216
 Push Test            __33-[B4I raiseUIEvent:event:params:]_block_invoke + 52
 libdispatch.dylib    B8C15E69-D076-317D-9296-1279500738FC + 9072
 libdispatch.dylib    B8C15E69-D076-317D-9296-1279500738FC + 16592
 libdispatch.dylib    B8C15E69-D076-317D-9296-1279500738FC + 76256
 libdispatch.dylib    _dispatch_main_queue_callback_4CF + 44
 CoreFoundation       4250D45C-51A9-3AC5-8C3D-69DE936FAD46 + 356196
 CoreFoundation       4250D45C-51A9-3AC5-8C3D-69DE936FAD46 + 344456
 CoreFoundation       CFRunLoopRunSpecific + 572
 GraphicsServices     GSEventRunModal + 164
 UIKitCore            187C890E-8E4C-32F2-A97E-FA905A55F094 + 4055744
 UIKitCore            UIApplicationMain + 340
 Push Test            main + 100
 dyld                 5DB83988-2EE6-3756-BD07-B8D67B1133A5 + 212276
)
Application_Pushtoken
FirebaseMessaging:
'Code module

Sub Process_Globals
    Private fm As FirebaseMessaging
    Private FirstTime As Boolean = True
End Sub

Public Sub SubscribeToTopics (Topics() As Object)
    If FirstTime Then
        FirstTime = False
        fm.Initialize("fm")
        fm.FCMConnect
        Wait For fm_FCMConnected
        Log("FCMConnected")
    End If
    For Each topic As String In Topics
        fm.SubscribeToTopic("ios_" & topic)
        Log("Subscribed to ios_" & topic)
    Next
    Log("Token: " & GetToken)
End Sub


Private Sub GetToken As String
    Dim FIRMessaging As NativeObject
    FIRMessaging = FIRMessaging.Initialize("FIRMessaging").GetField("messaging")
    Dim token As NativeObject = FIRMessaging.GetField("FCMToken")
    If token.IsInitialized = False Then Return "" Else Return token.AsString
End Sub

Public Sub MessageReceivedWhileInTheForeground (Message As Map)
    Log($"Message arrived while app is in the foreground: ${Message}"$)
    Dim aps As Map = Message.Get("aps")
    Dim alert As Map = aps.Get("alert")
    Log(alert.Get("body"))
    Log(alert.Get("title"))
End Sub

Following are all the files...
 

Attachments

  • FCMPush.zip
    1.9 KB · Views: 28
  • PushTest.zip
    4.1 KB · Views: 25
  • pushtest-980f7-firebase-adminsdk.zip
    1.8 KB · Views: 23
  • keys.zip
    14 KB · Views: 21
Last edited:
Upvote 0

Kevin Hartin

Active Member
Licensed User
I built an app and fb notification works fine on IOS but not on Android.

I used the pushExample app to test and neither iOS nor Android work, though they give different errors.

a third app cloned from the first does not work in either iOS or Android.

questions outstanding are

how do we manage the keys folder with respect to APS.cer etc for multiple apps?

why does analytics not initialize?

why does one app work and others fail at the get token sub?
 
Upvote 0

aminoacid

Active Member
Licensed User
Longtime User
Upvote 0
Top