iOS Question Show Notification while app is in the foreground

walterf25

Expert
Licensed User
Longtime User
Hi all, I have an app which I have configured with firebase push notifications, when I send a message and the app is in the background I see a physical notification pop up, but when the app is in the foreground, I don't see the notification, is it possible to display an actual notification while the app is in the foreground, I could have sworn i have done this before, or I may have been dreaming, any advice?

Thanks,
Walter
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

walterf25

Expert
Licensed User
Longtime User
You can use the new UserNotificationCenter to create a local notification from Application_RemoteNotification: UserNotificationCenter class
Such notifications will appear even when the app is in the foreground.

To prevent showing two notifications when the user clicks on the notification while the app is in the background: https://www.b4x.com/android/forum/threads/re-how-detect-user-tap-on-notification.117548/#content
Awesome, i will give this a try right away, thanks for the quick reply Erel.

Walter
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
You can use the new UserNotificationCenter to create a local notification from Application_RemoteNotification: UserNotificationCenter class
Such notifications will appear even when the app is in the foreground.

To prevent showing two notifications when the user clicks on the notification while the app is in the background: https://www.b4x.com/android/forum/threads/re-how-detect-user-tap-on-notification.117548/#content
I've tried it and it seems to work, I can now see the Notification even when the app is in the foreground, at first everything was working just great, the
Private Sub Application_RemoteNotification (Message As Map, CompletionHandler As CompletionHandler) Sub was being raised all the time, I didn't make any other changes than the ones you suggested above, now I can send a push notification and it is being received but the Sub Application_RemoteNotification Sub is not being raised anymore, any ideas what may be going on?

Thanks,
Walter
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
I decided to run some test and took out the UserNotificationCenter part out, now I can see the Application_RemoteNotification sub being raised.

So I decided to Initialize the UserNotificationCenter class once inside the Application_RemoteNotification sub, and the first time I launch the app I can see the Notification etc..

But all the consecutive app launches I start seeing the same issue again where the Application_RemoteNotfication is no longer being raised, I think the UserNotificationCenter class is causing this issue for sure, now the question is, How can I get the app to raise the Application_RemoteNotification every time even after Initializing the UserNotificationCenter class, This is specially important for me as there's some other code that needs to run inside the Application_RemoteNotification sub.

Thanks,
Walter
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
But all the consecutive app launches I start seeing the same issue again where the Application_RemoteNotfication is no longer being raised
Do you mean that it is not raised when another push notification arrives?

Best if you upload a small project with your implementation.
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Do you mean that it is not raised when another push notification arrives?
Yes, If I continue sending more notifications after the first one, then the Application_RemoteNotification Sub does not get raised anymore.
I will try to upload a small project.

Walter
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Best if you upload a small project with your implementation.
I've created a small project, and I see the same behavior, just to recap, the first time the app is launched the Application_RemoteNotification sub is raised and the localnotification pops up after 4 seconds, after this if i send a new Push notification, the firebase push notification is recieved but the Application_RemoteNotification Sub is not raised anymore, you can see this behavior on this example I created.

Thanks for looking at this.

Walter
 

Attachments

  • Example_Issue.zip
    4.5 KB · Views: 159
Upvote 0

walterf25

Expert
Licensed User
Longtime User
You are correct, however the behavior is actually better.

The push message is displayed even when your app is in the foreground. This is what you wanted, right? You don't need Application_RemoteNotification at all.
In my case I actually need the Application_RemoteNotification because there's some code that gets executed from inside this sub, for example saving the message's content inside a database and displaying those messages on a list view later on.

Walter
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Sounds like a mistake. You will lose:
- messages that arrive while the app is in the background.
- messages that are missed because of network problems.
I assume that most of the time your app is not in the foreground so most messages will be lost.

The correct solution is to check for the data with your server, once the app is started.
Only use push notifications to notify the user while the app is in the background.
 
Upvote 0
Top