Enable push notifications for Android (optional)

Push notifications are great for real-time messaging and informing your app users that there is a new message for them.
Written by Alex
Updated 5 months ago

We believe that push notifications are a great way to add real-time messaging to your application and inform your app users that there is a new message from HelpCrunch (i.e. from your admin dashboard). It allows you to stay in touch with your users even when the application is not in the foreground.

The HelpCrunch Mobile SDK does not necessarily require implementing push notifications. Without push notifications, your app's users will still be able to receive new messages, but they will only discover them after opening the HelpCrunch window.

We use Firebase Cloud Messaging (FCM). Is a cross-platform messaging solution that lets you reliably deliver messages at no cost.

So, to add push notifications to your app, you'll need:

  1. Add google-services.json to the app/ folder 
  2. To enable Firebase products in your app, add the google-services plugin to your app/build.gradle file. In your root-level (project-level) Gradle file (build.gradle), add rules to include the Google Services plugin. Check that you have Google's Maven repository as well. 
buildscript {

  repositories {
    // Check that you have the following line (if not, add it):
    google()  // Google's Maven repository
  }

  dependencies {
    // ...

    // Add the following line:
    classpath 'com.google.gms:google-services:4.4.0'  // Google Services plugin
  }
}

allprojects {
  // ...

  repositories {
    // Check that you have the following line (if not, add it):
    google()  // Google's Maven repository
    // ...
  }
}

//In your module (app-level) Gradle file (usually app/build.gradle), add a line to the bottom of the file.
apply plugin: 'com.android.application'
android {
     // ...
}

// Add the following line to the bottom of the file:
apply plugin: 'com.google.gms.google-services' // Google Play services Gradle plugin
Don't forget to configure your Android app and add the Google Service Account Key from Firebase Console. Read more about adding the key here.

Adding HelpCrunch notifications to a project with notifications

If you have a overriding of the FirebaseMessagingService class in your project, then the push messages will come into it. 

In this case, you need to separate your notifications from ours. The following code does a great job:

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        if (HelpCrunchExt.isHelpCrunchMessage(remoteMessage)) {
            HelpCrunch.showNotification(remoteMessage);
        } else {
            //Do something yours
        }
    }
}

FirebaseMessagingService conflicts

Please note that our service has a very high priority. If you encounter a problem when you do not receive your notifications in the application, then increase the priority of your FirebaseMessagingService over 9990 and follow the instructions from the Adding HelpCrunch notifications to a project with notifications category.

Service initialization example:

<service
	android:name=".YourFirebaseMessagingService"
	android:exported="false">
	<intent-filter android:priority="9991">
		<action android:name="com.google.firebase.MESSAGING_EVENT" />
	</intent-filter>
</service>

Did this answer your question?