Handling push notifications

Usage guides to help you get started with the Push Notifications in the Android Knock SDK.

Note: We Recommend taking advantage of our KnockMessagingService & KnockActivity to make managing your Push Notifications simpler.

Prerequisites

  • Before proceeding, ensure you've configured push notifications within your Knock account. For guidance on this initial setup, refer to our Push Notification Configuration Guide.
  • Review Firebase's documentation for advanced features and updated practices.

1. Create a Firebase project

  • Go to the Firebase Console.
  • Click on "Add project" and follow the on-screen instructions to create a new Firebase project.

2. Add your Android app to the Firebase project

  • In the Firebase Console, open the project you just created.
  • Click on the Android icon to add an Android app to your Firebase project.
  • Enter your app's package name and a nickname for your app.
  • (Optional) Enter the SHA-1 of your signing certificate.
  • Download the google-services.json file and place it in your app's app/ directory.

3. Add Firebase SDK to your project

  • In your project-level build.gradle file, add the Google services Gradle plugin as a dependency:

  • In your app-level build.gradle file, apply the Google services plugin at the bottom of the file and add Firebase Messaging dependency:

4. Update your app's manifest

  • Add the service you just created to your AndroidManifest.xml:

5: Requesting user permission for push notifications

  • To prombt the user to approve or deny push notification permissions call the Knock.shared.requestNotificationPermission() method.

6. Register device token

  • KnockMessagingService:

    • If using the KnockMessagingService, this will be handled for you automatically.
  • Manually:

5. Receive push notifications

To detect when a push notification is received in the foreground:

  • KnockMessagingService:

  • Manually:

8. Handling push notification taps

  • KnockMessagingService:

  • Manually: