iOS Tutorial Push notifications

New: https://www.b4x.com/android/forum/t...ions-push-messages-server-not-required.68645/

Old, don't use:
B4i v1.50 added support for push notifications. Push notifications feature requires some configuration.
Patience is a virtue ;)

The following steps are required inside Apple developer console:
1. Create a new explicit (non-wildcard) App ID with the package name of the push app. For example anywheresoftware.b4i.push. Enable push notification service.
2. Create an Apple Push Notification SSL certificate. Use the same certSigningRequest.csr file that you previously created.
3. Create a provision file with the new App ID.

Note that you can always delete all certificates and provisions and start from scratch.

The last step is done in the IDE by clicking on Tools - Build Server - Create Push Keystore. This sends the required files to the Mac builder which then creates a file named push.keystore. This file will be used by the B4X Push Server to connect to Apple service.

Make sure to switch to HD by clicking on the small gear button:


At the end of the process the key folder should look like:

SS-2014-12-24_11.45.12.png


Code

Whenever the app starts we need to register the app for remote notifications. This is done with this code:
B4X:
App.RegisterUserNotifications(True, True, True) 'allow badge, sound and alert
App.RegisterForRemoteNotifications

The PushToken event will be raised with the device token. We then send the token to the push server:
B4X:
Private Sub Application_PushToken (Success As Boolean, Token() As Byte)
   If Success Then
     Dim bc As ByteConverter
     Dim j As HttpJob
     j.Initialize("j", Me)
     j.PostString(ServerUrl & "/devicetoken", "token=" & bc.HexFromBytes(Token) & "&type=1")
   Else
     Log("Error getting token: " & LastException)
   End If
End Sub

Private Sub JobDone(j As HttpJob)
   If j.Success Then
     Log("Token uploaded successfully.")
   Else
     Log("Error uploading token")
   End If
   j.Release
End Sub

The Application_RemoteNotification event will be raised when a message arrives while the app is in the foreground. If the app is not in the foreground then a standard alert message will appear. The user can click on the message to start the app.
The Application_RemoteNotification event will be fired after the app has started.

B4X:
Private Sub Application_RemoteNotification (Message As Map, CompletionHandler As CompletionHandler)
   Log("Remote notification: " & Message)
   Dim m As Map = Message.Get("aps")
   Log(m)
   Log(m.Get("alert"))
   CompletionHandler.Complete
End Sub

Note the new signature of Application_RemoteNotification. You should call CompletionHandler.Complete once you are done with the task related to the message. This is mainly important for silent push messages, though you should always call it.

You should use the #ProvisionFile attribute to explicitly select the push.mobileprovision file.

See also:
Apple documentation: https://developer.apple.com/library...icationsPG/Chapters/CommunicatingWIthAPS.html
B4X Push Server: https://www.b4x.com/android/forum/threads/b4x-push-server.48560/
Managing multiple certificates / provision files

 
Last edited:

Rabbit

Member
Licensed User
Longtime User
Hi,

I did manage to run the push b4i and generate the token
CD32F94AAA8D47C4F210F389D97C35E5DD30717F81FCC4C248FF1D2C3ED12CE5

Would you please insert PHP code for the server? I want to echo to my device from my device this is will help me a lot.

Regards
 

walterf25

Expert
Licensed User
Longtime User
B4i v1.50 added support for push notifications. Push notifications feature requires some configuration.
Patience is a virtue ;)

The following steps are required inside Apple developer console:
1. Create a new explicit (non-wildcard) App ID with the package name of the push app. For example anywheresoftware.b4i.push. Enable push notification service.
2. Create an Apple Push Notification SSL certificate (start with Development). Use the same certSigningRequest.csr file that you previously created.
3. Create a provision file with the new App ID.

Note that you can always delete all certificates and provisions and start from scratch.

The last step is done in the IDE by clicking on Tools - Build Server - Create Push Keystore. This sends the required files to the Mac builder which then creates a file named push.keystore. This file will be used by the B4X Push Server to connect to Apple service.

Make sure to switch to HD by clicking on the small gear button:


At the end of the process the key folder should look like:

SS-2014-12-24_11.45.12.png


Code

Whenever the app starts we need to register the app for remote notifications. This is done with this code:
B4X:
App.RegisterUserNotifications(True, True, True) 'allow badge, sound and alert
App.RegisterForRemoteNotifications

The PushToken event will be raised with the device token. We then send the token to the push server:
B4X:
Private Sub Application_PushToken (Success As Boolean, Token() As Byte)
   If Success Then
     Dim bc As ByteConverter
     Dim j As HttpJob
     j.Initialize("j", Me)
     j.PostString(ServerUrl & "/devicetoken", "token=" & bc.HexFromBytes(Token) & "&type=1")
   Else
     Log("Error getting token: " & LastException)
   End If
End Sub

Private Sub JobDone(j As HttpJob)
   If j.Success Then
     Log("Token uploaded successfully.")
   Else
     Log("Error uploading token")
   End If
   j.Release
End Sub

The Application_RemoteNotification event will be raised when a message arrives while the app is in the foreground. If the app is not in the foreground then a standard alert message will appear. The user can click on the message to the start the app. The Application_RemoteNotification event will be fired after the app is started.
B4X:
Private Sub Application_RemoteNotification (Message As Map)
   Log("Remote notification: " & Message)
   Dim m As Map = Message.Get("aps")
   Log(m)
   Log(m.Get("alert"))
End Sub

You should use the new #ProvisionFile attribute to explicitly select the push.mobileprovision file.

See also:
Apple documentation: https://developer.apple.com/library...icationsPG/Chapters/CommunicatingWIthAPS.html
B4X Push Server: https://www.b4x.com/android/forum/threads/b4x-push-server.48560/
Managing multiple certificates / provision files
Very nice, i was just going to post a question about whether there was something similar in b4i to Google Cloud Messaging, i'm getting convinced more and more about buying B4i.

Thanks Erel.
Walter
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Instructions were updated to correctly raise the RemoteNotification event if the app is not running.
Example of a complete app:
B4X:
#Region  Project Attributes
   #ApplicationLabel: B4i Example
   #Version: 1.0.0
   'Orientation possible values: Portrait, LandscapeLeft, LandscapeRight and PortraitUpsideDown
   #iPhoneOrientations: Portrait, LandscapeLeft, LandscapeRight
   #iPadOrientations: Portrait, LandscapeLeft, LandscapeRight, PortraitUpsideDown
   #ProvisionFile: Push.mobileprovision
#End Region

Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'Public variables can be accessed from all modules.
   Public App As Application
   Public NavControl As NavigationController
   Private Page1 As Page
   Private Const ServerUrl As String = "http://192.168.0.6:51044"
End Sub

Private Sub Application_Start (Nav As NavigationController)
   NavControl = Nav
   Page1.Initialize("Page1")
   Page1.Title = "Page 1"
   Page1.RootPanel.Color = Colors.White
   NavControl.ShowPage(Page1)
   App.RegisterUserNotifications(True, True, True)
   App.RegisterForRemoteNotifications
   CheckForPushMessage
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_PushToken (Success As Boolean, Token() As Byte)
   If Success Then
     Dim bc As ByteConverter
     Dim j As HttpJob
     j.Initialize("j", Me)
     j.PostString(ServerUrl & "/devicetoken", "token=" & bc.HexFromBytes(Token) & "&type=1")
   Else
     Log("Error getting token: " & LastException)
   End If
End Sub

Private Sub JobDone(j As HttpJob)
   If j.Success Then
     Log("Token uploaded successfully.")
   Else
     Log("Error uploading token")
   End If
   j.Release
End Sub

Private Sub Application_RemoteNotification (Message As Map)
   Log("Remote notification: " & Message)
   Dim m As Map = Message.Get("aps")
   Log(m)
   Log(m.Get("alert"))
   Msgbox("remote: " & Message, "")
End Sub
 

Shay

Well-Known Member
Licensed User
Longtime User
I have 2 different apps
(for each I created it's own app id, certificate, provision)
do I need to create another push keystore from ide? or the same can be used for the 2 apps?
 

Shay

Well-Known Member
Licensed User
Longtime User
Thanks, but the question was if I can use the same push keystore or I need to create new one to the second app?
if new one? how can I put 2 files with same name in same directory?
 

Shay

Well-Known Member
Licensed User
Longtime User
@Erel is there a way (or can you make this) to configure paths per app and not global?
 

JanPRO

Well-Known Member
Licensed User
Longtime User
Hi,

when I call the CheckForPushMessage Sub, App.LaunchOptions.ContainsKey("UIApplicationLaunchOptionsRemoteNotificationKey") is always False, so nothing happens. Does anybody knows the answere?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can still use the same key folder. aps_x.cer and push.keystore files are not required during compilation. You only need to use them once to create the keystore and then copy the keystore to the push server (and save a backup in a different folder).
when I call the CheckForPushMessage Sub, App.LaunchOptions.ContainsKey("UIApplicationLaunchOptionsRemoteNotificationKey") is always False, so nothing happens. Does anybody knows the answere?
It will only return true in one specific case.
The questions:
1. Have you clicked on the notification alert?
2. Is RemoteNotification event raised?
 

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
Erel,

In XCODE, when you have in a project a sound (.caf, .mp3, etc), you can send a json push notifications with sound:"nameOfSounf.mp3" for example, and this sound play when notifications arrives, but in B4i I try to put in Special Folder, and Assets, but not working....

B4i needs to put this sound in another special folder?


Thanks

Alberto Iglesias
 
Top