Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

Convert APNS Tokens To FCM Tokens With Simple HTTP APIs

TwitterFacebookRedditLinkedInHacker News

Not too long ago I wrote a guest post regarding push notifications with the Firebase Cloud Messaging (FCM) service at the Telerik Developer Network. While that previous article was directly related to using FCM within a NativeScript application, it followed all the same rules that are required for any mobile development platform. There is a catch, however. The push notification plugin for NativeScript uses FCM for Android and Apple’s Push Notification Service (APNS) for iOS, which can be confusing as you’re then working with two different services.

It often makes sense to use a service like Firebase Cloud Messaging for both Android and iOS to make the application a little less complex. We’re going to see how to convert APNS tokens into FCM tokens using some available Google APIs and HTTP.

Before we worry about doing any conversions, I want to clear some things up. APNS tokens can be generated using any development platform, not strictly NativeScript. A lot of times, these tokens exist as part of legacy applications that have not been switched over to cloud platforms like Amazon, Firebase, or other. This creates a perfect opportunity to convert them.

At this point, assume that you have already registered a device and have an APNS token. While we can use this token with the Apple documentation to send push notifications, we’ll get errors when trying to send it with Firebase Cloud Messaging.

Instead, we need to construct an HTTP request against the Google APIs. Realistically, you’ll probably want to write a script that can send HTTP requests, but for this example we’re going to use cURL.

We need to send the following cURL request:

curl -X POST -H "Authorization: key=YOUR_FCM_SERVER_KEY" -H "Content-Type: application/json" -d '{
    "application": "com.nraboy.nativescriptexample",
    "sandbox":false,
    "apns_tokens":[
        "368dde283db539abc4a6419b1795b6131194703b816e4f624ffa12",
        "76b39c2b2ceaadee8400b8868c2f45325ab9831c1998ed70859d86"
    ]
}' "https://iid.googleapis.com/iid/v1:batchImport"

In the above request, take note of a few things. To be successful with the API, you’ll need to use your Firebase Cloud Messaging API token within the authorization header of the request. Within the Firebase console, there are several tokens to choose from. You’ll need to use the Server Key, which can be found in the Cloud Messaging tab of the settings.

Next, you’ll need to be using the correct application package name. When you registered with Firebase, you should have added your application package id. Make sure to use the correct package, otherwise you’ll get errors.

If you’re trying to send test push notifications using a development certificate generated with Apple, you’ll need to convert the tokens using the sandbox. Otherwise, if you’re in production, disable the sandbox.

Finally, make sure you’re providing APNS tokens. Google allows you to batch up to 100 tokens in a single request. The response should look something like this:

{
    "results":[
        {
            "apns_token": "368dde283db539abc4a6419b1795b6131194703b816e4f624ffa12",
            "status": "OK",
            "registration_token":"nKctODamlM4:CKrh_PC8kIb7O...clJONHoA"
        },
        {
            "apns_token": "76b39c2b2ceaadee8400b8868c2f45325ab9831c1998ed70859d86",
            "status":"Internal Server Error"
        },
    ]
}

For every correct APNS token passed, you’ll get a response with a registration_token that represents the Firebase Cloud Messaging token. Correctness is validated based on if the original token was generated using the same application package as well as other things.

If you plan to send push notifications to Android and iOS, make sure you’re using the FCM token with the Firebase APIs.

Conclusion

You just saw how to convert your Apple Push Notification Service (APNS) tokens into Firebase Cloud Messaging tokens which can then be used with Firebase. Converting from APNS can be a good thing because then Android and iOS push notifications can be sent using the same APIs.

Realistically, APNS tokens should be converted via a script. For example, an iOS device registers for an APNS token, that token is sent from device to your own API server, the API server requests a conversion to FCM, and then the FCM token is sent back to the device all in the same request. The resulting FCM token would also be saved on your server so that way messaging can happen.

Nic Raboy

Nic Raboy

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in C#, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Unity. Nic writes about his development experiences related to making web and mobile development easier to understand.