05/15/2024 15:19:51

推送模块

一、概述

推送功能分为本地推送与远程推送,可以在游戏未运行的情况下给玩家手机推送游戏相关的信息,如各种节假日活动、周年庆活动等。

支持的渠道有:

  1. TPNS-腾讯云版本
  2. Firebase

Android 设备的运行效果如下:

推送效果

二、接入向导

iOS 推送依赖苹果 APNS 实现,需要各业务 PM 或腾讯侧接口人在 飞鹰系统 上配置相关证书。

2.1 注册回调

1)功能描述

接受 MSDK Push模块的回调,游戏需要注册回调函数进行处理。

2)接口声明

C#
C++
/// Push的基本回调
public static event OnMSDKRetEventHandler<MSDKBaseRet> PushBaseRetEvent;

/// Push通知的回调
public static event OnMSDKRetEventHandler<MSDKPushRet> PushNotificationEvent;
public static void SetPushObserver(MSDKPushObserver *pushObserver)

class MSDKPushObserver
{
public:
    virtual ~MSDKPushObserver() {};
	// Push的基本回调
    virtual void OnPushOptNotify(const MSDKBaseRet &baseRet) {};
	// Push通知的回调
    virtual void OnReceiveNotification(const MSDKPushRet &pushRet) {};
};

3)示例代码

C#
C++
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);
}

//销毁的时候需要设置回调为 null
private void OnDestroy ()
{
	MSDKPush.PushBaseRetEvent -= OnPushBaseRetEvent;
	MSDKPush.PushNotificationEvent -= OnPushNotificationEvent;
}
MSDKPush::SetPushObserver(new MyPushObserver());

class MyPushObserver : public MSDKPushObserver {
public:
    void OnPushOptNotify(const MSDKBaseRet &baseRet) {

        UMSDKDemoBase::showNormalAlert(baseRet);
    };

    void OnReceiveNotification(const MSDKPushRet &pushRet) {
        UMSDKDemoBase::showNormalAlert(pushRet);
    };
};

4)数据结构

MSDKPushRet 详解
继承自 MSDKBaseRet,包含基础的信息

成员变量名称 类型 说明
Type int 收到消息类型
-1:未知
0:应用在前台时收到的远程通知
1:应用在后台时收到的远程通知
2:应用在前台时收到的本地通知
3:应用在后台时收到的本地通知
Notification string 收到消息内容

[warning] MSDK 5.13 版本开始,增加 Firebase Android 远程推送的回调。

  1. 如果 APP 在前台时收到 push,直接处理回调
    通过 MSDKPushRet 的 notification 字段透传 notification text, extraJson 字段透传 custom data 给业务
  2. 如果 APP 在后台,push 显示到通知栏,用户点击通知栏的通知,拉起 APP
    通过 MSDKPushRet 的 extraJson 字段透传 custom data 给业务
    notification text 由于已经在通知栏中显示,不再透传

2.2 注册推送和注销推送

1)功能描述

注册指定渠道推送,可以接收这个渠道的消息推送。在绑定设备注册的基础上,同时可以绑定账号,使用指定的账号,这样可以通过后台向指定的账号发送推送消息。

如需定向推送请注意,MSDK5.12版本之前,Android:该接口需要额外调用绑定接口setAccount;iOS:注册接口MSDK内部已包含绑定接口setAccount,无需调用setAccount。

2)接口声明

C#
C++
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)入参说明

参数名称 参数类型 说明
channel string 三方渠道专有名词,比如 "XG", "Firebase"
account string 绑定的账号,绑定后可以针对账号发送推送消息,account不能为单个字符如“2”,“a”,

4)调用示例

C#
C++
MSDKPush.RegisterPush ("XG", "msdk的openid");
MSDKPush.UnregisterPush ("XG");
MSDKPush::RegisterPush ("XG", "msdk的openid");
MSDKPush::UnregisterPush ("XG");

2.3 注册标签推送和注销标签推送

1)功能描述

游戏可以针对用户设置标签,如性别、年龄、学历、爱好等,推送时可根据不同的标签有针对的进行推送。另外说明,XG SDK 有默认标签,详情可咨询 TPNS_helper(腾讯移动推送助手)。

发送标签推送消息同发送推送消息一样,只是在添加推送消息时发送人群范围需选择个性化推送。

[warning] 标签格式
设置标签时注意标签中不可包含空格

2)接口声明

C#
C++
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)入参说明

参数名称 参数类型 说明
channel string 三方渠道专有名词,比如 "XG", "Firebase"
tag string 标签,不能为 null 或包含空格

4)调用示例

C#
C++
//设置标签
MSDKPush.SetTag ("XG", "标签信息");
//删除标签
MSDKPush.DeleteTag ("XG", "标签信息");
//设置标签
MSDKPush::SetTag ("XG", "标签信息");
//删除标签
MSDKPush::DeleteTag ("XG", "标签信息");

2.4 增加本地推送和清空本地推送

1)功能描述

本地通知由用户自定义设置,保存在本地,根据时间触发。(当设置的时间小于当前设备时间,通知立即弹出。) Android 和 iOS的消息结构体不一样的。

[warning] MSDK 从 V5.30.001 版本开始,Firebase 渠道新增 Android 本地推送能力。使用说明

2)接口声明

C#
C++
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)入参说明

参数名称 参数类型 说明
channel string 三方渠道专有名词,比如 "XG", "Firebase"
localNotification MSDKLocalNotification 本地消息结构体,Android 和 iOS不同

4)调用示例

C#
C++
#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 测试";
	message.AlertAction = "MSDK Action";
	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 = "有个什么活动,大家赶紧去王城城门集合";
	message.Title = "MSDK Push Android 测试";
	// 马上通知,本地推送会有五分钟的弹性时间,如果小于当前时间就是马上通知
	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 = "有个什么活动,大家赶紧去王城城门集合";
	message.Title = "MSDK Push Android 测试";
	// 如果小于当前时间就是马上通知
	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 本地推送测试";
    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 = "有个什么活动,大家赶紧去王城城门集合";
    message.title = "MSDK Push 测试";
    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);
    // 马上通知,本地推送会有五分钟的弹性时间,如果小于当前时间就是马上通知
    MSDKPush::AddLocalNotification ("XG", message);
	
    //Firebase
    MSDKLocalNotification message;
    message.iconType = 0;
    message.lights = 0;
    message.ring = 0;
    message.vibrate = 0;
    message.content = "有个什么活动,大家赶紧去王城城门集合";
    message.title = "MSDK Push 测试";
    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);
    // 如果小于当前时间就是马上通知
    MSDKPush::AddLocalNotification ("Firebase", message);
	
#endif

2.5 设置账号和删除账号

当前仅 XG 渠道支持设置账号和删除账号,Firebase 渠道不支持。

1)功能描述

针对不同用户设置不同账号,推送时可根据设置的账号有针对的进行推送。

2)接口声明

C#
C++
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)入参说明

参数名称 参数类型 说明
channel string 三方渠道专有名词,比如 "XG", "Firebase"
account string 设置的账号,设置账号后可以针对账号发送推送消息,account不能为单个字符如“2”,“a”

4)调用示例

C#
C++
MSDKPush.setAccount ("XG", "msdk的openid");
MSDKPush.deleteAccount ("XG", "msdk的openid");
MSDKPush::SetAccount ("XG", "msdk的openid");
MSDKPush::DeleteAccount ("XG", "msdk的openid");

2.6 清空图标推送条目

当前仅 iOS 支持

1)功能描述

将应用桌面图标右上角的推送条目清空。

2)接口声明

C#
C++
public static void CleanBadgeNumber ();
public static void cleanBadgeNumber();

3)调用示例

C#
C++
MSDKPush.CleanBadgeNumber();
MSDKPush::cleanBadgeNumber();

三、数据结构说明

3.1 MSDKLocalNotification Android 结构体说明

成员变量名称 类型 说明
Type int 设置本地消息类型,1:通知,2:消息
详情可参考 腾讯移动推送TPNS接口文档
ActionType int 设置动作类型:1:打开activity或app本身,2:打开浏览器,3:打开Intent,4:通过包名打开应用
IconType int 通知栏图标是默认应用图标还是自定义图标(0是默认应用图标,1是自定义图标,默认0)
Lights int 是否呼吸灯(0否,1是,默认1)
Vibrate int 是否振动(0否,1是,默认1)
StyleID int 是否覆盖原先build_id的保存设置。1覆盖,0不覆盖
BuilderID long 设置消息样式,默认为0或不设置
Content string 设置消息内容
CustomContent string 消息自定义key-value
Activity string 设置拉起应用页面
PackageDownloadUrl string 设置下载应用URL;Firebase 渠道当前不支持该字段
PackageName string 拉起其他应用的包名,当动作类型为4时有效;Firebase 渠道当前不支持该字段
IconRes string 应用内图标文件名(xg.png)或者下载图标的url地址,例如:xg或者图片url
Date string 设置消息日期,格式为:20140502
Hour string 设置消息触发的小时(24小时制),例如:22代表晚上10点
Min string 获取消息触发的分钟,例如:05代表05分
Title string 设置消息标题
Intent string 设置打开intent,例如(10086拨号界面)intent:10086#Intent;scheme=tel;action=android.intent.action.DIAL;S.key=value;end当动作类型为3时有效;Firebase 渠道当前不支持该字段
Url string 打开 url,例如:http://www.qq.com 当动作类型为2时有效;Firebase 渠道当前不支持该字段
RingRaw string 指定应用内的声音(ring.mp3),例如:ring
SmallIcon string 指定状态栏的小图片(xg.png),例如:xg
Category string 设置 TPNS 本地消息分类,详情可参考 TPNS 文档 。当业务未显式设置时,MSDK 默认消息 Category 为 CATEGORY_MESSAGE 关于 Category 的官方说明
Importance int 设置 TPNS 本地消息的重要程度,详情可参考 TPNS 文档 。当业务未显式设置时,MSDK 默认消息 Importance 为 IMPORTANCE_DEFAULT 关于 Importance 的官方说明

MSDK 5.33.000 版本开始,新增 Category、 Importance:
厂商逐步对 App 开发者的本地通知根据分类进行限额限频,也以此保证终端用户不被过度骚扰,不同的消息分类主要通过通知 Category 进行区分。TPNS 结合厂商的要求,对于本地通知和自建通道下发通知增加了通知分类和 Importance 的支持。在增加本地通知时,可以指定通知 Category 和通知 Channel Importance。

3.2 MSDKLocalNotification iOS 结构体说明

成员变量名称 类型 说明
RepeatType int 推送重复发送周期。1-分钟,2-小时,3-天,4-星期,5-月,6-年,默认是0代表不重复
FireTime long 本地推送触发的时间
Badge int 角标
AlertBody string 推送的内容
AlertAction string 替换弹框的按钮文字内容(默认为"启动")
UserInfo List 自定义参数,可以用来标识推送和增加附加信息



Copyright © 2024 MSDK.
All rights reserved.

results matching ""

    No results matching ""