Android Question B4A Push Notifications

morphe2011

Member
Licensed User
Longtime User
Good day from the wilds of Western Canada. What event is raised by push notifications when the app has been closed or is in the background? The context is that we are developing an app for the BMX business. The clients want to turn off notifications for 12 hours while they are at their regular jobs. While the app is running on another page and therefore in the foreground it is easy to intercept but I cannot figure out despite all of the excellent documentation here to do when app is closed or in the background. This is being built with the latest B4A for apps (YEGBMX and BMXTraxx) that are already live in both stores. BMXTraxx is the test app.

Any help or suggestions would be most appreciated
 

DonManfred

Expert
Licensed User
Longtime User
https://www.b4x.com/android/forum/t...-messages-firebase-cloud-messaging-fcm.67716/

This is the event which is raised.
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"), Main)
   n.Notify(1)
End Sub
Note that this event is raised even if your app is in background (the service will be started).
You are free to ignore incoming Notifications in this sub and - maybe - not to show a Notification.
 
Upvote 0

morphe2011

Member
Licensed User
Longtime User
Thank You for your reply.
That is what I thought however I cannot figure out how to cancel it before it shows the icon. My code intercepts the messages at the top of the subroutine with a simple return if notifications are turned off but still the icon shows up. I watched the log when the app was in the background or not running and it does not appear to get this far. I can post the entire subroutine if that will help

If cc.fsNotification="N" And cc.fsANotification="" Then Return

Most Thanks for your help.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
however I cannot figure out how to cancel it before it shows the icon
B4X:
Sub fm_MessageArrived (Message As RemoteMessage)
   Log("Message arrived")
   Log($"Message data: ${Message.GetData}"$)
   log("I´ll not do anything but creating this LOG-Entry")
   '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
 
Upvote 0

morphe2011

Member
Licensed User
Longtime User
That is what I thought as well. Although I rarely post here for security reasons I have been working with B4A since early 2011 and therefore have a decent understanding of how it works. In the file I've uploaded if the app is closed or in the background the log entry is never written. I have done my usual troubleshooting including putting log entries everywhere, different devices (I have a few on my desk) and building just a test app. I know the answer is simple. What did I miss?
 

Attachments

  • messarrived.txt
    1.9 KB · Views: 462
Upvote 0

morphe2011

Member
Licensed User
Longtime User
Ahh but I do need help finding the problem and hence the solution. With respect (my social skills are a little rusty like me and for that I apologize ) did you look at the file and what can you suggest?
 
Upvote 0

morphe2011

Member
Licensed User
Longtime User
Update. After a bit more troubleshooting, watching the logs and trying it on different devices, the solution that is working 100% of the time is when I check the actual messages. Erel in his sample I think called it GrabMessages. If Notifications are off then I initialize fm to "" and unsubscribe from the topic. When the user chooses to turn it back on I simply restart the service. Happy to share my code if anyone is interested
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
Important: Send data messages only (there are different types). Otherwise Android throws notifications automatically. There are 3 types: data, notification only and mixed. Search the forum. There's a thread where I have summarised the topic.
 
Upvote 0
Top