B4J Tutorial Creating push-enabled android-apps on blackberry

I would like to share my experience with creating push-enabled android-apps for blackberry-devices. Many thanks to Erel, who supports my efforts decisively.

I have tried a lot of things to enable the google play-services on blackberry-devices for using the GCM, but I was not able to register a device for google push. The only way to push an android-app on a blackberry-device is to use the blackberry-push service. In the following steps, I made my messenger-app blackberry-compatible. While using the apps only in our company, I don’t know, if these apps are accepted by the blackberry-world. I didn’t tested it.


Client-side:

1.
First, you have to order a blackberry key for developer here:
https://www.blackberry.com/SignedKeys/codesigning.html


Fill out the personal information and click also on

“For Blackberry 10 apps developed using Blackberry 10 SDK 10.2 or higher……”


Note, that your Email-address in the personal information must be the same as the address in the second step for the blackberry push-account!

2.
After that, you have to create an account to the blackberry push service. There are two types of accounts:

An account for a test environment:
https://www.blackberry.com/profile/?eventId=8121

And an account for using your app in production:
https://www.blackberry.com/profile/?eventId=8207

I used the first link for testing. In the application description, I wrote 4 or 5 sentences and the push type I chooses was “Blackberry Push Essentials”. The different to “Push Plus” is, that there is no status request and it is a free service.

After that, you will get a mail from Blackberry with a csk-file, which is the your developer Blackberry-ID and a mail with your private push credentials. The ID is valid for one year. It could take up to 5 days until you get the mail with your push credentials.

3.
Now, we make our android-push app valid for registration to the blackberry-push service. The construction of the push-enabled android-app and the server-side is the same as:
https://www.b4x.com/android/forum/threads/b4x-push-server.48560/

It is no need to change the code of a push-enabled android-app. Everything could be done from “outside”. It is only necessary to later adjust the server-code.

We now have to generate a simple android.cfg with a simple text editor:

B4X:
<?xml version="1.0" encoding="utf-8"?> <android>
  <push>
  <appid>XXXX-546353vcgsfsre46424M988518o8645vdgdfd64</appid>
  <ppgurl>http://cpXXXX.pushapi.eval.blackberry.com</ppgurl>
  <tokenprefix>bb:</tokenprefix>
  </push>
</android>

Copy the text into the editor and set your private credentials of the blackberry-email (Client Configuration).

You can change the token prefix in every string you want or you could set nothing. The token prefix will preceding to the device token after registration the device to the blackberry push service. Every blackberry-token is only eight characters long. So you can differentiate a returning token between android (for example: APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx) and a blackberry token (for example: 31D54A54) with the token length or you use the prefix like I do. For example bb:31D54A54

Save the file under android.cfg.

4.
Now you need to download the Android Studio Plugin:
https://developer.blackberry.com/android/tools/

We need the Blackberry APK Packager to configure the android-apk-file. Start Blackberry APK Packager and configure the packager like I did:

bild.png


After pressing “Package”, you need to set the “Autor-Detect”. Here, you have to choose the csk-file, which was send to you in one of the blackberry emails. Note, that you have to confirm the file with your password.

In some descriptions out of the internet, there is told, that it is necessary to configure the manifest-file to enable the blackberry device for push mails. I’m using a Blackberry Leap with OS 10.3 and there is no need to configure the manifest. You could let the manifest-file be as under android.

After that, we have the ready and signed BAR-File, which is now able to register the device to blackberry push service.

I think, that it is possible, to change the bar-file again to apk, but I don’t tested it, because these tools are always online and I don’t want to upload my app.

5.
To install the BAR-File to my blackberry, I uses the DDPB-Tool from here:
http://macberry.de/blackberry/faq/w...aybook-erhalt-ein-update-auf-v1-09-bug-fixes/

Now, you could test if the blackberry could register to the blackberry-push service.

6.
To test if the device will be pushed, you could use a simple poster add-on from google chrome or the HttpRequester from firefox.

Set the following configuration with your blackberry credentials and POST this to the blackberry-server. The authorization is a string, which is configured as followed:

B4X:
Private BLACKBERRY_PUSH_APPLICATION_ID As String ="XXXX-546353vcgsfsre46424M988518o8645vdgdfd64"
Private BLACKBERRY_PUSH_PASSWORD As String = "VGsRTfA5"
Dim b64 As Base64
Dim Login As String
Login=BLACKBERRY_PUSH_APPLICATION_ID & ":" & BLACKBERRY_PUSH_PASSWORD
Login=”Basic “ & b64.EncodeStoS(Login, "UTF8")

URL:
https://cpXXXX.pushapi.eval.blackberry.com/mss/PD_pushRequest

Header:
Authorization:
Basic GHHFDFDFDBBdh6645grjRNOTg4NTE4bzhpMDQyMDVsN656565QWERRFFRZOVnJONg==

Content-Type:
multipart/related; boundary=PMasdfglkjhqwert;type=application/xml

Request:
B4X:
--PMasdfglkjhqwert
Content-Type: application/xml; charset=UTF-8
<?xml version="1.0"?>
<!DOCTYPE pap PUBLIC "-//WAPFORUM//DTD PAP 2.1//EN" "http://www.openmobilealliance.org/tech/DTD/pap_2.1.dtd">
<pap>
<push-message push-id="999999999"  source-reference="XXXX-546353vcgsfsre46424M988518o8645vdgdfd64"  deliver-before-timestamp="2020-12-31T23:59:59Z">    <address address-value="3E4D4545"/>
<quality-of-service delivery-method="confirmed"/>
</push-message>
</pap>
--PMasdfglkjhqwert

{"key":"value"}
--PMasdfglkjhqwert–-


You now have to get a push-message on your device with the token under address-value.

Caution: you have to copy the lines above to the request-field with all spaces and CRLF, otherwise the message is not accepted.

Server-side:

1.
If you are using something like Erel’s B4X Push Server, you could change the handler-Class “Send” into:

B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
    If req.GetParameter("password") <> Main.config.Get("PushServerPassword") Then
        resp.SendError(500, "Invalid password")
        Return
    End If
    Dim m As Message
    m.Initialize
    If IsNumber(req.GetParameter("badge")) Then
        m.Badge = req.GetParameter("badge")
    End If
    m.Text = req.GetParameter("text")
    m.Sound = True
    m.HighPriority = True
    m.ExpirationDate = DateTime.Now + DateTime.TicksPerDay
    Dim rows As List = DBUtils.ExecuteMemoryTable(Main.db, _
        "SELECT token, type FROM tokens WHERE updated_time > ?", _
        Array As String (DateTime.Now - DAYS_LIMIT * DateTime.TicksPerDay), 0)
    Dim iosTokens, AndroidTokens, BlackberryTokens As List
    iosTokens.Initialize
    AndroidTokens.Initialize
    BlackberryTokens.Initialize
    For Each row() As String In rows
        If row(1) = Main.TYPE_IOS Then
            iosTokens.Add(row(0))
            If iosTokens.Size > 900 Then
                CallSubDelayed3(iOSPush, "SendMessageTo", iosTokens, m)
                Dim iosTokens As List
                iosTokens.Initialize
            End If
        End If
        If row(1) = Main.TYPE_ANDROID Then
            AndroidTokens.Add(row(0))
            If AndroidTokens.Size > 900 Then
                CallSubDelayed3(AndroidPush, "SendMessageTo", AndroidTokens, m)
                Dim AndroidTokens As List
                AndroidTokens.Initialize
            End If
        End If
        If row(1) = Main.TYPE_Blackberry Then
            BlackberryTokens.Add(row(0))
            If BlackberryTokens.Size > 900 Then
                CallSubDelayed3(BlackberryPush, "SendMessageTo", BlackberryTokens, m)
                Dim BlackberryTokens As List
                BlackberryTokens.Initialize
            End If
        End If
    Next
    If iosTokens.Size > 0 Then CallSubDelayed3(iOSPush, "SendMessageTo", iosTokens, m)
    If AndroidTokens.Size > 0 Then CallSubDelayed3(AndroidPush, "SendMessageTo", AndroidTokens, m)
    If BlackberryTokens.Size > 0 Then CallSubDelayed3(BlackberryPush, "SendMessageTo", BlackberryTokens, m)

    resp.Write("Message sent to " & rows.Size & " device(s).")
End Sub

2.
And then add a code modul with the name “BlackberryPush” in such a way:

B4X:
Sub Process_Globals

End Sub

Public Sub SendMessageTo(Devices As List, msg As Map)
    Dim job As HttpJobBlackberry
    job.Initialize("BlackberryPush", Me)
    job.PostMultipartXML(Devices, msg)
End Sub

Private Sub JobDone(job As HttpJobBlackberry)
    If job.Success=False Then
        Log("Fehler Blackberry Push!")
    Else
        Log("Blackberry Push erfolgreich!")
        Log(job.GetString)
    End If
    job.Release
End Sub

3.
To send the message to the blackberry-server, you could do that in xml-format. To send xml, you have to use a multipart-post, based on jhttp. Tests with okhttp has shown, that the okhttp library rejects the content-type, because it considered it to be invalid. Here a sample-sub:

B4X:
public Sub PostMultipartXML(Link As String)
    Dim Absender As String = "Blackberry"
    Dim Filename As String = "test.txt"
    Dim Daten As String = "test"
    Dim Zeitstempel As Long = 1452172482568
    Dim Groesse As Int = 1234

    Dim b() As Byte
    Dim eol As String = Chr(13) & Chr(10)

        Dim s As String = _
$"--PMasdfglkjhqwert
Content-Type: application/xml; charset=UTF-8
<?xml version="1.0"?>
<!DOCTYPE pap PUBLIC "-//WAPFORUM//DTD PAP 2.1//EN" "http://www.openmobilealliance.org/tech/DTD/pap_2.1.dtd">
<pap>
  <push-message push-id="999999999"  source-reference="XXXX-546353vcgsfsre46424M988518o8645vdgdfd64"  deliver-before-timestamp="2020-12-31T23:59:59Z">    <address address-value="3E4D4545"/>
    <quality-of-service delivery-method="confirmed"/>
  </push-message>
</pap>
--PMasdfglkjhqwert

{"Absender":"${Absender}", "Filename":"${Filename}", "Daten":"${Daten}", "Zeitstempel":"${Zeitstempel}", "Groesse":"${Groesse}"}
--PMasdfglkjhqwert–-"$

    b = s.Replace(CRLF, eol).GetBytes("UTF8")
    PostBytes(Link, b)
    req.SetContentType("multipart/related;boundary=PMasdfglkjhqwert;type=application/xml")
    req.SetHeader("Authorization", "Basic GHHFDFDFDBBdh6645grjRNOTg4NTE4bzhpMDQyMDVsN656565QWERRFFRZOVnJONg==")
End Sub

Note, that you can send up to 8KB over the blackberry-push service!

To send a push-message to multiple devices, you can configure the address-string as followed:
B4X:
Dim Device As List
Device.Initialize
Device.Add("7F45DE67")
Device.Add("5RE33D45")
Dim Devicesstring As String
For i=0 To Device.Size-1
     Devicesstring=Devicesstring & "<address address-value='" & Device.Get(i) & "'/>"
Next

and change the line in the string s to

B4X:
<push-message push-id="999999999"  source-reference="XXXX-546353vcgsfsre46424M988518o8645vdgdfd64"  deliver-before-timestamp="2020-12-31T23:59:59Z">    ${Devicesstring}

It is always possible to send a push-message to all devices, which are registered under your account with

B4X:
<address address-value="push_all"/>

In this way, it is no need to program an elaborate adress-database, because the blackberry service will manage that for you.

Not to renounce the okhttp-lib, I made a small project, which uses only for the blackberry-push the multipart-post based on jhttp. You can use it parallel to okttp. Set in your own blackberry-credentials and it should work.

Hope, that someone could use it…
 

Attachments

  • BlackberryPushServercode.zip
    4.5 KB · Views: 390
Last edited:

schimanski

Well-Known Member
Licensed User
Longtime User
I have to give some additional informations to this tutorial because I had some problems with the latest version of my app:

With the newer versions of my app, it was not possible to register my devices to the blackberry-push-service. I don't know why, but I found out, that the two new lines in the GCM-register-code of the 'RegisterDevice'-sub seems to be the problem. I isolate them in the following way:

B4X:
#if ANDROID
        Dim jo As JavaObject = i
        jo.RunMethod("setPackage", Array("com.google.android.gms"))
#end if

After that, registration was possible again.

I also made now a production-push-ID account under

https://www.blackberry.com/profile/?eventId=8207

because it is valid for 10 years and it works without problems. When register the production-push-account, you have to set an App-ID. I set the App-ID which I got from blackberry after register the push-developer-account in my credentials. So i think, that you first have to get a developer-account before apply a production-push-account.

Under point 5 of the client-side above, i told you to use the blackberry DDPB-Tool to install the bar-file to your blackberry-device. It is more simple to enable 'Deploy' in the blackberry-apk-packager. After enabling the developer-mode in the settings/security, the apk packager can install the bar-file straight on your phone.
 
Top