Concepts¶
What every PlingBuddy SDK works with, whatever the platform.
Application and environments¶
Your app is an application on PlingBuddy. You create it in the dashboard, under your organization, and add its connections to Apple and Firebase there. Its application id, shown on the application's settings page, is what you pass to the SDK when it starts. Getting started walks through it.
The service runs in two environments, each with its own applications:
- Production,
https://app.plingbuddy.com/, which the SDKs use by default. - Staging,
https://staging.plingbuddy.com/, for trying your app before release, with accounts and applications of its own and access on request (see Getting started). Pass the SDK's staging constant (PlingBuddy.stagingUrlin Swift and Dart,PlingBuddy.STAGING_URLin Kotlin) with the application id from staging.
Switching an install of your app between the two starts a new installation on the other one.
Recipients and users¶
A recipient is a person who gets your app's notifications. Each of their devices belongs to one recipient.
- A device starts with an anonymous recipient, so the SDK works without users.
- When the user signs in, you tell the SDK their external id: your own id for the user, such as the id in your database. The device then belongs to that user's recipient, together with their other devices.
- When they sign out, the device moves to a new anonymous recipient, so the previous user no longer gets its notifications.
The SDK remembers the user across launches. Recipients carry optional data (email, country, language, time zone, metadata), tags and the notification preference, which the dashboard can use.
Installations¶
Each install of your app is an installation on the service, with an id and a secret the SDK makes on the device. It holds the device's push token, whether notifications are allowed, the app and OS versions, and device properties you set. Deleting and reinstalling the app makes a new installation.
Every call is saved first¶
Every SDK call returns as soon as its change is saved on the device. The SDK sends it to the service itself, combines changes made together into one request, and sends again later when the service can't be reached, also after a relaunch. You never wait for the network, and nothing is lost offline.
The exceptions are deleting the user, which answers how its first attempt ended, and calls made while a deletion is pending: they wait in memory until it ends and aren't saved, so a relaunch before then loses them. Each platform's Deleting the user page has the details.
Notification preference¶
Users can choose whether they want all notifications or only the required ones (receipts, security alerts and the like). Campaigns are marked required or promotional in the dashboard, and a promotional campaign leaves out every user who chose required only. A user the service was never told about gets all notifications.
Identity verification¶
By default, the SDK names the signed-in user by their external id alone. An application can turn on identity verification: then every request for a signed-in user needs an identity token proving the device's user is that user. Anonymous users, and signing out, need none.
Your own backend makes the token after the user signed in there, and the app hands it to the SDK. It's a JSON Web Token signed with ES256, with these claims:
| Claim | Value |
|---|---|
iss |
Your application id |
sub |
The user's external id |
exp |
When it expires. Keep tokens short-lived. |
It has no aud claim, and may name the signing key in its header as kid.
Your backend signs it with the identity key's private key, which the dashboard gives you as a PEM file. In most languages it's a few lines with a common JWT library. Each example reads the key and your application id from environment variables and makes a token whose exp is 15 minutes away; the service accepts it for 30 minutes after that, since a call the SDK queued may arrive late, so it's usable for up to 45 minutes:
import { SignJWT, importPKCS8 } from "jose";
const privateKey = await importPKCS8(process.env.PLINGBUDDY_IDENTITY_KEY, "ES256");
export function plingBuddyToken(externalId) {
return new SignJWT({})
.setProtectedHeader({ alg: "ES256" })
.setIssuer(process.env.PLINGBUDDY_APPLICATION_ID)
.setSubject(externalId)
.setExpirationTime("15m")
.sign(privateKey);
}
import os
import time
import jwt
def plingbuddy_token(external_id: str) -> str:
claims = {
"iss": os.environ["PLINGBUDDY_APPLICATION_ID"],
"sub": external_id,
"exp": int(time.time()) + 15 * 60,
}
return jwt.encode(claims, os.environ["PLINGBUDDY_IDENTITY_KEY"], algorithm="ES256")
require "jwt"
require "openssl"
PRIVATE_KEY = OpenSSL::PKey.read(ENV.fetch("PLINGBUDDY_IDENTITY_KEY"))
def plingbuddy_token(external_id)
claims = {
iss: ENV.fetch("PLINGBUDDY_APPLICATION_ID"),
sub: external_id,
exp: Time.now.to_i + 15 * 60,
}
JWT.encode(claims, PRIVATE_KEY, "ES256")
end
<?php
use Firebase\JWT\JWT;
function plingBuddyToken(string $externalId): string
{
$claims = [
'iss' => getenv('PLINGBUDDY_APPLICATION_ID'),
'sub' => $externalId,
'exp' => time() + 15 * 60,
];
return JWT::encode($claims, getenv('PLINGBUDDY_IDENTITY_KEY'), 'ES256');
}
import (
"os"
"time"
"github.com/golang-jwt/jwt/v5"
)
func PlingBuddyToken(externalID string) (string, error) {
privateKey, err := jwt.ParseECPrivateKeyFromPEM([]byte(os.Getenv("PLINGBUDDY_IDENTITY_KEY")))
if err != nil {
return "", err
}
claims := jwt.RegisteredClaims{
Issuer: os.Getenv("PLINGBUDDY_APPLICATION_ID"),
Subject: externalID,
ExpiresAt: jwt.NewNumericDate(time.Now().Add(15 * time.Minute)),
}
return jwt.NewWithClaims(jwt.SigningMethodES256, claims).SignedString(privateKey)
}
import com.nimbusds.jose.JWSAlgorithm;
import com.nimbusds.jose.JWSHeader;
import com.nimbusds.jose.crypto.ECDSASigner;
import com.nimbusds.jwt.JWTClaimsSet;
import com.nimbusds.jwt.SignedJWT;
import java.security.KeyFactory;
import java.security.interfaces.ECPrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.time.Instant;
import java.util.Base64;
import java.util.Date;
public static String plingBuddyToken(String externalId) throws Exception {
String pem = System.getenv("PLINGBUDDY_IDENTITY_KEY")
.replace("-----BEGIN PRIVATE KEY-----", "")
.replace("-----END PRIVATE KEY-----", "")
.replaceAll("\\s", "");
ECPrivateKey privateKey = (ECPrivateKey) KeyFactory.getInstance("EC")
.generatePrivate(new PKCS8EncodedKeySpec(Base64.getDecoder().decode(pem)));
JWTClaimsSet claims = new JWTClaimsSet.Builder()
.issuer(System.getenv("PLINGBUDDY_APPLICATION_ID"))
.subject(externalId)
.expirationTime(Date.from(Instant.now().plusSeconds(15 * 60)))
.build();
SignedJWT token = new SignedJWT(new JWSHeader(JWSAlgorithm.ES256), claims);
token.sign(new ECDSASigner(privateKey));
return token.serialize();
}
using System.Security.Cryptography;
using Microsoft.IdentityModel.JsonWebTokens;
using Microsoft.IdentityModel.Tokens;
static string PlingBuddyToken(string externalId)
{
var ecdsa = ECDsa.Create();
ecdsa.ImportFromPem(Environment.GetEnvironmentVariable("PLINGBUDDY_IDENTITY_KEY"));
var descriptor = new SecurityTokenDescriptor
{
Issuer = Environment.GetEnvironmentVariable("PLINGBUDDY_APPLICATION_ID"),
Claims = new Dictionary<string, object> { ["sub"] = externalId },
Expires = DateTime.UtcNow.AddMinutes(15),
SigningCredentials = new SigningCredentials(new ECDsaSecurityKey(ecdsa), SecurityAlgorithms.EcdsaSha256),
};
return new JsonWebTokenHandler().CreateToken(descriptor);
}
Your app then asks your backend for a token and hands it to the SDK.
Sign only for the user you've authenticated
The endpoint that makes the token must take the external id from its own session, the user it has already signed in, never from the request. A token for another user's id lets whoever holds it act as that user on PlingBuddy, including deleting them.
To set it up, open the application's Identity verification page in the dashboard:
- Generate a key. The dashboard shows its private key once, to download or copy; PlingBuddy keeps only the public half. Store the private key in your backend's secret store, never in the app.
- Have your backend sign a token for each signed-in user with it, and your app pass the tokens to the SDK (see your platform's Users and data page).
- Turn identity verification on, last: until then the service ignores the tokens, so you can ship the app first. Wait until app versions that don't send tokens are no longer in use: once it's on, their signed-in users' calls are refused.
Identity verification is also what lets a signed-in user delete their data from the app: without it, anyone who knows a user's external id could erase that user.
Sending¶
The dashboard saves campaigns, which the service sends to every reachable subscribed device or, for a test campaign, to the application's test devices. Devices are added as test devices from a recipient's page in the dashboard. There's no API for apps to send notifications yet.
Delivery is at least once: a notification can arrive twice when the service didn't hear back from Apple or Google, and the SDK shows and counts it once.
Events¶
The SDK tells the service what happens to its notifications (received, displayed, suppressed, opened, dismissed) and to the device (app opened, permission changed, push token refreshed). Apps call nothing for it. Each platform's Events page lists exactly when each one is reported.