Skip to content

Connecting notifications on Android🔗

To integrate notifications, you need to make a few simple steps:

Receiving notifications🔗

Step 1. Import the notification module🔗

Add a dependency in build.gradle file:

dependencies {
    def mrgsVersion = "5.0.0"
    implementation "games.my.mrgs:notifications:$mrgsVersion"
}

  • Download the latest version of the library from the MRGS website.
  • Extract the MRGSNotifications module from the archive.
  • Copy the MRGSNotifications.aar file to the libs directory of your project.
  • Add the necessary dependencies to the build.gradle file:
dependencies {
    implementation(name: 'MRGSNotifications', ext:'aar')

    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.firebase:firebase-messaging:23.1.2'

    // Uncomment for Huawei/AppTouch Store
    // implementation 'com.huawei.hms:push:6.11.0.300'
}

Notification Icons.

If you want to use icons different from the application icons in the notifications, specify their file names when initializing MRGS (the name of the icon file; without the png extension; is in the res/drawable directory of your project).

Permission to display notifications

Unlike iOS, on Android prior to Android 13, the app can display notifications without requiring the user's permission. Starting with Android 13, this will require permissions given by the user.

Step 2. (Optional) Server notifications🔗

If you want to send notifications to the client via your server or MGC server, you need to connect Firebase Cloud Messaging or Huawei Push Kit for Huawei/AppTouch Store.

Firebase Cloud Messaging🔗

  • Copy the googe-services.json file downloaded from the Google Firebase console to the root directory of your project.
  • Add a dependency to google-services plugin to the build.gradle file:
buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        //...
        classpath 'com.google.gms:google-services:4.3.13'
    }
}
  • Add a call to the google-services plugin with the last line in the build.gradle file:
apply plugin: 'com.google.gms.google-services'

Huawei Push Kit🔗

  • Sign in to AppGallery Connect and click My projects.
  • Find your app project and click the app.
  • Go to Project settings > General information. In the App information area, download the agconnect-services.json file. Add this json file to the root of your project.
  • Add a dependency to agconnect plugin to the build.gradle file:
buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        //...
        classpath 'com.huawei.agconnect:agcp:1.8.0.300'
    }
}
  • Add a call to the agconnect plugin with the last line in the build.gradle file:
apply plugin: 'com.huawei.agconnect'
  • For full information on integrating Huawei Push Kit see documentation.

Important

The latest Huawei devices usually do not have Google Services, so connecting libraries such as Firebase or other libraries that require Google Services will have little effect. Just keep in mind HuaweiStore aims to work with its own services as well as Google Play with its own.

Step 3. Configure the notification receiving delegate🔗

To receive a callback about a notification arrival, or about a click on it, it is necessary to install a listener:

import games.my.mrgs.MRGSMap;
import games.my.mrgs.notifications.MRGSNotificationCenter;
import games.my.mrgs.notifications.MRGSPushNotificationHandler;

final MRGSPushNotificationDelegate notifyDelegate = new MRGSPushNotificationDelegate() {
    @Override
    public void clickOnNotification(int idNotify, String title, String msg, MRGSMap developerPayload, boolean isLocal) {

    }

    @Override
    public void receivedNotification(int idNotify, String title, String msg, MRGSMap developerPayload, boolean isLocal) {

    }
};

MRGSNotificationCenter.getInstance().setLocalDelegate(notifyDelegate);
  • idNotify - id of the scheduled notification.
  • title - notification title.
  • msg - notification message.
  • developerPayload - additional information, if the developer added any when creating the notification.
  • isLocal - true if local notification, false if a server one.

Working with notifications🔗

Important

Before working with the MRGSNotifications module, you need to initialize MRGService.

Permission to display notifications

If the user has Android version 13 or higher, permission to display notifications will be requested during initialization, if the delayed notification start parameter was not set. For more information, see Manual management of registration and permission request

Creating notifications🔗

To create notifications, use the MRGSPushNotification class. To display a notification, you need to create an instance of this class using the create method and specify the main notification parameters: id (digital identifier of the notification that can help you determine which notification worked), message text (message) and time in seconds in Unix Time format that shows when the notification will be made.

int time = MRGS.timeUnix() + 10; // After 10 seconds
int notifyId = 12345;
MRGSPushNotification notification = MRGSPushNotification.create("Local Push notification", notifyId, time);

Sending notifications🔗

After the notification was created using the MRGSPushNotification.create method, it can be sent to the notification scheduler:

import games.my.mrgs.notifications.MRGSNotificationCenter;

MRGSNotificationCenter.getInstance().addLocalPush(notification);
Your own channel and notification group.

If you created your channel and group, you need to specify their ids in the notification settings:

notification.setChannelId("my_channel_id");
notification.setChannelGroupId("my_group_id");

MRGSNotificationCenter.getInstance().addLocalPush(notification);

Getting a list of notifications🔗

For a list of all scheduled notifications, use the following method:

import games.my.mrgs.MRGSList;
import games.my.mrgs.notifications.MRGSNotificationCenter;

MRGSList notifications = MRGSNotificationCenter.getInstance().getAllLocalPushes();

Receiving notification information🔗

If you want to get the MRGSPushNotification object by its id, use the following method:

import games.my.mrgs.notifications.MRGSNotificationCenter;
import games.my.mrgs.notifications.MRGSPushNotification;

MRGSPushNotification notification = MRGSNotificationCenter.getInstance().getLocalPush(notificationId);

Canceling notifications🔗

If you want to cancel (delete) a scheduled notification, use the following method:

import games.my.mrgs.notifications.MRGSNotificationCenter;

MRGSNotificationCenter.getInstance().removeLocalPush(context, notificationId);
  • context - Application Context.
  • notificationId - notification id that you've set when calling the MRGSPushNotification.create method.

Visibility of notifications on LockScreen(secure)🔗

If you need additional control over the visibility of notifications on a secure lock screen (which is protected by a password, pin, etc.), you must specify the scope.

import games.my.mrgs.notifications.MRGSNotificationCenter;
import games.my.mrgs.notifications.MRGSPushNotification;

MRGSPushNotification notification = MRGSPushNotification.setVisibility(visibility);
MRGSNotificationCenterImpl.getInstance().addLocalPush(notification);
  • visibility - one of the parameters of MRGSPushNotification.VISIBILITY_PUBLIC, MRGSPushNotification.VISIBILITY_PRIVATE, MRGSPushNotification.VISIBILITY_SECRET;

Behavior features

When screen protection is activated, there are several ways to display notifications on the lock screen:

  • Show all notification content
  • Hide sensitive content
  • Don’t show notifications at all

This option allows you to configure global settings for all notifications applications, which can be switched separately for each application in its settings. Starting with Android 8.0, the setting is added for each channel separately.

In addition, when Screen lock and Notification scope are combined, the following behaviors are obtained:

LockScreen with Show all notification content: VISIBILITY_PUBLIC and VISIBILITY_PRIVATE - display notifications on the LockScreen without any restrictions. VISIBILITY_SECRET - doesn't display notifications on LockScreen.

LockScreen with Hide sensitive content: VISIBILITY_PUBLIC - display notifications on the LockScreen without any restrictions. VISIBILITY_PRIVATE - displays the notification but does not display its contents. VISIBILITY_SECRET - doesn't display notifications on LockScreen.

LockScreen with Don’t show notifications at all: No notifications can be shown on the Lock screen.

Keep in mind that the final behavior of notifications on LockScreen depends only on the user.


Last update: 2025-01-21
Created: 2020-02-17