Push Notification

This steps will tell you how to integrate push notifications with Android app using FCM.
First, integrate your Android app with Channel plug-in and follow the instructions below.

Step 1. Create Firebase Project

📘

If you already set a FCM in your project, skip this step.

Go to Firebase console. Click ‘Create a project’ and fill in information.

16441644

After creating a project, add android platform

660660

Add your app

14341434

Download google-service.json and move the file into your Android app module root directory. Click ‘Continue’ and set gradle by editing bundle.gradle.

16461646

In your project & app build.gradle, you will need to add the following line to your dependencies:

Project build.gradle

dependencies {
    ...
    classpath 'com.google.gms:google-services:4.0.1'
    ...
  }

Maven CentralMaven Central

App build.gradle

dependencies {
    ...
    implementation 'io.channel:plugin-android-fcm:$[version]'
    ...
      
    
}

...
//At the bottom of your 'app build.gradle', you must add this line.   
apply plugin: 'com.google.gms.google-services'

Step 2. Register your server key to ChannelIO for Android settings

Click the Settings button on the top left and move to Project settings.

852852

Move to ‘Cloud messaging’ tab and create server key and copy it

22942294

Launch Channel Desk. Go to Settings and click ‘Integrate’.

24802480

Click ‘Android application’ card. Paste the server key you copied into API Key and click ‘save'.

Step 3. Set push notifications title (optional)

Set notification title as string resource.
Default value is app name

<resources>
  ...
  <string name="notification_title">Your notification title</string>
  ...
</resources>

Step 4. Register your FCM icon

If you wish to customise the icon of notification, add an image with an identical file name to the following path. For example:

Notification small icon

  • /res/drawable/ch_push_icon.png

Step 5. Open ChannelIO chat from FCM

If you want to move to the corresponding chat when when a user tap ChannelIO notification, handle push message by adding the following code in the last line of your Main Activity's onCreate() method.

public class MyActivity extends AppCompatActivity {
 
    @Override
    public void onCreate() {
        super.onCreate();
 
        // TODO : Your code
        // ...
        // ...
        if (ChannelIO.hasStoredPushNotification(this)) {
          ChannelIO.openStoredPushNotification(this);
        }
    }
}

Step 6. Using ChannelIO with other FCM setups (Optional)

If you’re already using FCM in existing projects, you’ll need to update class that extends FirebaseMessagingService

This service is that deliver token to ChannelIO server to register your device and show notification

public class MyFirebaseMessagingService extends FirebaseMessagingService {

  @Override
  public void onNewToken(String refreshedToken) {
    ChannelIO.initPushToken(refreshedToken);
    // Enter your code
  }

  @Override
  public void onMessageReceived(RemoteMessage remoteMessage) {
    Map<String, String> pushMessage = remoteMessage.getData();

    if (ChannelIO.isChannelPushNotification(pushMessage)) {
      ChannelIO.receivePushNotification(getApplication(), pushMessage);
    } else {
      // Enter your code
    }
  }
}

Step 7. Disable push notification (Optional)

We recommend you to call ChannelIO.shutdown() when they have logged out of the app for stoping users from receiving push messages

Troubleshooting tips

If you’re having trouble getting FCM to work in your app, you should check this list:

  • Check user online status. The Push notification is sent only when the user is offline.
  • Remove and reinstall the app to update the token again.
  • Check that the notifications are not disabled for your app on your test device. Settings > Sound & Notification > App notifications.