iOS Tutorial Silent Push Notifications

This tutorial explains how you can use push notifications that don't show any visual cue to the user and start your app in the background for a limited time (up to 30 seconds).
You can use it for example to download new content.

Make sure to start with standard push notifications: https://www.b4x.com/android/forum/t...ions-push-messages-server-not-required.68645/

1. Add the remote notification background mode:
B4X:
#PlistExtra: <key>UIBackgroundModes</key><array><string>remote-notification</string></array>

2. When a remote notification arrives the Application_RemoteNotification event will be raised. You have up to 30 seconds to do whatever your app needs to and then call CompletionHandler.Complete.

3. Sending code: remove the notification element and add the content_available field:
B4X:
If Topic.StartsWith("ios_") Then
   Dim iosalert As Map =  CreateMap("title": Title, "body": Body, "sound": "default")
   'm.Put("notification", iosalert) 'comment this line
   m.Put("priority", 10)
   m.Put("content_available", True) '<--- add
End If
Note that you can add other fields to 'data' map.

Testing

iOS will not start the app automatically if the user has killed it. This makes it a bit difficult to test that it really works.
In order to test it you need to compile it in Release mode and run it once.
Now install the app again and do not start it. Send a silent push message and the process should start.
The app will not be visible. You can show a local notification to see that it is working.
 
Last edited:

marcick

Well-Known Member
Licensed User
I used the source provided and placed the main.m file in the "Special" folder.

This is not related to your problem, but note that with that file in the special folder the app will be rejected when trying to publish it to the store
 

marcick

Well-Known Member
Licensed User
I'm updated, but last week I was publishing a new beta and it was rejected. I had to remove that file that I forgot in the special folder when I was doing experiments with silent notifications.
 
Top