iOS Question Firebase Push arrives only with App Active

Luiz Fernando Orlandini

Active Member
Licensed User
Longtime User
Hi there,

after migrate the APP to Firebase I started to deal with something weird.

The push message only arrives when I open the App. If the App stays in the background mode the message is not fired.

I have reviewed and I'm calling App.RegisterForRemoteNotifications and also called App.RegisterUserNotifications(True, True, True)

Any thoughts?

Thanks.
 

Luiz Fernando Orlandini

Active Member
Licensed User
Longtime User
Are you setting the content-available field (you shouldn't)?

I'm not. That's what I'm sending in terms of payload to firebase. The message arrives, although only when the App is active.

B4X:
    public static String getFirebaseMessage(AbstractPushTO pushMessage, String token) {

        Map<String, Object> firebaseMessageMap = new HashMap<String, Object>();

        firebaseMessageMap.put("priority", "normal");

        firebaseMessageMap.put("delay_while_idle", false);

        firebaseMessageMap.put("time_to_live", 86400);

        firebaseMessageMap.put("dry_run", false);

        firebaseMessageMap.put("content_available", true);

        firebaseMessageMap.put("data", getData(pushMessage));

        firebaseMessageMap.put("to", token);



        Map<String, String> notification = new HashMap<String, String>();

        notification.put("title", pushMessage.getMessageTitle());

        notification.put("body", pushMessage.getMessageBody());

        firebaseMessageMap.put("notification", notification);



        System.out.println(jsonify(firebaseMessageMap));   

        return jsonify(firebaseMessageMap);

    }
 
Last edited:
Upvote 0

Luiz Fernando Orlandini

Active Member
Licensed User
Longtime User
Some more findings https://firebase.google.com/docs/cloud-messaging/concept-options

Note: If you want to send messages consisting of only custom key-values to an iOS device when the app is in the background, set custom key-value pairs in the data key and set content_available to true.

Note: On iOS, set content_available when the app server needs to send a Send-to-Sync message. An inactive client app executes your logic in the background, while an app in the foreground passes the message to didReceiveRemoteNotification:.

Tried that, although the behaviour the same still.
 
Last edited:
Upvote 0

Luiz Fernando Orlandini

Active Member
Licensed User
Longtime User
Are you setting the content-available field (you shouldn't)?
Erel,

here are my findings after a lot of tests and researches. Hope this helps you to try to help to find a better solution.

I used to have my App using the old push solution. With B4i 2.8 I started thinking to migrate to FCM, once I had a bunch of problems with Amazon SNS (just for Android, that's other thing though).

I did all the recommendations on the tutorial, and ported my server code to start sending messages via FCM, this is working good

So, I have in my App.

Scenario 01

B4X:
Private Sub Application_Start (Nav As NavigationController)
    Analytics.Initialize
    CheckForPushMessage
    fm.Initialize("fm")
End Sub

Private Sub CheckForPushMessage
    If App.LaunchOptions.IsInitialized And _
          App.LaunchOptions.ContainsKey("UIApplicationLaunchOptionsRemoteNotificationKey") Then
          Dim data As Object = App.LaunchOptions.Get("UIApplicationLaunchOptionsRemoteNotificationKey")
          Dim no As NativeObject = App
          no.GetField("delegate").RunMethod("application:didReceiveRemoteNotification:", _
            Array(App, data))
    End If
End Sub

Private Sub Application_RemoteNotification (Message As Map, CompletionHandler As CompletionHandler)
    Log("Remote notification ARRIVED:" & Message)
End Sub

When I send a message with content-available = 1, the message does not arrive in any cases.


Scenario 02

B4X:
Private Sub Application_Start (Nav As NavigationController)
    Analytics.Initialize
    fm.Initialize("fm")
End Sub

Private Sub Application_RemoteNotification (Message As Map, CompletionHandler As CompletionHandler)
    Log("Remote notification ARRIVED:" & Message)
    CompletionHandler.Complete
End Sub

When I send a message with content-available = 1, the message arrives only when the device is Active.

Scenario 03

B4X:
Private Sub Application_Start (Nav As NavigationController)
    Analytics.Initialize
    fm.Initialize("fm")
End Sub

Private Sub Application_RemoteNotification (Message As Map, CompletionHandler As CompletionHandler)
    Log("Remote notification ARRIVED:" & Message)
End Sub

When I send a message with content-available = 1, the message arrives only when the device is Active.

Conclusion
Based on this we potentially need some extra piece of code to make it works, or even to open a hot fix on B4i.

Thoughts?

Thanks in advance.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

Luiz Fernando Orlandini

Active Member
Licensed User
Longtime User
Actually you are.

Please start with the exact code from the tutorial and use the B4J code to send the messages. Once it works you can modify it.
https://www.b4x.com/android/forum/t...ions-push-messages-server-not-required.68645/

Erel,

I did a different set of tests with the content_available tag (true and false combinations) and all of them without success.

In the B4J code we have:

B4X:
Private Sub SendMessage(Topic As String, Title As String, Body As String)
    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)
    If Topic.StartsWith("ios_") Then
        Dim iosalert As Map =  CreateMap("title": Title, "body": Body, "sound": "default")
        m.Put("notification", iosalert)
        m.Put("priority", 10)
    End If
    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=" & API_KEY)
End Sub

And this allows us to work only with notifications. I'm not quite sure. Is this any kind of restrictions (combine notification and data push message in the payload) at this point?

Thanks for your support and patience.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

Luiz Fernando Orlandini

Active Member
Licensed User
Longtime User

I'm not trying to send a silent notifications. I can remove content-availave tag from payload, that not a big deal

Although this does not solve my problem. I just need to receive a push notification when my APP is in background and foreground modes. But please note that my payload is very different.

I'm not using topics and I'm sending messages direct to users with data + notifications datas. And this is not supported nowadays by B4J.


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