# Push notifications — idiot-proof setup + test (Metriku Demo)

Follow **in order**. Do not skip. No TestFlight needed for the first successful test on a physical iPhone from Xcode.

---

## What you need

| Item | Where |
|------|--------|
| Metriku project + **App Key** | Dashboard → Settings (created with the project) |
| Firebase project | console.firebase.google.com |
| **Service account JSON** | Firebase → Project settings → Service accounts → Generate new private key |
| **APNs Auth Key (.p8)** | Apple Developer → Keys → Apple Push Notifications |
| Physical iPhone + cable | Simulator cannot receive real push |
| MetrikuDemo or your app | DoraSDK / Examples |

**Not** `GoogleService-Info.plist` for Metriku server — that file is for the iOS Firebase **client**. Metriku’s backend needs the **service account JSON**.

The iOS SDK talks to **https://metriku.dev** automatically. You never set an API base URL.

---

## Step 1 — Apple APNs key → Firebase

1. [Apple Developer](https://developer.apple.com/account) → Certificates, Identifiers & Profiles → **Keys** → **+**.
2. Name it `Metriku APNs`, enable **Apple Push Notifications service (APNs)** → Continue → Register.
3. Download the `.p8` once. Note **Key ID** and your **Team ID**.
4. Firebase Console → your project → Project settings → **Cloud Messaging** → Apple app configuration → upload **APNs Authentication Key** (the `.p8`), Key ID, Team ID.
5. Ensure Bundle ID in Firebase iOS app matches Xcode (`com.urimhaxhiu.metrikudemo` for the demo).

## Step 2 — Service account → Metriku Settings

1. Firebase → Project settings → **Service accounts** → **Generate new private key** → save `.json`.
2. Metriku → your project → **Settings** → Firebase → **Upload .json** → Save.
3. Status should show Configured.

## Step 3 — Wire the iOS app (MetrikuDemo)

1. Xcode: open MetrikuDemo, set your Team, enable **Push Notifications** capability.
2. Paste **App Key** only → Connect (host is always metriku.dev).
3. Run on a **physical device** (not Simulator).
4. Allow notifications when prompted.
5. Copy **deviceId** shown in the demo (or Xcode console: `Metriku.User.deviceId()`).

Minimal code (if not using Demo UI):

```swift
import Metriku
import UIKit

Metriku.initialize("YOUR_APP_KEY", launchOptions: launchOptions)
#if DEBUG
Metriku.setSandbox(true)
#endif

Metriku.Notifications.requestPermission { granted in
  print("push permission", granted)
}

func application(_ application: UIApplication,
                 didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  Metriku.Notifications.setPushToken(deviceToken)
  print("deviceId", Metriku.User.deviceId())
}
```

## Step 4 — Send a test push

1. Metriku → **Settings** → Test messaging (or **Push** → Test by device ID).
2. Paste `deviceId` → **Send test push**.
3. Phone should show a notification within seconds.
4. Watch Xcode console for Metriku logs if it fails.

---

## Checklist if nothing arrives

1. Physical device? (Simulator = no)
2. Push capability + permission granted?
3. `setPushToken` called? (check Users page: push on)
4. Firebase APNs key uploaded?
5. Metriku Firebase JSON uploaded for **this** project?
6. App Key correct?

---

## Copy-paste prompts for an AI coding assistant

### Prompt A — Add Metriku SDK + initialize

```
Add the Metriku iOS SDK via SPM from https://github.com/haxhiuurim/DoraSDK.git (product Metriku)
or binary DoraVersion 1.2.0+. Call Metriku.initialize("YOUR_APP_KEY", launchOptions: launchOptions)
only — do NOT pass apiBaseURL (defaults to https://metriku.dev).
In DEBUG call Metriku.setSandbox(true). Use Metriku.Analytics.trackPaywallView() for funnel events.
No REST API key exists.
```

### Prompt B — Push permission + token

```
Wire UNUserNotificationCenter and UIApplicationDelegate:
1) On launch request Metriku.Notifications.requestPermission
2) In didRegisterForRemoteNotificationsWithDeviceToken call Metriku.Notifications.setPushToken(deviceToken)
3) Log Metriku.User.deviceId() so I can paste it into the Metriku dashboard test push field.
Enable Push Notifications capability in the Xcode project if missing.
```

### Prompt C — Handle notification taps

```
Forward UNNotificationResponse to Metriku.Notifications.handleNotificationResponse.
Add a Metriku.Notifications.addClickListener that prints campaignId, deepLink, redirectURL.
```

### Prompt D — Verify live config

```
After Metriku.initialize, call Metriku.Config.refresh(force: true) and print Metriku.Config.allKeys().
New projects have zero keys until the user creates them in the dashboard. Only read keys you created
(e.g. getBool / getStringArray with safe defaults). Do not crash if keys are missing.
```
