Unity Push Guide
This article will show you how to use push notifications in your Unity project. We recommend that you take a look at Push Notification Overview if you haven’t already.
Currently we only support iOS and FCM.
Getting Started
iOS
Please first obtain an iOS push notification certificate by following APNs Configuration Guide.
Android
Please first apply for push notification permissions from FCM by following Android Mixpush Guide.
Note that you only need to follow the guide to apply for push notification permissions from FCM. You don’t need to follow the guide to set up push notifications for Android.
Integrate the Push Notification Service
Install the SDK
You can download the latest unity-push.unitypackage
or unity-push-without-gradle.unitypackage
from SDK Releases.
If your project has “no” other Android Gradle configurations, you can download unity-push.unitypackage
. This package includes all iOS and Android configurations. You will only need to configure the parameters for different vendors.
If your project “has” other Android Gradle configurations, you’ll need to download unity-push-without-gradle.unitypackage
. This package doesn’t contain Android Gradle configurations related to push notifications. You will need to provide those configurations yourself.
The SDK cannot be installed with UPM because it involves Android Gradle configurations.
Import the LeanCloud-SDK-Realtime-Unity
Package:
Method 1: Use Unity Package Manager
Add the following dependency to the Packages/manifest.json
file in your project:
"dependencies":{
"com.leancloud.realtime": "https://github.com/leancloud/csharp-sdk-upm.git#realtime-2.3.0"
}
You can view all installed packages by going to Window > Package Manager.
Method 2: Import Manually
Locate the LeanCloud C# SDK download URL on Downloads and download
LeanCloud-SDK-Realtime-Unity.zip
.Unzip
LeanCloud-SDK-Realtime-Unity.zip
and drag the Plugins directory to Unity.
Set Up
iOS
Provide the iOS developer’s TeamId during initialization. See Initialization.
Android
To initialize FCM:
LCFCMPushManager.RegisterFCMPush();
Installation
The SDK comes with Installation objects that can be used to hold tokens and other data needed for push notifications.
LCInstallation lcInstallation = await LCInstallation.GetCurrent();
Send Push Notifications
By default, Prevent clients from sending push notifications is checked in Developer Center > Your game > Game Services > Cloud Services > Push Notification > Settings so clients won’t be able to send push notifications to other devices without restriction. We recommend leaving this option enabled and sending push notifications through the REST API or the dashboard.
If you need to send push notifications from clients, uncheck this option.
Send Push Notifications to All Devices
try {
LCPush push = new LCPush {
Data = new Dictionary<string, object> {
{ "alert", pushData }
}
};
await push.Send();
} catch (Exception e) {
Debug.LogError(e);
}
Send Push Notifications to Specific Users
Below is an example of how to send a push notification from a client to an iOS device in the test environment:
try {
LCPush push = new LCPush {
Data = new Dictionary<string, object> {
{ "alert", pushData }
},
IOSEnvironment = LCPush.IOSEnvironmentDev,
};
LCInstallation installation = await LCInstallation.GetCurrent();
push.Query.WhereEqualTo("objectId", installation.ObjectId);
await push.Send();
} catch (Exception e) {
Debug.LogError(e);
}
Respond to Push Notifications
Message Format
For more information about the message format, see Push Notification REST API Guide. For Android devices, the default message content parameter contains the following properties:
{
"alert": "Message content",
"title": "The title that appears in the notification center",
"custom-key": "Custom properties; custom-key is just an example and you can use any other keys"
}
Perform an Action When a User Taps a Push Notification
When a user taps a push notification to open your app, the Unity scene might not be initialized. This means that Unity may not know which notification was tapped.
To provide the notification parameters to Unity, the SDK will cache the parameters in the native layer when sending push notifications. It provides a C# interface for your program to retrieve the parameters:
Dictionary<string, object> launchData = await LCPushBridge.Instance.GetLaunchData();
Verification
Once you have initialized the SDK in your project and run your project on a real iOS or Android device, an entry with the device information will be created in the _Installation
table.
You can send a test push notification with custom conditions on Developer Center > Your game > Game Services > Cloud Services > Push Notification > Send notifications. This will help you verify that your device can receive push notifications. You can send the push notification by providing the objectId of the Installation. For iOS you can also provide the deviceToken and for Android you can use the registrationId.
FAQ
How do I remove the push notification services provided by certain vendors?
The SDK includes vendor SDKs for some Chinese Android vendors. You can remove them to reduce the size of the package.
- Delete
Assets/LeanCloud/Push/Android/xx
. Herexx
is the vendor name such asHuaWei
andXiaoMi
. - Delete
dependencies
fromAssets/Plugins/Android/mainTemplate.gradle
. - Delete vendor SDKs from
Assets/Plugins/Android/AndroidManifest.xml
(see to the comments in the file for more information).