Android Question Firebase notification issue with app closed

juacochero

Member
Licensed User
Longtime User
Hi everyone!
I have gone over all the tutorials/questions I could find in the forums and still can't make this work properly.
I have a php page that sends a push notification through Firebase to my B4A app. The notification displays perfectly if I have the app on, but they do not appear if I have the app closed (swipe closed that is, force closed).

Any idea how I can fix this? Thank you so much!

My App Code goes as follows.
In Starter Service:

B4X:
Sub Service_Create
    CallSubDelayed(FirebaseMessaging, "SubscribeToTopics")
End Sub

In FirebaseMessaging Service

B4X:
#Region  Service Attributes
    #StartAtBoot: True
#End Region

Sub Process_Globals
    Public fm As FirebaseMessaging
End Sub

Sub Service_Create
    fm.Initialize("fm")
End Sub

Public Sub SubscribeToTopics'************ Google Play Services Base ************
    fm.SubscribeToTopic("general") 'you can subscribe to more topics
    Log (fm.Token)
End Sub

Sub Service_Start (StartingIntent As Intent)
    If StartingIntent.IsInitialized Then fm.HandleIntent(StartingIntent)
    Sleep(0)
    Service.StopAutomaticForeground
    
End Sub

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"), Main)
    n.Notify(1)
End Sub

Sub Service_Destroy

End Sub

My php Function (Removed my API key) in a server

PHP:
function notify ($deviceID, $t, $noti)
    {
    // API access key from Google API's Console
        if (!defined('API_ACCESS_KEY')) define( 'API_ACCESS_KEY', 'MY API KEY GOES HERE' );
        $tokenarray = array($deviceID);
        // prep the bundle
        $msg = array
        (
            'title'     => $t,
            'body'     => $noti,
            'MyKey1'       => 'MyData1',
            'MyKey2'       => 'MyData2',
          
        );
        $fields = array
        (
            'registration_ids'     => $tokenarray,
            'data'            => $msg
        );
        
        $headers = array
        (
            'Authorization: key=' . API_ACCESS_KEY,
            'Content-Type: application/json',
        );
        
        $ch = curl_init();
        curl_setopt( $ch,CURLOPT_URL, 'fcm.googleapis.com/fcm/send' );
        curl_setopt( $ch,CURLOPT_POST, true );
        curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
        curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
        curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
        curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
        $result = curl_exec($ch );
        curl_close( $ch );
        return $result;
    }

And finally app manifest:
B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="26"/>
<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, FirebaseAnalytics.GooglePlayBase)
CreateResourceFromFile(Macro, FirebaseAnalytics.Firebase)
CreateResourceFromFile(Macro, FirebaseNotifications.FirebaseNotifications)
 

juacochero

Member
Licensed User
Longtime User
Thank you Erel,
1. Yes, I used the B4J code first and I had the exact same issue, following the tutorial step by step.
2. It is indeed in release mode.
3. That is a problem. How do other apps that have push notifications (Whatsapp, Facebook, etc.) manage this? When I explicitly kill those they do still show the notifications.

Thanks again
 
Upvote 0
Top