# Push Notification Registration

## Goal

Register the device for remote push notifications and obtain the device token to send to your server.

## Actions Required

| Action                                                     | Purpose                                               |
| ---------------------------------------------------------- | ----------------------------------------------------- |
| NotificationServicesRequestPermission                      | Request notification permissions                      |
| NotificationServicesGetRequestPermissionError              | Read cached error after permission failure (optional) |
| NotificationServicesOnRegisterForPushNotificationsComplete | Listen for device token                               |

## Variables Needed

* deviceToken (String)
* errorCode (Int)
* errorDescription (String)
* serverUrl (String) - Your backend URL

## Implementation Steps

### 1. State: RequestPermission

**Action:** NotificationServicesRequestPermission

* **Inputs:**
  * requestAlert: true
  * requestBadge: true
  * requestSound: true
* **Events:**
  * grantedEvent → ListenForToken
  * deniedEvent → ShowError
  * failureEvent → ShowError

**Optional:** On `failureEvent`, call `NotificationServicesGetRequestPermissionError` to read `errorCode` + `errorDescription`.

### 2. State: ListenForToken

**Action:** NotificationServicesOnRegisterForPushNotificationsComplete

* **Outputs:**
  * deviceToken → deviceToken variable
  * errorCode → errorCode
  * errorDescription → errorDescription
* **Events:**
  * successEvent → SendTokenToServer
  * failureEvent → ShowError

**Note:** This is a persistent listener - it waits for the registration callback from the OS.

### 3. State: SendTokenToServer

Send deviceToken to your backend server via HTTP POST:

* Endpoint: serverUrl/register-device
* Payload: {"token": deviceToken, "platform": "iOS/Android"}

**Transition:** Go to Complete

### 4. State: Complete

* Store deviceToken locally for reference
* Display success message
* Ready to receive push notifications from server

## Server Integration Flow

```
Request Permission
    └─ Listen for Token
        └─ Success → Send to Server
            └─ Server uses token for APNs (iOS) or FCM (Android)
```

## Common Issues

* **No Token Received**: Check internet connection, verify platform push setup (APNs/FCM certificates)
* **Token Changes**: Device token can change on app reinstall; always update server with latest token
* **Simulator Limitation**: Push notifications don't work on iOS Simulator; test on real devices
* **Permission Denied**: Cannot register for push without notification permissions

## Server Requirements

**iOS (APNs):**

* Valid APNs certificate or auth key
* Server sends notifications to Apple Push Notification service

**Android (FCM):**

* Firebase Cloud Messaging project setup
* Server API key configured

## Platform Differences

**iOS:**

* Returns 64-character hex string token
* Requires APNs setup in Apple Developer

**Android:**

* Returns FCM registration token
* Requires google-services.json configuration

## Security Note

Treat device tokens as sensitive data. Always transmit via HTTPS to your server and store securely.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://assetstore.essentialkit.voxelbusters.com/features/notification-services/playmaker/use-cases/use-case-4-push-registration.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
