Code Examples

Here are examples of how to send the subscription request from various environments once you have successfully obtained the device token.

cURL

curl -X POST https://api.pushblitz.com/subscribe \ -H "Content-Type: application/json" \ -H "X-Channel-ID: 123e4567-e89b-12d3-a456-426614174000" \ -d '{ "platform": "ios", "provider_token": "a1b2c3d4e5f6g7h8i9j0...", "attributes": { "user_language": "en" } }'

Swift (iOS Native)

Once you receive the APNS deviceToken in didRegisterForRemoteNotificationsWithDeviceToken, convert it to a hex string and send it to Pushblitz.

func registerPushblitzToken(apnsToken: String) { let url = URL(string: "https://api.pushblitz.com/subscribe")! var request = URLRequest(url: url) request.httpMethod = "POST" request.setValue("123e4567-e89b-12d3-a456-426614174000", forHTTPHeaderField: "X-Channel-ID") request.setValue("application/json", forHTTPHeaderField: "Content-Type") let payload: [String: Any] = [ "platform": "ios", "provider_token": apnsToken ] request.httpBody = try? JSONSerialization.data(withJSONObject: payload) URLSession.shared.dataTask(with: request) { data, response, error in if let error = error { print("Pushblitz registration failed: \(error)") return } print("Pushblitz registration successful!") }.resume() }

Kotlin (Android Native)

Once you retrieve the FCM token via FirebaseMessaging.getInstance().token, send it to Pushblitz.

fun registerPushblitzToken(fcmToken: String) { val url = URL("https://api.pushblitz.com/subscribe") val connection = url.openConnection() as HttpURLConnection connection.requestMethod = "POST" connection.setRequestProperty("X-Channel-ID", "123e4567-e89b-12d3-a456-426614174000") connection.setRequestProperty("Content-Type", "application/json") connection.doOutput = true val payload = """ { "platform": "android", "provider_token": "$fcmToken" } """.trimIndent() connection.outputStream.use { os -> val input = payload.toByteArray(Charsets.UTF_8) os.write(input, 0, input.size) } val responseCode = connection.responseCode println("Pushblitz Response Code: $responseCode") }

React Native (Fetch API)

If you are using a cross-platform framework, your code handles both conditionally.

const registerWithPushblitz = async (token, platform) => { // 'token' is your FCM or APNS token // 'platform' should be 'ios' or 'android' try { const response = await fetch('https://api.pushblitz.com/subscribe', { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-Channel-ID': '123e4567-e89b-12d3-a456-426614174000', }, body: JSON.stringify({ platform: platform, provider_token: token, }), }); const data = await response.json(); console.log('Pushblitz registration:', data); } catch (error) { console.error('Error registering to Pushblitz:', error); } };

Flutter (Dart)

In Flutter, after obtaining the device token via firebase_messaging or apns, you can send it to Pushblitz using the standard http package.

import 'dart:convert'; import 'package:http/http.dart' as http; Future<void> registerWithPushblitz(String token, String platform) async { // 'token' is your FCM or APNS token // 'platform' should be 'ios' or 'android' final url = Uri.parse('https://api.pushblitz.com/subscribe'); try { final response = await http.post( url, headers: { 'Content-Type': 'application/json', 'X-Channel-ID': '123e4567-e89b-12d3-a456-426614174000', }, body: jsonEncode({ 'platform': platform, 'provider_token': token, }), ); if (response.statusCode == 200 || response.statusCode == 201) { print('Pushblitz registration successful: ${response.body}'); } else { print('Pushblitz registration failed: ${response.statusCode} - ${response.body}'); } } catch (e) { print('Error registering to Pushblitz: $e'); } }