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:

fbritop

Active Member
Licensed User
Longtime User
Erel,
Are there any "special" settings to be done when uploading for production?

My app uploads OK, but late after I receive an email that states:

Missing Push Notification Entitlement - Your app appears to include API used to register with the Apple Push Notification service, but the app signature's entitlements do not include the "aps-environment" entitlement.

Not sure if I'm missing something. It did work for development.

Thanks
FBP
 

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
Hey Erel,

Some questions:

1) When "App.IsInitialized" turn to TRUE?

2) After this instructions:

App.RegisterUserNotifications(True, True, True)
App.RegisterForRemoteNotifications

the Iphone should be appear the message to accept Notifications by the user, right? not working!!!

Now I rebuild all of certifieds, aps and profile, and generate the PUSH STORE by B4i IDE and aparently is OK.

I got the APP token through "Application_PushToken" aparently is OK.

But when I try to send a PUSH message not work.


I do another app and work, but now its not working.


I think the problem is in the first step, NOT APPEAR A MESSAGE FOR ACCEPT NOTIFICATIONS, why?


Thanks

Alberto Iglesias
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Application is a special type. You don't need to initialize it.

2. What is not working? It should show a message on the first time.

I think the problem is in the first step, NOT APPEAR A MESSAGE FOR ACCEPT NOTIFICATIONS, why?
You can check or set the permission in the Settings app.
 

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
In the settings OK:
push6.png


Look my certifies:
push1.png


My APP ID
push2.png

push3.png



The log in B4i, retrieving the TOKEN from apple servers

push4.png


I generate the pusk.keystore and use in PUSHSERVER B4J

push5.png

The APP compile OK, aparently is all OK, but not works!

Where I do something wrong?


For gererate the certifies i dont use MAC for nothing, only in windows and B4i, im a right? for create a .CSR, etc
 

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
Yes.

Can you post the push server logs?

Before I create the certifieds, I delete all logs, and the recreate when push, look in attachment.

I dont see where I´m doing wrong
I try with sandbox and production servers

iGateway=gateway.sandbox.push.apple.com
#iGateway=gateway.push.apple.com
 

Attachments

  • logs.zip
    2.6 KB · Views: 700

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
I try in these phones: iPhone 5S, iPhone 6 and iPhone 6 PLUS and still not working!

Can be my B4iBuilder? I have a 1.50.... Do you have new version?

Have a correct sequence for create steps to push work?

My steps:

1: [In B4i] Create a project in B4i with package name and save-it
2: [In B4i] Create a Private Sign Key to generate: B4i.keystore, B4i.p12 and certSigningRequest.csr (I use only this last one)
3: [In Apple Member Center] Generate an iOS Certificates (development or production) using the certSigningRequest.csr
4: [In Apple Member Center] Generate the APP ID with push notification (based on package name)
5: [In Apple Member Center] Generate the APNS certificate (based on package name)
6: [In Apple Member Center] Generate the profile
7: [In B4i] Create Push Keytore
8: [in PUSH SERVER B4J] Send Push based on this push keystore
 

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
Erel,

In my log appear all ok, but still not receveing the push. I think i create the certificates more than 30 times.

Can you try to send to me? Look this token

http://locahost:51044/devicetoken?t...DC13D019FD367A7A88E3EAFC806EE761E2187C&type=1

and result when try to send

Qtde of rows: 1
Token: EED9B135CDAD3C760D1A2D31F5DC13D019FD367A7A88E3EAFC806EE761E2187C
Message sent to 1 device(s).


I put the push.keystore attachment in this.. Can you please try to send to me a simple push?

In CONFIG.TXT i use this:
iPushKeystore=c:/temp/push.keystore
iPushKeystorePassword=batata
iGateway=gateway.push.apple.com

Thanks

Alberto
 

Attachments

  • push.zip
    3.3 KB · Views: 762

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
But I can test with distribution server no? My certificates i try with sandbox and distribution and both not work
I try with gateway.sandbox.push.apple.com and gateway.push.apple.com

The PushServer can be installed in anywhere no? And the only needed files to work should be push.keystore and password, is correct?

I ask you for do a simple test to send a message to me, with this token and keystore, to you can look the terminal/log messages and find my error.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
But I can test with distribution server no?
No. During development you can only use the sandbox with development APN ssl. Only when you release the app to the app store you will switch to non-sandbox.

Run the server from B4J IDE. With your store file it immediately disconnects (see the terminated message):

SS-2015-02-17_09.31.00.png


It is probably a distribution APN instead of development.
 

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
Erel,

I deleted all certificates, and gerenate again with Development environment, but still not working!


Look:

Now I used this configurations:

iPushKeystore=c:/temp/push.keystore
iPushKeystorePassword=batata
iGateway=gateway.sandbox.push.apple.com
iGateWayPort=2195


I put the push.keystore now with development certificates, can you HELPPP MEEEEEEEEE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! AHHHHHHHHHHHHH

LOG

2015-02-17 13:38:31.078:INFO::main: Logging initialized @1152ms
2015-02-17 13:38:31.500:INFO:eek:ejs.Server:main: jetty-9.1.z-SNAPSHOT
2015-02-17 13:38:31.609:WARN:eek:ejh.MimeTypes:main: java.util.MissingResourceExcep
tion: Can't find bundle for base name org/eclipse/jetty/http/encoding, locale pt
_BR
2015-02-17 13:38:31.671:INFO:eek:ejsh.ContextHandler:main: Started o.e.j.s.ServletC
ontextHandler@1c6a36e{/,file:/C:/Aplic/PushServer/www,AVAILABLE}
2015-02-17 13:38:31.687:INFO:eek:ejs.AbstractNCSARequestLog:main: Opened C:\Aplic\P
ushServer\logs\b4j-2015_02_17.request.log
2015-02-17 13:38:32.125:INFO:eek:ejs.ServerConnector:main: Started ServerConnector@
1c4c61e{HTTP/1.1}{0.0.0.0:51044}
2015-02-17 13:38:32.125:INFO:eek:ejs.Server:main: Started @2347ms
server version: 0.9
server is listening on port: 51044
Socket connected.
Token added: 3AC37F558C0B6BE4A77C29F45FBE6BA36A641CCF9B8E4491F060F385691BCBC9
send message to
send message to => EED9B135CDAD3C760D1A2D31F5DC13D019FD367A7A88E3EAFC806EE761E21
87C
JSONGenerator:{"aps":{"badge":12,"alert":"testemsg","sound":"default"}}
send message to => 3AC37F558C0B6BE4A77C29F45FBE6BA36A641CCF9B8E4491F060F385691BC
BC9

JSONGenerator:{"aps":{"badge":12,"alert":"testemsg","sound":"default"}}
status=8
terminated
Trying to reconnect...
Socket connected.
 

Attachments

  • pushDevelopment.zip
    3.3 KB · Views: 707

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
I don´t know if this information help you to identify the issue:

Normally, when you install an app and pass by the lines:

App.RegisterUserNotifications(True, True, True) 'allow badge, sound and alert
App.RegisterForRemoteNotifications

should be appear a message to accept the notifications, is correct? But not appearing!

I think this problem is before the push server. Can be with b4iBuilder? I using a 1.50 version of B4iBuilder.
 

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
Erel,

2 problems solved:

  • The PUSH Server is always disconnect for some reason unknown, but I create a PUSH iOS Tool and works very well, look in
https://www.b4x.com/android/forum/threads/push-notifications-with-new-tool.50830



  • For the Notification Alert in APP first time launch appear again, have a tip in a APPLE FORUM in a
    Troubleshooting Push Notifications, look in:
https://developer.apple.com/library/ios/technotes/tn2265/_index.html

Resetting the Push Notifications Permissions Alert on iOS

  • The first time a push-enabled app registers for push notifications, iOS asks the user if they wish to receive notifications for that app. Once the user has responded to this alert it is not presented again unless the device is restored or the app has been uninstalled for at least a day.

    If you want to simulate a first-time run of your app, you can leave the app uninstalled for a day. You can achieve the latter without actually waiting a day by following these steps:
    1. Delete your app from the device.

    2. Turn the device off completely and turn it back on.

    3. Go to Settings > General > Date & Time and set the date ahead a day or more.

    4. Turn the device off completely again and turn it back on.
 
Top