Send a Test Push

To test whether your app can receive a push directly from the Apple Push Notification service (APNs), follow these steps.

  1. Get the push token from the SDK by calling the deviceToken method and send the result to yourself via email, alert, or another method. These code examples show how to call the deviceToken method.

    For SDK version 10 or later, use this code:

    1PushFeature.requestSdk { pushFeature in
    2  print(pushFeature?.deviceToken() ?? "error: no token - was UIApplication.shared.registerForRemoteNotifications() called?")
    3}

    For SDK version 8, use this code:

    1print(SFMCSdk.mp.deviceToken() ?? "error: no token - was UIApplication.shared.registerForRemoteNotifications() called?")

    For SDK version 7, use this code:

    1print(MarketingCloudSDK.sharedInstance().sfmc_deviceToken() ?? "error: no token - was UIApplication.shared.registerForRemoteNotifications() called?")
  2. Trigger the APNs API directly from the command line by running a bash script. The script you run depends on whether your app uses a .p8 key or a .p12 certificate.

    This bash script points to the development APNs service by default. To trigger push from the APNs production server, change the value of the endpoint variable to https://api.push.apple.com.

    Note

    1#!/bin/bash
    2
    3# To get curl with HTTP/2 and openssl with ECDSA: run 'brew install curl-openssl'
    4curl=/usr/local/opt/curl/bin/curl
    5openssl=/usr/local/opt/openssl/bin/openssl
    6
    7--------------------------------------------------------------------------
    8
    9deviceToken=YOUR_PUSH_TOKEN
    10authKey=PATH_TO_YOUR_P8_FILE
    11authKeyId=KEY_ID_FOR_YOUR_P8_FILE
    12teamId=YOUR_TEAM_ID
    13bundleId=YOUR_BUNDLE_ID
    14endpoint=https://api.development.push.apple.com
    15
    16read -r -d '' payload <<-'EOF'
    17{
    18"aps": {
    19    "badge": 1,
    20    "alert": {
    21        "title": "TEST_PUSH_TITLE",
    22        "subtitle": "TEST_PUSH_SUBTITLE",
    23        "body": "TEST_PUSH_BODY"
    24    },
    25    "_sid": "SFMC"
    26}
    27}
    28EOF
    29
    30# --------------------------------------------------------------------------
    31
    32base64() {
    33$openssl base64 -e -A | tr -- '+/' '-_' | tr -d =
    34}
    35
    36sign() {
    37printf "$1"| $openssl dgst -binary -sha256 -sign "$authKey" | base64
    38}
    39
    40time=$(date +%s)
    41header=$(printf '{ "alg": "ES256", "kid": "%s" }' "$authKeyId" | base64)
    42claims=$(printf '{ "iss": "%s", "iat": %d }' "$teamId" "$time" | base64)
    43jwt="$header.$claims.$(sign $header.$claims)"
    44
    45$curl --verbose \
    46--header "content-type: application/json" \
    47--header "authorization: bearer $jwt" \
    48--header "apns-topic: $bundleId" \
    49--data "$payload" \
    50$endpoint/3/device/$deviceToken
    1#!/bin/sh
    2
    3export DEVICE_TOKEN=YOUR_PUSH_TOKEN
    4export BUNDLE_ID=YOUR_APP_BUNDLE_ID
    5export CERT_PATH=PATH_TO_YOUR_P12_FILE
    6export CERT_PASSWORD=PASSWORD_FOR_YOUR_P12_FILE
    7
    8# sent your endpoint to prod or sandbox
    9# sandbox MUST use a development push certificate
    10# sandbox MUST use a push token from a debugging build of your app (run from
    11# Xcode to a connected device)
    12export ENDPOINT=https://api.push.apple.com/3/device
    13#export ENDPOINT=https://api.sandbox.push.apple.com/3/device
    14
    15curl -v \
    16-d '{"_m": "TEST_MESSAGE_ID", "aps": {"alert": {"body": "TEST_PUSH_BODY", "title": "TEST_PUSH_TITLE", "subtitle": "TEST_PUSH_SUBTITLE"},"_sid": "SFMC"}}' \
    17-H "apns-topic: ${BUNDLE_ID}" \
    18--http2 \
    19--cert ${CERT_PATH}:${CERT_PASSWORD} \
    20--cert-type P12 \
    21${ENDPOINT}/${DEVICE_TOKEN}

Send a Test Message by Using Marketing Cloud Engagement 

After you send a test message by using the API, you can also send a test by using the Marketing Cloud Engagement user interface.

Make sure that you select the same environment MobilePush Administration page as you used to send the test message via the API. If you select Development on the MobilePush Administration page, Marketing Cloud Engagement contacts the Sandbox APNs server. If you select Production, it contacts the Production APNs server.