Connecting notifications on client🔗
To integrate notifications, you need to make a few simple steps:
Receiving notifications🔗
Step 1. Import the notification module and add permissions🔗
- Add the SDK to the project:
Step 1. Add Sources
To add MRGS to a project via the Unity Package Manager (available from Unity 2018+) simply add a scopedRegistries section to the Packages/manifest.json file by adding the following entry:
{
"dependencies": {
...
},
"scopedRegistries": [
{
"name": "MRGS",
"url": "https://mrgs-nexus.my.games/repository/mrgs-uninty-plugins/",
"scopes": [
"games.my.mrgs"
]
}
]
}
Alternatively, you can click Edit -> Project Settings -> Package Manager -> '+' in scoped registry section, and fill in the fields according to the data above.
Step 2. Add dependency
- Click
Window -> Package Manager -> select 'Packages: MyRegistries' from dropdown list, select packageMRGSNotificationsfrom the list, then click "Install" - Import the module:
using MRGS;
- Download the latest version of the library. Unzip the archive.
- (For unitypackage integration) In Unity, click
Assets -> Import Package -> Custom Package, and select thegames.my.mrgs.notifications.unitypackagepackage from the downloaded archive. - (For tgz integration) In Unity, click
Window -> Package Manager -> '+' -> Add package from tarball, and select thegames.my.mrgs.notifications-<version> package. tgzfrom the downloaded archive. - Import the module:
using MRGS;
- To connect server notifications on iOS, make sure that certificates for Developer and Release builds of your application are added in the admin panel of your application on the MRGS website.
- To connect server notifications on Android, make sure that the key for sending notifications is registered in the admin panel of your application on the MRGS website.
- For Android, follow the documentation on connecting Firebase to your project, the resulting google-services.json file must be in the root of the assembled Android application.
- For Huawei/AppTouch Store, follow the documentation on connecting Huawei Push Kit to your project, the resulting agconnect-services.json file must be in the root of the assembled Android application.
- For iOS, if you want to support push notifications, then you need to add the necessary permissions to work with them in the project settings in Xcode:
How to add necessary permissions in project settings in Xcode
Permission to work with push notifications:

And SilentPush notifications:

Alternatively, you can do it in the Unity project code in PostProcessBuild:
public class PostBuildProcessSample
{
[PostProcessBuild]
public static void PostProcessBuild(BuildTarget target, string path)
{
if (target == BuildTarget.iOS)
{
#if UNITY_IOS
string projPath = PBXProject.GetPBXProjectPath(path);
PBXProject proj = new PBXProject();
proj.ReadFromString(File.ReadAllText(projPath));
#if UNITY_2019_3_OR_NEWER
string projectTarget = proj.GetUnityMainTargetGuid();
#else
string targetName = PBXProject.GetUnityTargetName();
string projectTarget = proj.TargetGuidByName(targetName);
#endif
//Adding capabilities
#if UNITY_2019_3_OR_NEWER
ProjectCapabilityManager projectManager = new ProjectCapabilityManager(projPath, "ios.entitlements", targetGuid: projectTarget);
#else
ProjectCapabilityManager projectManager = new ProjectCapabilityManager(projPath, "ios.entitlements", targetName);
#endif
projectManager.AddPushNotifications(development: true);
projectManager.AddBackgroundModes(options: BackgroundModesOptions.RemoteNotifications);
projectManager.WriteToFile();
#endif
}
}
}
How to put google-services.json in the root of the android project.
By default, Unity copies all files from the Assets directory of the Unity project to the assets directory of the Android project. In order for the google-services.json file to appear in the root of the Android project, use a small script added at the end of the mainTemplate.gradle(For Unity 2017/2018) or launcherTemplate.gradle(For Unity 2019+) file:
How to put agconnect-services.json in the root of the android project.
By default, Unity copies all files from the Assets directory of the Unity project to the assets directory of the Android project. In order for the agconnect-services.json file to appear in the root of the Android project, use a small script added at the end of the mainTemplate.gradle (For Unity 2017/2018) or launcherTemplate.gradle(For Unity 2019+) file:
If you want to use location-based notifications.
If you are going to use local notifications based on the user's geolocation (for details see MRGSNotificationTrigger), then in the Info.plist of your application you need to add the NSLocationWhenInUseUsageDescription field (Xcode displays it as "Privacy - Location When In Use Usage Description"), in which you need to describe why you need permission to use the location, here's an example of the necessary part of Info.plist:
FirebaseMessaging.unitypackage can intercept notifications from the MRGS, which will lead to the fact that you will not see notifications.
Step 2. Parameter settings🔗
On launch of the MRGS SDK you need to add installation of the notification settings:
using MRGS;
public class MasterController : MonoBehaviour
{
void Awake()
{
MRGServiceParams serviceParams = new MRGServiceParams(MRGS_APP_ID, CLIENT_SECRET);
serviceParams.IOSExtraOptions.AllowPushNotifications = true; // Enabling notifications
serviceParams.IOSExtraOptions.ShouldResetBadge = true; // Reset the number on the icon when opening from background
// Further setup and initialization of MRGS
// ...
}
}
The ShouldResetBadge flag must be set to true if you want MRGS to work with the number on the application icon - when the application starts the number on the icon will be reset.
Further, after the SDK launch, MRGS will do everything itself, namely:
- Request permission to send notifications.
- If the user agrees, we will send deviceToken to the MRGS server to send Push notifications.
Permission to show Android 13 notifications
On Android prior to Android 13, the app can display notifications without asking the user's permission. Starting with Android 13, this will require permissions given by the user (same as on iOS).
Delaying permission request
If you need to request permission to send notifications not at launch, but later, or if you did not set the above flags and want to manually perform this process (registration), refer to the Manual control of registration and permission request section.
Trial notifications (for iOS)
iOS allows you to send notifications until the user allows them, which can then be allowed or refused.

If you want to use this type of permission, see the section Working with Trial (Provisional) Notifications on iOS
Requesting not all permissions (on iOS) (for example, only banners, excluding sounds and badges)
By default, we will request all available permissions: sounds, banners, badge on the icon. If you want to request only a part of them (for example, you can only request alerts and badges, excluding the sound), you can use the excludePermissionsForNotificationsAuthorizationRequest method. This method must be called before initializing MRGS at every start, we recommend calling it when setting parameters for starting MRGS. Example:
// MRGService settings
MRGServiceParams serviceParams = new MRGServiceParams(MRGS_APP_ID, CLIENT_SECRET);
// ...
// Exclusion of badges on the icon and sound, only banners will remain
MRGSNotificationCenter.ExcludePermissionsForNotificationsAuthorizationRequest(MRGSNotificationAuthorizationOptionType.Badge | MRGSNotificationAuthorizationOptionType.Sound);
// Further configuration and initialization of MRGS
// ...
You can leave only tracking.
Even if you do not want to work with notifications using MRGS, you can set the flags above in TRUE, and MRGS will simply watch for notifications arrival and send statistics to the server. The only condition for such a "non-interfering and quiet" work is that on IOS 10+ you must use the new UNUserNotificationCenter, otherwise, we can subscribe to its delegate, and notifications of incoming notifications will not reach the old delegate.
Step 3. Delegate setup🔗
Step 3.1. Notification receiving delegate🔗
To receive a callback about a notification arrival, or about a click on it, you must install and implement a delegate of the IMRGSNotificationCenterDelegate type.
// Setting a generic delegate without splitting by the type of notifications received (local or push)
// 'this' conforms to 'IMRGSNotificationCenterDelegate' protocol
MRGSNotificationCenter.Instance.Delegate = this;
When a notification is received or it is clicked on, the delegate method will be called:
void OnNotificationReceive(MRGSNotification notification){ //Receiving
}
void OnNotificationClick(MRGSNotification notification){ //Clicking
}
You can find out the type of notification received by using the
notification.Trigger.TriggerTypeparameter, which will be discussed below.
What if we want separate delegates for local and server notifications?
We have such a capability. In this case, you need to install and implement delegate types IMRGSNotificationCenterLocalDelegate and IMRGSNotificationCenterRemoteDelegate.
// Setting a separated delegate with a breakdown by the type of notifications received (separately local, separately push)
// 'this' conforms to 'IMRGSNotificationCenterRemoteDelegate' protocol
MRGSNotificationCenter.Instance.RemoteDelegate = this;
// 'this' conforms to 'IMRGSNotificationCenterLocalDelegate' protocol
MRGSNotificationCenter.Instance.LocalDelegate = this;
When a notification is received or it is clicked on, the delegate method will be called:
Step 3.2. Notification permission delegate (for iOS)🔗
If you want to receive a callback at the moment when the user clicks the "Allow notifications" button with information about their decision, you can use the IMRGSNotificationCenterAuthorizationDelegate delegate. The delegate must be set as soon as possible.
Example:
// Setting delegate
// 'this' conforms to 'IMRGSNotificationCenterAuthorizationDelegate' protocol
MRGSNotificationCenter.Instance.AuthorizationDelegate = this;
// Receiving callback
public void OnRequestUserNotificationsAuthorizationWithResult(bool granted)
{
string isGranted = granted ? "granted" : "not granted";
Debug.Log("Received callback that user has " + isGranted + " access to notifications" );
}
Working with notifications🔗
The main object that describes the notification is the MRGSNotification. It is necessary for sending local notifications, and also describes the notification received (both Push and local).
Important
Before working with the MRGSNotifications module, you need to initialize MRGService.
Step 1. Creation🔗
To create a notification object, use the following method:
Important!
When creating a notification, the identifier is an int type number, but the identifier field itself is a string type. How did this happen? It's simple: on Android, notifications have an int id and on iOS a string. That's why when a server Push notification on iOS arrives its identifier will be a string type. Therefore, during the creation, you use the int notification identifier, but when reading, you will receive a string.
Then you need to configure the notification:
notification.Title = "Hello"; //Notification header
notification.Subtitle = "Sub_Hello"; //Notification subtitle. (Only on iOS)
notification.Body = "From Unity"; //The body of the notification. (The main text of the notification.)
notification.Sound = "soundFile.caf"; //Custom sound
//Additional information added to the notification. (payload for Push notifications is located in this field)
notification.DeveloperPayload = new Dictionary<string,object>();
notification.DeveloperPayload["someInfoKey"] = "someImportantInfo";
//Setting up the trigger rule
//In this case, the notification will be displayed after 7 seconds.
notification.Trigger = MRGSNotificationTrigger.TriggerWithTimeInterval(timeInterval: 7, repeats: false);
We also provide the ability to "quickly" create notification objects - you can use the quick notification creation using the following methods:
MRGSNotification.Create(title: "_", body: "_", identifier: 1123, timeInterval: 7); //will trigger in 7 seconds
MRGSNotification.Create(title: "_", body: "_", identifier: 1123, trigger: TRIGGER); //where TRIGGER is an object of the MRGSNotificationTrigger class
MRGSNotification.Create(title: "_", body: "_", identifier: 1123, date: DATE); //where DATE is an object of DateTime class in UTC format
Default Values.
When creating the MRGSNotificationobject, the default fields are:
- Field sound = "default" - standart notification sound
- Field badge = 1
Step 2. Trigger rules🔗
MRGSNotificationTrigger is a class for setting or defining rules for triggering notifications. There are four types:
TimeInterval- Trigger with a set time interval (Will trigger after X seconds).DateComponents- Trigger with set date rules (For example, 15th day of the month, 15 hours 17 minutes (Then the trigger will fire on the next 15 hours 15 minutes local time.). (using Local time!)Location- Trigger based on the user's location (only for iOS).Remote- Trigger indicating that notification is the Push notification.
To create the selected trigger (except for Remote - it is created by MRGS upon receiving the notification) use the following method:
//Creating the kMRGSTimeIntervalTrigger trigger
MRGSNotificationTrigger* trigger = MRGSNotificationTrigger.TriggerWithTimeInterval(timeInterval: 70, repeats: false) //Will trigger after 70 seconds.
//Creating the kMRGSDateComponentsTrigger trigger
MRGSNotificationTrigger.MRGSDateComponents components = new MRGSNotificationTrigger.MRGSDateComponents();
components.minute = 30;
components.hour = 5;
components.day = 15;
MRGSNotificationTrigger* trigger = MRGSNotificationTrigger.TriggerWithDateComponents(dateComponents: components, repeats: false); //Will trigger on the nearest 15th of the month at 5 hours 30 minutes. (using Local time!)
//Creating the kMRGSLocationTrigger trigger
MRGSNotificationTrigger.MRGSLocation location = MRGSNotificationTrigger.MRGSLocation.Create(centerX: 1.32f, centerY: 1.325f, radius: 100.345f);
MRGSNotificationTrigger locationTrigger = MRGSNotificationTrigger.TriggerWithLocation(location: location, notifyOnEntry: true, notifyOnExit: true, repeats: false); //Will trigger on entering and leaving the specified area.
The repeats: parameter, when created, indicates whether the trigger will be repeated (for example, will it repeat every 70 seconds, every day at 13.00, or work each time user enters the specified location)). (only for iOS)
Don't forget to disable repeat
Otherwise, notifications will come endlessly while your notification is set. On IOS 8 and 9, notifications will be repeated a maximum of ten times, after that you will need to re-add the notification to the queue. When using the kMRGSTimeIntervalTrigger trigger, the minimum interval for using it repeatedly should be 60 seconds.
The trigger in the arrived notification will contain the triggerType field, from which you can determine whether the notification is local or remote, and it will also contain the corresponding fields with data for each type of trigger (interval, or date components, or location).
You can also use the quick creation of a trigger for a specific date in DateTime or long (for unixTime) format:
MRGSNotificationTrigger* trigger = MRGSNotificationTrigger.TriggerWithDate(DateTime.UtcNow.AddDays(5)); //In UTC
MRGSNotificationTrigger* trigger = MRGSNotificationTrigger.TriggerWithUnixTime(1287623847623); //In UTC
Step 3. Sending🔗
To send a notification, use the following method:
Example🔗
MRGSNotificationCenter.Instance.Delegate = this;
// Sending:
MRGSNotification notification = MRGSNotification.Create(title: "titleUnity", body: "bodyUnity", identifier: 1234, timeInterval: 7);
notification.DeveloperPayload = new Dictionary<string,object>();
notification.DeveloperPayload["unity1"] = "TESTUSERINFO";
notification.DeveloperPayload["unity2"] = 1;
notification.IosExtraOptions.ThreadIdentifier = "unityTheread1";
notification.IosExtraOptions.CategoryIdentifier = "categoryOnReturnNotifications";
notification.AndroidExtraOptions.GroupId = 3;
notification.AndroidExtraOptions.ChannelGroupId = "testChannel";
MRGSNotificationCenter.Instance.Add(notification);
// Receiving:
public void OnNotificationReceive(MRGSNotification notification)
{
bool isRemote = notification.Trigger.TriggerType == MRGSNotificationTrigger.Type.Remote;
Debug.Log("MRGSNotification received: " + notification);
Debug.Log("Remote: " + isRemote);
}
public void OnNotificationClick(MRGSNotification notification)
{
bool isRemote = notification.Trigger.TriggerType == MRGSNotificationTrigger.Type.Remote;
Debug.Log("MRGSNotification clicked: " + notification);
Debug.Log("Remote: " + isRemote);
}
Advanced notification settings, additional features🔗
MRGS allows you to work with all the features and novelties in notifications, namely:
- Request permission to send notifications at the time you need (on iOS).
- Get notification settings of the user.
- Work with delivered and scheduled notifications.
- Add buttons to notifications and customize their appearance.
- Manage notification grouping in IOS 12+, including the summary view and its arguments.
- Add attachments to notifications.
- Add custom sounds to notifications.
- The convenient way to turn on and off notifications on the client with just one method call, and we will take care of canceling notifications or invalidating the token (feature is useful for projects with a switch for notifications availability in the settings)
- Put the number on the iOS application icon.
- Work with Trial notifications on iOS.
To check out the documentation on these functions, go to the next section - customization and additional features.
Created: 2020-02-17