# Push notifications

Start a Screeb survey when the user receives or taps a push notification, by sending the survey id in the notification payload.

Screeb does not send push notifications. Your push service (Firebase Cloud Messaging, for example) sends them, with a survey id in the payload, and your app starts that survey when the notification arrives or when the user taps it.

You need a survey in the Screeb admin and a push service already working in your app.

## Add the survey id to the payload

In your push service, create the notification (title, body, audience, schedule) and add a custom data entry with the key `screeb` and the survey id as value. In the Firebase console, custom data is in the "Additional options" step.

```json
{
  "screeb": "<SURVEY-ID>"
}
```

The key `screeb` is a convention used in the examples below: any key works, as long as your app reads the same one.

## App in the foreground

When the app is in the foreground, Firebase delivers the payload to your `FirebaseMessagingService`. Start the survey in `onMessageReceived`.

```kotlin
import app.screeb.sdk.Screeb
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage

class PushService : FirebaseMessagingService() {
    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        remoteMessage.data["screeb"]?.let { surveyId ->
            Screeb.startSurvey(surveyId = surveyId)
        }
    }
}
```

Declare the service in your `AndroidManifest.xml` with the `com.google.firebase.MESSAGING_EVENT` intent filter, as the Firebase documentation describes.

## App in the background

When the app is in the background, the system shows the notification. Tapping it opens the app with the payload.

```kotlin
// Call it from onCreate() of the activity the notification opens.
private fun startScreebSurveyFromIntent() {
    intent.extras?.getString("screeb")?.let { surveyId ->
        Screeb.startSurvey(surveyId = surveyId)
    }
}
```

`startSurveyFromNotification` is deprecated: call `startSurvey` with the survey id read from the payload, as above.

To add hidden fields, force the language or start a message the same way, see [Start a survey programmatically](https://developers.screeb.app/sdk-android/start-survey-programmatically) and [Start a message programmatically](https://developers.screeb.app/sdk-android/start-message-programmatically).
