01/10/2025 10:53:00
Push Module
I. Overview
The push function is divided into local push and remote push. It can push game-related information to the player's cellphone when the game is not running, such as various holiday activities, anniversary activities, etc.
The supported channels include:
The running effect of Android device is as follows:
II. Access Guide
iOS push relies on Apple APNS to implement it; it requires each business PM or Tencent contact person to configure relevant certificates on the Feiying System
2.1 Register Callback
1) Function Description
Receive MSDK Push module's callback. The game needs to register a callback function for processing.
2) Interface Declaration
/// <summary>
/// Push's basic callback
/// </summary>
public static event OnMSDKRetEventHandler<MSDKBaseRet> PushBaseRetEvent;
/// <summary>
/// Push notification's callback
/// </summary>
public static event OnMSDKRetEventHandler<MSDKPushRet> PushNotificationEvent;
class MSDKPushObserver
{
public:
// Push's basic callback
virtual void OnPushOptNotify (const MSDKBaseRet &baseRet) {};
// Push notification's callback
virtual void OnReceiveNotification (const MSDKPushRet &pushRet) {};
};
3) Demo code
MSDKPush.PushBaseRetEvent += OnPushBaseRetEvent;
MSDKPush.PushNotificationEvent += OnPushNotificationEvent;
public void OnPushBaseRetEvent (MSDKBaseRet baseRet)
{
string methodTag = "";
if (baseRet.MethodNameId == (int) MSDKMethodNameID.MSDK_PUSH_REGISTER_PUSH) {
methodTag = "RegisterPush";
} else if (baseRet.MethodNameId == (int) MSDKMethodNameID.MSDK_PUSH_UNREGISTER_PUSH) {
methodTag = "UnregisterPush";
} else if (baseRet.MethodNameId == (int) MSDKMethodNameID.MSDK_PUSH_SET_TAG) {
methodTag = "SetTag";
} else if (baseRet.MethodNameId == (int) MSDKMethodNameID.MSDK_PUSH_DELETE_TAG) {
methodTag = "DeleteTag";
}
SampleInstance.showRetDialog (methodTag, baseRet);
}
public void OnPushNotificationEvent (MSDKPushRet pushRet)
{
string methodTag = "";
if (pushRet.MethodNameId == (int) MSDKMethodNameID.MSDK_PUSH_ADD_LOCAL_NOTIFICATION) {
methodTag = "AddLocalNotification";
} else if (pushRet.MethodNameId == (int) MSDKMethodNameID.MSDK_PUSH_CLEAR_LOCAL_NOTIFICATION) {
methodTag = "ClearLocalNotification";
} else if (pushRet.MethodNameId == (int) MSDKMethodNameID.MSDK_PUSH_NOTIFICAITON_CALLBACK) {
methodTag = "NotificationCallback";
}
SampleInstance.showRetDialog (methodTag, pushRet);
}
//Need to configure the callback as null when destroying it
private void OnDestroy ()
{
MSDKPush.PushBaseRetEvent -= OnPushBaseRetEvent;
MSDKPush.PushNotificationEvent -= OnPushNotificationEvent;
}
MSDKPush:: SetPushObserver (new MyPushObserver ());
class MyPushObserver: public MSDKPushObserver {
public:
void OnPushOptNotify (const MSDKBaseRet &baseRet) {
String ret = MSDKUtils::FormatJson(MSDKJsonManager::ToJson(baseRet));
UMSDKDemoBase::showNormalAlert(ret);
};
void OnReceiveNotification (const MSDKPushRet &pushRet) {
String ret = MSDKUtils::FormatJson(MSDKJsonManager::ToJson(pushRet));
UMSDKDemoBase::showNormalAlert(ret);
};
};
4) Data structure
MSDKPushRet explanation
Inherited from MSDKBaseRet; contain the basic information
Field Name | Type | Description |
---|---|---|
Type | int | Received message type 0: Click to enter - notification 1: Received at the front desk - notification 2: Click to enter - local notification 3: Received at the front desk - Local notification |
Notification | string | Received message content |
[warning]MSDK 5.13 version starts to add the callback of Firebase Android remote push.
- If APP receives a push at the front-end, the callback can be handled directly
notification text is transmitted through thenotification
field of MSDKPushRet, and theextraJson
field transmits custom data to the game- If APP pushes and displays a notification in the notification bar at the backend, the user clicks on the notification in the notification bar to launch the APP
Transmit custom data to the game via theextraJson
field of MSDKPushRet
notification text is no longer transmitted because it has been displayed in the notification bar
2.2 Register Push And Cancel Push
1) Function Description
Register push specified channel, so that you can receive the message push of the channel. On the basis of binding the device registration, you can also bind an account and use the specified account, so that you can send the push message to the specified account through the backend
If you want to make directional push, please note the following points. Before Version MSDK5.12,Android: this interface requires an additional call to the binding interface setAccount; iOS: inside MSDK, the registration interface already contains the binding interface setAccount, so it has no need to call setAccount.
2) Interface Declaration
public static void RegisterPush (string channel, string account="");
public static void UnregisterPush (string channel);
static void RegisterPush (const String &channel, const String &account = "");
static void UnregisterPush (const String &channel);
3) Parameter Description
Parameter Name | Parameter Type | Description |
---|---|---|
channel | string | third-party channel's roper noun, such as "XG", "Firebase" |
account | string | bound account; the push message can be sent to the bound account; account cannot be a single character such as "2", "a"; if you fill in NULL, use openID which has logged in successfully as the account |
4) Call example
MSDKPush.RegisterPush ("XG", " game openid");
MSDKPush.UnregisterPush ("XG");
MSDKPush:: RegisterPush ("XG", " game openid");
MSDKPush:: UnregisterPush ("XG");
2.3 Register Tag Push and Cancel Tag Push
1) Function Description
The game can configure tags for the user, such as gender, age, education level, hobby, etc. In addition, there are default tags preset in SDK.For details, please consult TPNS_helper (Tencent Mobile Push Assistant).
Sending a tag push message is the same as sending a push message, except that it needs to select personalized pushes for different groups when adding push messages
[warning] Tag format
Warning when configuring tags Any tag cannot contain any space
2) Interface Declaration
public static void SetTag (string channel, string tag);
public static void DeleteTag (string channel, string tag);
public static void SetTag (const String &channel, const String &tag);
static void DeleteTag (const String &channel, const String &tag);
3) Parameter Description
Parameter name | Parameter type | Description |
---|---|---|
channel | string | third-party channel's roper noun, such as "XG", "Firebase" |
tag | string | tag, which can't be null or contain any space |
4) Call example
// configure tag
MSDKPush.SetTag ("XG", "tag information");
// delete tag
MSDKPush.DeleteTag ("XG", "tag information");
// configure tag
MSDKPush:: SetTag ("XG", "tag information");
// delete tag
MSDKPush:: DeleteTag ("XG", "tag information");
2.4 Add Local Notification and Clear Local Notification
1) Function Description
The local notification is defined and configured by the user and stored in the local and is triggered by according to the time. (When the configured time is less than the current device time, the notification will pop up immediately.) The message structs of Android and iOS are not the same
[warning]Since MSDK 5.30.001, the Firebase channel has added Android local push capabilities. Instructions for Use
2) Interface Declaration
public static void AddLocalNotification (string channel, MSDKLocalNotification localNotification);
public static void ClearLocalNotifications (string channel);
static void AddLocalNotification (const String &channel,
const MSDKLocalNotification &localNotification);
static void ClearLocalNotifications (const String &channel);
3) Parameter Description
Parameter name | Parameter type | Description |
---|---|---|
channel | string | third-party channel's roper noun, such as "XG", "Firebase" |
localNotification | MSDKLocalNotification | local message struct; Android and iOS are different |
4) Call example
#if UNITY_IOS
MSDKLocalNotification message = new MSDKLocalNotification ();
TimeSpan ts = DateTime.UtcNow - new DateTime (1970, 1, 1, 0, 0, 0, 0);
message.FireTime = Convert.ToInt64 (ts.TotalSeconds) + 5;
message.AlertBody = "MSDK Push iOS test";
message.AlertAction = "There is an activity. Everyone, please hurry to Wangcheng City Gate to gather";
message.Badge = 2;
message.RepeatType = 1;
Dictionary<string, string> userInfo1 = new Dictionary<string, string> ();
userInfo1["msdkvakey1"] = "msdkvalue1";
Dictionary<string,string> userInfo2 = new Dictionary<string,string>();
userInfo2["msdkvakey2"] = "msdkvalue2";
message.UserInfo = new List<Dictionary<string, string>> ();
message.UserInfo.Add (userInfo1);
message.UserInfo.Add(userInfo2);
MSDKPush.AddLocalNotification ("XG", message);
#else
// XG
MSDKLocalNotification message = new MSDKLocalNotification ();
message.ActionType = 1;
message.Type = 1;
message.Content = "There is an activity. Everyone, please hurry to Wangcheng City Gate to gather";
message.Title = "MSDK Push Android test";
// Immediately notify. The local push will have a five-minute flexible time. If it is less than the current time, it will be notified immediately
message.Date = DateTime.Now.ToString ("yyyyMMdd");
message.Hour = DateTime.Now.Hour.ToString ();
message.Min = (DateTime.Now.Minute - 1).ToString ();
MSDKPush.AddLocalNotification ("XG", message);
//Firebase
MSDKLocalNotification message = new MSDKLocalNotification ();
message.Content="There is an event. Everyone hurries to gather at the gate of the Royal City";
message.Title="MSDK Push Android Test";
// If it is less than the current time, notify immediately
message.Date = DateTime.Now.ToString ("yyyyMMdd");
message.Hour = DateTime.Now.Hour.ToString ();
message.Min = (DateTime.Now.Minute - 1).ToString ();
MSDKPush.AddLocalNotification ("Firebase", message);
#endif
static std::string IntToString(int nSour) {
std::stringstream sTemp;
if (nSour < 10) {
sTemp << "0" <<nSour;
} else {
sTemp << nSour;
}
return sTemp.str();
}
#if __APPLE__
MSDKLocalNotification notification;
notification.fireTime = [[NSDate date] timeIntervalSince1970] + 5;
notification.alertBody = "MSDK local push test";
notification.alertAction = "MSDK Action";
notification.badge = 2;
notification.repeatType = 1;
notification.userInfo.insert(std::make_pair("MSDK1", "user info value"));
notification.userInfo.insert(std::make_pair("MSDK2", "user info value"));
MSDKPush::AddLocalNotification("", notification);
#elif ANDROID
// XG
MSDKLocalNotification message;
message.actionType = 1;
message.type = 1;
message.iconType = 0;
message.lights = 0;
message.ring = 0;
message.vibrate = 0;
message.styleID = 0;
message.builderID = 0;
message.content = "There is an event. Everyone hurries to gather at the gate of the Royal City";
message.title = "MSDK local push test";
time_t rawTime = 0;
time(&rawTime);
struct tm * timeInfo;
timeInfo = localtime(&rawTime);
std::string dateStr;
std::stringstream ss;
dateStr.append(IntToString(timeInfo->tm_year + 1900));
dateStr.append(IntToString(timeInfo->tm_mon + 1));
dateStr.append(IntToString(timeInfo->tm_mday));
message.date = dateStr;
message.hour = IntToString(timeInfo->tm_hour);
message.min = IntToString(timeInfo->tm_min);
// Immediately notify. The local push will have a five-minute flexible time. If it is less than the current time, it will be notified immediately
MSDKPush::AddLocalNotification ("XG", message);
//Firebase
MSDKLocalNotification message;
message.iconType = 0;
message.lights = 0;
message.ring = 0;
message.vibrate = 0;
message.content = "There is an event. Everyone hurries to gather at the gate of the Royal City";
message.title = "MSDK local push test";
time_t rawTime = 0;
time(&rawTime);
struct tm * timeInfo;
timeInfo = localtime(&rawTime);
std::string dateStr;
std::stringstream ss;
dateStr.append(IntToString(timeInfo->tm_year + 1900));
dateStr.append(IntToString(timeInfo->tm_mon + 1));
dateStr.append(IntToString(timeInfo->tm_mday));
message.date = dateStr;
message.hour = IntToString(timeInfo->tm_hour);
message.min = IntToString(timeInfo->tm_min);
// If it is less than the current time, notify immediately
MSDKPush::AddLocalNotification ("Firebase", message);
#endif
2.5 Set accounts and delete accounts
Currently, only XG channel supports setting accounts and deleting accounts, and Firebase channel does not support them
1) Function description
Set different accounts for different users. Messages can be pushed pointedly according to the set account.
2) Interface Declaration
public static void setAccount(String channel, String account);
public static void deleteAccount(String channel, String account);
static void SetAccount(const String &channel, const String &account);
static void DeleteAccount(const String &channel, const String &account);
3) Input parameter description
Parameter name | Parameter type | Description |
---|---|---|
channel | string | Dedicated term for a tripartite channel, such as "XG","Firebase" |
account | string | The set account; after setting a account, you can send push messages to the account; the account cannot be a single character such as "2", "a" |
4) Call example
MSDKPush.setAccount ("XG", "msdk's openid");
MSDKPush.deleteAccount ("XG", "msdk's openid");
MSDKPush::SetAccount ("XG", "msdk's openid");
MSDKPush::DeleteAccount ("XG", "msdk's openid");
2.6 Clear icon push items
Currently only supported by iOS
1) Function description
Clear all the push items in the upper right corner of the application desktop icon.
2) Interface Declaration
public static void CleanBadgeNumber ();
public static void cleanBadgeNumber();
3) Call example
MSDKPush.CleanBadgeNumber();
MSDKPush::cleanBadgeNumber();
III. Data Structure
3.1 MSDKLocalNotification Android Struct Description
Field Name | Type | Description |
---|---|---|
Type | int | configure local message type, 1: notification, 2: message For details, please refer to Tencent Mobile Push TPNS interface documentation |
ActionType | int | configure action type: 1 open activity or app itself, 2 open the browser, 3 open the Intent, 4 open the app via package name |
IconType | int | Is the notification bar icon an in-app icon or an upload icon? (0 is an in-app icon , 1 is an upload icon , default 0) |
Lights | int | Is it a breathing light? (0 No, 1 Yes, default 1) |
RingRaw | int | Whether to ring? (0 No, 1 Yes, default 1) |
Vibrate | int | Whether to vibrate? (0 No, 1 Yes, default 1) |
StyleID | int | Whether to overwrite the original configuration saved by build_id. 1 overwrite, 0 not overwrite |
BuilderID | long | configure message style; the default is 0 or not configure |
Content | string | configure message content |
CustomContent | string | custom the message, key-value |
Activity | string | configure the page of the launched app |
PackageDownloadUrl | string | configure the URL for downloading app; The Firebase channel currently does not support this field |
PackageName | string | launch the package name of other app; the current action type is valid within 4h; The Firebase channel currently does not support this field |
IconRes | string | in-app icon file name (xg.png) or the URL for downloading the icon, such as: xg or image url |
Date | string | configure message date, format: 20140502 |
Hour | string | configure the hour when the message is triggered (24-hour system), such as: 22 indicates 10pm |
Min | string | The minute when message is triggered, such as: 05 indicates 05 minutes |
Title | string | configure message title |
Intent | string | configure intent, such as (10086 dial UI) intent: 10086#Intent; scheme=tel; action=android.intent.action.DIAL; S.key=value; end; it is valid when the action type is 3; The Firebase channel currently does not support this field |
Url | string | Open url, such as: http://www.qq.com; it is valid when the action type is 2; The Firebase channel currently does not support this field |
RingRaw | string | Specify the in-app sound (ring.mp3), such as: ring |
SmallIcon | string | specify the small image (xg.png) of the status bar, such as: xg |
3.2 MSDKLocalNotification iOS Struct Description
Field Name | Type | Description |
---|---|---|
RepeatType | int | the repeated delivery cycle of push. 1-minute, 2-hour, 3-day, 4-week, 5-month, 6-year, default is 0, which indicates not repeating. |
FireTime | long | local push triggering time |
Badge | int | superscript |
AlertBody | string | push content |
AlertAction | string | replace the text content of the button in the pop-up box (the default is "start") |
UserInfo | List |
user-defined parameter, which can be used to identify push and add additional information |
All rights reserved.