> ## Documentation Index
> Fetch the complete documentation index at: https://docs.suprsend.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Mobile Push Setup

> Step-by-step guide to integrate FCM Push notifications into your android app using SuprSend.

## Integration Steps

<Steps>
  <Step title="Create Firebase project in firebase console">
    To start sending notifications from FCM, you'll have to first create a firebase project. Create firebase project and application in [firebase console](https://firebase.google.com/) with your applications package name which you can find in *`MainApplication.java`* or *`AndroidManifest.xml`*
  </Step>

  <Step title="Adding google-services.json">
    You can get your Service Account JSON by [following these instructions](https://firebase.google.com/docs/cloud-messaging/auth-server#provide_credentials_manually). Download **google-services.json** and add the file inside your android>app folder.

    <Frame>
      <img src="https://mintcdn.com/suprsend/jhGzZpggWCp1KSgu/images/docs/e2d76a2-Group_6.png?fit=max&auto=format&n=jhGzZpggWCp1KSgu&q=85&s=2e85e8f3794bee2bd3e1eac2ae1e0a9f" width="2212" height="1514" data-path="images/docs/e2d76a2-Group_6.png" />
    </Frame>
  </Step>

  <Step title="Adding Firebase dependencies and plugins">
    Add below dependency inside projects *build.gradle* inside dependencies

    <CodeGroup>
      ```groovy build.gradle theme={"system"}
      dependencies {
              ...
              classpath 'com.google.gms:google-services:4.3.10' // or latest version
      }
      ```
    </CodeGroup>

    Add below plugin inside apps *build.gradle*

    <CodeGroup>
      ```groovy build.gradle theme={"system"}
      apply plugin: 'com.google.gms.google-services'
      ```
    </CodeGroup>

    Add below dependency inside apps *build.gradle* inside dependencies

    <CodeGroup>
      ```groovy build.gradle theme={"system"}
      implementation("com.google.firebase:firebase-messaging:22.0.0") // or latest version
      ```
    </CodeGroup>
  </Step>

  <Step title="Implementing push">
    Push feature can be implemented in two ways:

    <AccordionGroup>
      <Accordion title="Token Generation and Notification handled By SDK [Recommended]" defaultOpen={false}>
        You may use this option if all of your android push notifications are to be handled via SuprSend SDK. We recommend you to use this method as it is just a single step process to just register the service in your application manifest and everything else will be ready.

        <CodeGroup>
          ```xml AndroidManifest.xml theme={"system"}

          <uses-permission android:name="android.permission.INTERNET" />
          <!--If you are targeting to API 33 (Android 13) you will additional need to add POST_NOTIFICATIONS -->
          <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

          <service
              android:name="app.suprsend.fcm.SSFirebaseMessagingService"
              android:enabled="true"
              android:exported="false">
              <intent-filter>
                  <action android:name="com.google.firebase.MESSAGING_EVENT" />
              </intent-filter>
          </service>
          ```
        </CodeGroup>
      </Accordion>

      <Accordion title="Token Generation and Notification handled By Your Application" defaultOpen={false}>
        Since your service is registered in the app manifest, to render the push notification directed from SuprSend server you will have to add the below code to your service.

        <CodeGroup>
          ```javascript helloWorld.js theme={"system"}
          console.log("Hello World");
          ```
        </CodeGroup>
      </Accordion>
    </AccordionGroup>
  </Step>

  <Step title="Sending the FCM token to SuprSend">
    <CodeGroup>
      ```kotlin kotlin theme={"system"}
      class YourApplication : Application() {

          override fun onCreate() {
          FirebaseMessaging.getInstance().token.addOnCompleteListener(OnCompleteListener { task ->
              if (!task.isSuccessful) {
                  Log.w(TAG, "Fetching FCM registration token failed", task.exception)
                  return@OnCompleteListener
              }
              val fcmToken = task.result
              suprsend.user.setAndroidFcmPush(fcmToken);
          })

          //or if you are on older FCM version

          val fcmToken  = FirebaseInstanceId.getInstance().token
          instance.getUser().setAndroidFcmPush(fcmToken)
      }
      ```
    </CodeGroup>
  </Step>
</Steps>

### Asking for permission -Android 13(API-33)

<CodeGroup>
  ```kotlin YourActivity.kt theme={"system"}
  import android.Manifest
  import android.annotation.SuppressLint
  import android.content.Intent
  import android.net.Uri
  import android.os.Build
  import android.os.Bundle
  import android.provider.Settings
  import android.util.Log
  import android.widget.Toast
  import androidx.activity.result.contract.ActivityResultContracts
  import androidx.appcompat.app.AlertDialog
  import androidx.appcompat.app.AppCompatActivity
  import androidx.viewpager.widget.ViewPager
  import com.google.firebase.messaging.FirebaseMessaging
  import org.json.JSONObject

  class YourActivity : AppCompatActivity() {
      private val activityResultLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted: Boolean ->
          if (!isGranted) {
              // You can show a dialog which explains the intent of this permission request of how it is important for certain features of your app to work
              AlertDialog.Builder(this)
                  .setView(R.layout.notification_permission_desc)
                  .setTitle(getString(R.string.app_name))
                  .setPositiveButton("Proceed") { _, _ ->
                      val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
                      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                      val uri: Uri = Uri.fromParts("package", packageName, null)
                      intent.data = uri
                      startActivity(intent)
                  }
                  .setNegativeButton("Deny") { _, _ ->
                  }.show()
          }
      }

      override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
              activityResultLauncher.launch(
                  Manifest.permission.POST_NOTIFICATIONS
              )
          }
      }
  }
  ```
</CodeGroup>

If the androidx dependency is not present then you will have to add the below dependency in your app dependencies

<CodeGroup>
  ```Text app/build.gradle.kts theme={"system"}
  dependencies {
  	implementation("androidx.appcompat:appcompat:1.3.1")
  }
  ```
</CodeGroup>

<Tip>
  **How to identify if notification is sent by SuprSend?**

  If notification payload contains key **supr\_send\_n\_pl** then simply consider this as payload sent from suprsend and pass the payload to suprsend SDK by:

  if (payload?.data?.supr\_send\_n\_pl) \{&#x20;

  suprsend.showNotification(payload.data.supr\_send\_n\_pl);&#x20;

  }
</Tip>

***
