# Send a test push and use Message Center

Configure messaging, bind an authenticated user to an installation, preview a campaign, and verify push or in-app Message Center delivery.

Source: https://appportal.io/docs/sdks/messaging

Messaging shares your app and environment with telemetry, but requires its own setup. Use a test
user and device before selecting a production audience. You need the relevant plan entitlement,
campaign-management access, an app credential with the required client permissions, and a backend
credential when issuing user bindings or calling Automation.

## 1. Configure the client and channel

Add the optional product for your SDK: `AppPortalMessaging` on Apple,
`io.appportal:messaging` on Android, or
`package:appportal/messaging.dart` in Flutter. Use the
[compatibility guide](https://appportal.io/docs/sdks/compatibility) and your preview's exact package versions.

The .NET Messaging implementation is currently a source/test family, not a published package.
Use its preview only when your selected integration explicitly supplies it; Telemetry installation
does not silently activate Messaging. The authenticated HTTP diagnostic below remains a separate
adapter check, not a claim that every SDK exposes its binding or pagination support.

For push, configure your app's APNs or FCM identity and register a real device token through the
selected SDK. Apple configuration needs the correct bundle identity and sandbox/production
selection. Android configuration needs the matching Firebase project. Grant OS notification
permission when your application asks for it.

Collection consent, OS notification permission, and communication-channel consent are different
decisions. One does not automatically grant the others.

## 2. Bind the signed-in user and installation

After authenticating the user in **your backend**, issue a binding with an Automation credential
carrying `UserBindingIssue`. POST to `/v1/automation/user-bindings` with a body such as:

```json
{
  "userId": "test-user-42",
  "installationId": "installation-from-the-test-device",
  "operations": [
    "MessageCenterRead",
    "MessageCenterInteract",
    "PushRegistration",
    "ConsentRead",
    "ConsentWrite"
  ],
  "lifetimeSeconds": 900
}
```

Use the [Automation authentication instructions](https://appportal.io/docs/api/automation). The response includes a
`bindingToken` and `expiresAt`. Deliver the token only to the authenticated installation.
Refresh it through your backend before expiry. Do not let the app choose another user's identity
when requesting a binding. On logout, revoke the installation's binding and clear the app's user state.

An arbitrary user ID plus an app-wide credential does not authorize Message Center access.
Message Center requests require both the app credential and `X-AppPortal-User-Binding`.
If your SDK cannot supply the binding header, use the authenticated Client API requests below.

## 3. Preview and send to a test user

In **Engagement → Campaigns**, choose **New campaign**. Select a template or use
**Create new template** to supply a title and body. Choose an audience and channel policy.
Run the dry run and inspect reachable recipients, exclusions, channel choices, and any cost.

Enter only your test user's ID in the test-recipient field and choose **Send test**.
For a backend-only push test, use the [Automation push dry run](https://appportal.io/docs/api/automation) first,
then change `dryRun` to `false` with a new idempotency key after reviewing the intended recipient.

**Success:** the test device receives the notification, or the Message Center contains the new item
when that channel is selected. Inspect **Engagement → Deliveries** for the recorded channel and
outcome. A provider accepting a notification is not proof that a person viewed it.

## 4. Read and render Message Center items

The Client API uses `https://api.appportal.io` in production and
`https://api.stage.appportal.io` for Stage. This diagnostic request shows the authorization
required by a messaging adapter. Set the values through a secure test environment:

```bash
curl --fail-with-body \
  --header "X-AppPortal-SDK-Key: $APPPORTAL_APP_CREDENTIAL" \
  --header "X-AppPortal-User-Binding: $APPPORTAL_USER_BINDING" \
  "$APPPORTAL_CLIENT_URL/v1/message-center/test-user-42/page?limit=25"
```

The response is a page with `items`, `nextCursor` and `totalCount`. `totalCount` is `null`, not
zero: this live inbox has no snapshot-wide count. Keep the same user, credential, binding and
`limit`; URL-encode the returned opaque cursor and send it as `cursor` on the next request.
Continue until `nextCursor` is `null`, checking every HTTP outcome before treating the read as
complete. New arrivals above the boundary appear on refresh; expired or archived items can leave
later pages. Do not present already-collected pages as a complete inbox if a later request fails.

A successful terminal empty page is a server observation of no active items for that selection,
not proof from an SDK's fallback return value. In particular, the current .NET API retains its
legacy empty-list failure result and emits a diagnostic; inspect that result's diagnostics rather
than equating an empty SDK list with verified server emptiness.

Older servers may only expose `/v1/message-center/{userId}`. Its array representation is complete
only for small inboxes (up to 100 items on the current server); larger current inboxes return
`409 pagination_required` and point to the same-authority `/page` route. This response bound is
not a Message Center entitlement cap. Do not silently ignore overflow or accept a partial array.
Render each item's title and body in your app. Record a read or click only when the corresponding
interaction occurs, using POST `/v1/message-center/{userId}/items/{itemId}/interactions`
with the same credential and binding:

```json
{ "kind": "read" }
```

Supported kinds are `read`, `clicked`, `dismissed`, and `archived`.
Acknowledge the returned state before presenting a mutation as saved.

## When delivery fails

Check the app environment, current binding, device registration, APNs/FCM identity, OS permission,
channel consent, suppression, audience exclusions, and plan policy. Expired bindings need refresh;
do not replace them with a server key in the app. For SMS, also verify phone ownership, explicit
SMS consent, available spend, and suppression state.
