Skip to content

Advanced notification settings, additional features🔗

Delaying the notification permission request🔗

In Android 13+, the first time the user is asked for permission to send notifications, a popup will be shown to the user. With the standard MRGSNotificationCenter setting, this popup will be shown at the very start of the application, which may negatively affect the number of permissions, because this is bad practice, it's better to explain to the user why notifications are needed first.

If you need to ask the user for permission the first time later (for example, by clicking on a button, or after a certain screen), then you can launch our MRGSNotificationCenter at the right time. After launch, permission to send notifications will be requested, a token will be received. To do this, set the delayed start flag to true at the parameter configuration step:

import games.my.mrgs.MRGSExternalSDKParams;
import games.my.mrgs.MRGService;
import games.my.mrgs.MRGServiceParams;

public class YourApplicationClass extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        final MRGServiceParams serviceParams = MRGServiceParams.init(<MRGS_APP_ID>, <CLIENT_SECRET>, MRGSPlatform.ANDROID);
        serviceParams.setDeferredPushStart(true);

        MRGService.service(context, serviceParams);
    }
}

Then, at the moment when you are ready to show the user a request, you need to call the method:

MRGSNotificationCenter.getInstance().enableNotifications(NOTIFICATIONS_TYPE_LOCAL);

Or other variations of this method (for more details on enabling and disabling notifications, see corresponding section).

After calling the method, our MRGSNotificationCenter will be launched, the popup will be shown. In the future, it is not necessary to call this method every time, we ourselves will automatically run MRGSNotificationCenter at the application start if the popup has been shown at least once.

Alternative request display method

If you have delayed the notification permission request (or need to be shown again as the dialog can be shown multiple times), you can invoke it later like this:

MRGSNotificationCenter.getInstance().requestNotificationsPermissions();

Getting user notification settings🔗

To get the status of notifications (enabled/disabled), use the areNotificationsEnabled method:

games.my.mrgs.notifications.MRGSNotificationCenter;

boolean status = MRGSNotificationCenter.getInstance().areNotificationsEnabled(context);

Go to settings

If the user has refused notifications in the system window, or turned off the types of notifications you need, but is ready to turn them back on, then you can transfer it directly to the settings application on the tab of your application using using this method

Adding custom sounds to notifications🔗

To add your own sound that plays when a notification arrives, use the following field:

int time = MRGS.timeUnix() + 10;
MRGSPushNotification notification1 = MRGSPushNotification.create("Message with sound", 1, time);
notification1.setSound("custom_notification_sound"; //File name without extension

On Android 8.0 and higher, there is no way to set the sound for a particular notification. To configure the notification sound, you must specify it when creating a channel.

Sound file location.

If you specified the Sound parameter when creating the notification channel, then a file with this name should be in the resources directory (res/raw/).

Notification sound

Creating channels and channel groups🔗

Starting with Android 8, notification channels and groups of notification channels were added to the system. Using them, the user can disable some of the notifications (which belong to a certain channel) in their application. MRGS automatically creates a channel with ID MRGS_DEFAULT_CHANNEL and group MRGS_DEFAULT_CHANNEL_GROUPand uses them in scheduled notifications. If you want to create a channel yourself, you can use the MRGSPushNotificationChannel and MRGSPushNotificationChannelsGroup classes:

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

final MRGSNotificationChannelGroup group = new MRGSNotificationChannelGroup("my_group_id", "My Group");
MRGSNotificationCenter.getInstance().createNotificationChannelGroup(context, group);
  • Id - string, unique group identifier in the application.
  • Name - the name of the group that will be displayed in the system settings of the application.
import games.my.mrgs.notifications.MRGSNotificationCenter;
import games.my.mrgs.notifications.MRGSNotificationChannel;

MRGSNotificationChannel channel = new MRGSNotificationChannel("my_channel_id", "My Channel");
channel.setDescription("Description");
channel.setGroup("my_group_id");
channel.setSound("push_sound");
channel.enableLights(true);

MRGSNotificationCenter.getInstance().createNotificationChannel(context, channel);
  • Id - a string, unique channel identifier in the application.
  • Name - the name of the channel that will be displayed in the system settings of the application.
  • Description - a short description of the channel that will be displayed in the system settings of the application.
  • Group - the id of the group created using the createNotificationChannelGroupmethod, as described above.
  • Sound - a file name (without extension) with the sound that will be played when a notification is received (optional).
  • ShowLights - whether to use the light indicator on the smartphone (if there is any) when a notification is displayed (optional).
Sound file location

If you specified the Sound parameter when creating the notification channel, then a file with this name should be located in the resources directory (res/raw/)

Notification sound

Setting up notification grouping🔗

Grouping allows you to combine several notifications into one and display them in the form of a drop-down list. Shouldn't be confused with channel groups.

By default, Android does not group notifications. If you want to combine several notifications into one, you need to set their group Id.

Use the fields:

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

final int time = MRGS.timeUnix() + 10;
MRGSPushNotification notification1 = MRGSPushNotification.create("Message number 1", 1, time);
notification1.setGroupId(222);
notification1.setGroupMessage("New resources arrived");
notification1.setGroupTitle("Receive your resources");

final int time2 = MRGS.timeUnix() + 15;
MRGSPushNotification notification2 = MRGSPushNotification.create("Message number 2", 1, time2);
notification2.setGroupId(222);
notification2.setGroupMessage("New resources arrived");
notification2.setGroupTitle("Receive your resources");

final MRGSNotificationCenter notificationCenter = MRGSNotificationCenter.getInstance();
notificationCenter.addLocalPush(notification1);
notificationCenter.addLocalPush(notification2);

Custom layout in notifications🔗

Android Custom Notification1

To use your own layout of notifications on Android, you need to make the following steps:

  • Create a layout in the Android resources. Use the (res/layout) directory.
  • You need to put the images that you want to use inside this markup into the resources (res/drawable).
  • Text can be set directly, or also downloaded from Android resources.

Example:

int time = MRGS.timeUnix() + 10;
MRGSPushNotification notification = MRGSPushNotification.create("Custom push notification", 1, time);
//animated_notification - the name of the markup file (res/layout/animated_notification.xml)
notification.setCustomViewResId(R.layout.animated_notification);
//notification_icon - the ImageView id inside the markup (<ImageView android:id="@+id/notification_icon">)
//app_icon - the icon in resources (res/drawable/app_icon.png)
//context - application context
notification.setCustomViewImage("notification_icon", "app_icon", context);
//notification_text1 - the TextView id inside the markup (<TextView android:id="@+id/notification_text1">)
//app_name - string resource (res/values/strings.xml <string name="app_name">Unity</string>)
//context - application context
notification.setCustomViewText("notification_text1", "app_name", context);
//notification_text2 - the TextView id inside the markup (<TextView android:id="@+id/notification_text2">)
//Hello, World - the text is not from resources for display in the notification
//context - application context
notification.setCustomViewTextString("notification_text2", "Hello, World!", context);

Changing notifications state🔗

MRGS allows you to easily enable or disable specific types of notifications on a user's device. This functionality is useful for projects where in the settings there is a switch on the availability of notifications. So you can control the availability of local and push notifications:

To enable or disable local notifications, use the methods

import games.my.mrgs.notifications.MRGSNotificationCenter;
import static games.my.mrgs.notifications.MRGSNotificationCenter.NOTIFICATIONS_TYPE_LOCAL;

MRGSNotificationCenter.getInstance().enableNotifications(NOTIFICATIONS_TYPE_LOCAL);
MRGSNotificationCenter.getInstance().disableNotifications(NOTIFICATIONS_TYPE_LOCAL);

To enable or disable push notifications, use the methods

import games.my.mrgs.notifications.MRGSNotificationCenter;
import static games.my.mrgs.notifications.MRGSNotificationCenter.NOTIFICATIONS_TYPE_REMOTE;

MRGSNotificationCenter.getInstance().enableNotifications(NOTIFICATIONS_TYPE_REMOTE);
MRGSNotificationCenter.getInstance().disableNotifications(NOTIFICATIONS_TYPE_REMOTE);

To enable or disable both local and push notifications, use the methods

import games.my.mrgs.notifications.MRGSNotificationCenter;
import static games.my.mrgs.notifications.MRGSNotificationCenter.NOTIFICATIONS_TYPE_ALL;

MRGSNotificationCenter.getInstance().enableNotifications(NOTIFICATIONS_TYPE_ALL);
MRGSNotificationCenter.getInstance().disableNotifications(NOTIFICATIONS_TYPE_ALL);

Android 13

For Android version 13 and above, when notifications are enabled, the user will be asked for permission to show notifications if this request was not previously made.

To check the status of local and push notifications

import games.my.mrgs.notifications.MRGSNotificationCenter;
import static games.my.mrgs.notifications.MRGSNotificationCenter.NOTIFICATIONS_TYPE_ALL;
import static games.my.mrgs.notifications.MRGSNotificationCenter.NOTIFICATIONS_TYPE_LOCAL;
import static games.my.mrgs.notifications.MRGSNotificationCenter.NOTIFICATIONS_TYPE_REMOTE;

boolean isLocalEnabled = MRGSNotificationCenter.getInstance().isNotificationsEnabled(NOTIFICATIONS_TYPE_LOCAL);
boolean isRemoteEnabled = MRGSNotificationCenter.getInstance().isNotificationsEnabled(NOTIFICATIONS_TYPE_REMOTE);
boolean isAllEnabled = MRGSNotificationCenter.getInstance().isNotificationsEnabled(NOTIFICATIONS_TYPE_ALL);

Control notifications removing at application start🔗

At application start, MRGS removes all are received notifications from notification tray for current application. To change this behavior:

import games.my.mrgs.MRGServiceParams;

final MRGServiceParams params = ...;
params.setClearNotificationTrayEnabled(false);

Last update: 2023-10-25
Created: 2020-02-17