Android Question Is it possible to automatically open my app upon receipt of a Firebase push message?

Ralph Parkhurst

Member
Licensed User
Longtime User
I am using the B4A FirebaseNotifications Library with the sample B4A code found in this thread: FirebaseNotifications - Push messages / Firebase Cloud Messaging (FCM)

This works extremely well, and it notifies the user even when the app is paused, or not running. The user gets the Notification pop up and when they click on that notification message, the app opens to the foreground nicely.

What I'd like to know is this:- Is it possible to open the app automatically even when the Main activity is paused - without having to click on the notification message?

I tried calling and passing the notification data payload to my message handler sub "MessageReceived" within the Main Activity:

B4X:
CallSubDelayed3(Main, "MessageReceived", Message.GetData.Get("TxTopic"), Message.GetData.Get("Payload"))

but it just returns a queuing message in the debug log:

B4X:
sending message to waiting queue of uninitialized activity (messagereceived)

I am using B4A 10.7 and FirebaseNotifications Library 2.00

Many thanks,
Ralph
 

Alex_197

Well-Known Member
Licensed User
Longtime User
Check line #20

Start the app when notification arrives:
Sub fm_MessageArrived (Message As RemoteMessage)
    
    Try
        Log("Message arrived")
        Log($"Message data: ${Message.GetData}"$)
        
        Dim n As Notification
        Dim ID As Int=modFun.GetID
        
        'n.Initialize
        n.Initialize2(n.IMPORTANCE_HIGH)
    
        n.AutoCancel=True
        n.Sound=True
        
        n.Icon = "icon"
        n.Vibrate=True
        n.Light=True
    
        n.SetInfo(Message.GetData.Get("title"), Message.GetData.Get("body"), Main)
            
        
                
        n.Notify(ID)
    
    Catch
        Log("fm_MessageArrived " & LastException)
        modFun.ShowError("FirebaseMessaging_fm_MessageArrived " & LastException)
    End Try
    
End Sub
 
Upvote 0

Ralph Parkhurst

Member
Licensed User
Longtime User
Hi Alex,

Thank you kindly for your reply and your note regarding line #20.

I had a similar line to your line #20 already in my code. However I wish to avoid the user having to press the notification.

Here is my full code for the MessageArrived code within the FirebaseMessaging service module:

Sub fm_MessageArrived:
Sub fm_MessageArrived (Message As RemoteMessage)
    Log("Message arrived")
    Log($"Message data: ${Message.GetData}"$)
    Dim n As Notification
    n.Initialize2(n.IMPORTANCE_HIGH)
    n.Icon = "icon"
    n.vibrate = True
    n.AutoCancel = True
    n.SetInfo(Message.GetData.Get("Title"), Message.GetData.Get("Body"), Main)
    n.Notify(1)
    CallSubDelayed3(Main, "MessageReceived", Message.GetData.Get("TxTopic"), Message.GetData.Get("Payload"))
End Sub

The help text for the SetInfo command states that "this activity will start when the user presses on that notification". What I am trying to achieve is way to start the Main activity so it can process my MessageReceived sub without the user pressing anything.

The CallSubDelayed3 line does not seem to start the Main activity - it only seems to send a request that is queued.

Any help would be warmly appreciated.

- Ralph
 
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
Hi Alex,

Thank you kindly for your reply and your note regarding line #20.

I had a similar line to your line #20 already in my code. However I wish to avoid the user having to press the notification.

Here is my full code for the MessageArrived code within the FirebaseMessaging service module:

Sub fm_MessageArrived:
Sub fm_MessageArrived (Message As RemoteMessage)
    Log("Message arrived")
    Log($"Message data: ${Message.GetData}"$)
    Dim n As Notification
    n.Initialize2(n.IMPORTANCE_HIGH)
    n.Icon = "icon"
    n.vibrate = True
    n.AutoCancel = True
    n.SetInfo(Message.GetData.Get("Title"), Message.GetData.Get("Body"), Main)
    n.Notify(1)
    CallSubDelayed3(Main, "MessageReceived", Message.GetData.Get("TxTopic"), Message.GetData.Get("Payload"))
End Sub

The help text for the SetInfo command states that "this activity will start when the user presses on that notification". What I am trying to achieve is way to start the Main activity so it can process my MessageReceived sub without the user pressing anything.

The CallSubDelayed3 line does not seem to start the Main activity - it only seems to send a request that is queued.

Any help would be warmly appreciated.

- Ralph
It's not a good idea. What if the user does something on the other activity and the new message has arrived?

If the user needs to press the icon it's up to him when to do it.

And I'm not sure if the Google play market will allow to do this.
 
Upvote 0

Ralph Parkhurst

Member
Licensed User
Longtime User
Thanks Alex.

In this case, my app is for notifying critical warning messages to a private group of users and is not distributed to the public. They want to minimise the need for users for press anything when receiving these warnings.

However I do accept and agree it may not a good idea generally to do this. And I suspect B4A does not allow it for the reasons you mention.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I think you can build the notification with big image using NB6. It can almost cover the entire screen.
 
Upvote 0

Ralph Parkhurst

Member
Licensed User
Longtime User
Thank you Aeric. I will check that out.

The app uses voice announcements via the media player to convey critical warnings, however a big image notification that covers a large part of the screen would be most helpful.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

Ralph Parkhurst

Member
Licensed User
Longtime User
Thank you for your clarification Erel.

From that tutorial, I believe you are referring to ...
Additional changes in Android 10 that are not related to the targetSdkVersion value:

That is a shame. The particular handsets selected for this project (and already purchased) will not permit the Display Over Other Apps feature. The device returns a "Feature Not Available" message when I try.

Oh well, at least I now know the answer for sure.

Warm regards,
Ralph
 
Upvote 0
Top