iOS Tutorial Push notifications

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:

tufanv

Expert
Licensed User
You don't need to change the password. You do need to create a new keystore file with the non-development APN certificate.

But to create a new push keystore with non development cerificate i have to change my private sign key or i can not build the ipa and if i change it dont i need to change to password to new one in config.txt ?

Another question: to create a new keystore file with non decelopment apn cert, i have to just change the build location to store files jist like creating a build for distrbution and create push keystore with those settings? ( provision and cert for distrubition which also contains the distrubition apn cert ) ?
 

tufanv

Expert
Licensed User
I'm not sure that I understand the issue. You can use the same csr file for as many apps as you like.

You can also use a different csr file if you prefer. Just change the keys folder in the IDE.

What I mean is , For development version of my app , i can successfuly send push notifications. Now i will create a production build to put to apstore. But my csr files are not same so i use different private sign keys for development and distrubution.

I created the push keystore file with development prov. & cert. and with development password: lets say pass1 . In config.txt of pushserver I used the pass1 for push keystore password and the push.keystore file generated with development files (development cert. and development provisining file ). Works perfect.

Now i want to switch to distrbution. are these correct :
1) Change the location to keys folder to distrubiton folder ( where certificate and prov. for appstore build exists)
2) change private sign key ( i have to change because i used different csrs )
3) create another push.keystore file .
4) change the config.txt to new privatge sign key
5) change the push.keystore on my server to new push.keystore (which i created for distrubiton )
6) Restart the server

With these steps, my server will be running for the distribution build without problems ?
 

tufanv

Expert
Licensed User
Why are the csr files not the same?
I dont know. One of them is 968 bayts other one is 960 bytes. I followed the tutorial when i first purchased b4i i dont know why they are diiferent or remember =)
 

le_toubib

Active Member
Licensed User
i need the server to be hosted (and dont have a vps so cant use b4j) , i googled for a php script that acts as a push server and it requires the apns.pem file instead of the push.keystore file
 

Juan Marrero

Active Member
Licensed User
Do I have to create a new provisionprofile for using push notification? Or can I use the same provisionprofile I have (not default, mine is DevelPRUSB.mobileprovision) which says that it have Push Notification enabled?
 

Juan Marrero

Active Member
Licensed User
That means that if I created a provisionprofile like this "com.something.*" but when I added the app to itunesconnect and forced me to use "com.something.myapp" it will not work because of the already wildcard provision profile created?

EDIT: I created a new App ID (non-wildcard), a new Certificate and a new ProvisionProfile and I can't get it to work.
 
Last edited:

marcick

Well-Known Member
Licensed User
All files from the "special folder" are copied to the main bundle folder (where the sounds should be). You can see it in the build server inside the project folder.

I'm trying to obtain a custom sound for a push notification.
I have put in the special folder the file "test.mp3" and compiling the app I see it in the build server.
Then, in B4J pushserver, Sub SendMessageTo, IOSPush module, I have changed from

B4X:
If msg.sound then m.put("sound", "default")

to

B4X:
If msg.sound then m.put("sound", "test.mp3")

But it doesn't work

What's wrong ?
 
Top