写点什么

MobPush Android For Unity

  • 2023-07-18
    上海
  • 本文字数:2951 字

    阅读完需:约 10 分钟

本文档以 unity2020.3.41 演示

集成准备

注册账号

使用MobSDK之前,需要先在MobTech官网注册开发者账号,并获取 MobTech 提供的 AppKey 和 AppSecret,详情可以点击查看注册流程

下载.unitypackage 包

打开 Github 下载 MobPush-For-Unity 项目,下载完成后直接双击或者在 Unity 里面选择打开 MobPush.unitypackage,导入相关资源和脚本到您的 Unity 项目即可使用。


导入 unitypackage

全部选择即可(其中Demo.cs 为API使用和页面示例,可删除)
复制代码


修改 unity 编译环境

Android 集成编译配置

资源修改

删掉 Android 目录下的 baseProjectTemplate.gradle 、launcherTemplate.gradle 文件


修改 unity 配置

使用系统的 gradle 配置文件


增加 MobPush 的 gradle 配置

修改 baseProjectTemplate.gradle 和 launcherTemplate.gradle(注:此处修改为新生成的 baseProjectTemplate.gradle 文件和 launcherTemplate.gradle 文件)



baseProjectTemplate.gradle1.在 classpath 'com.android.tools.build:gradle'后添加 mob 的 classpath


classpath "com.mob.sdk:MobSDK:+"
复制代码


2.添加MobPush需要的 maven 地址


maven {    url "https://mvn.mob.com/android/"}
复制代码


3.参考示例如截图



launcherTemplate.gradle1.修改 launcherTemplate.gradle 增加MobPush配置


apply plugin: 'com.mob.sdk'MobSDK {    appKey "xxxxxxxxx"    appSecret "xxxxxxxxxx"    MobPush {        debugLevel 4        devInfo {            HUAWEI{                 appId "xxxxxxxxx"             }            XIAOMI {                appId "xxxxxxx"                appKey "5581830029242"            }            MEIZU {                appId "xxxxx"                appKey "3fc6d1acc7ea4f90a0304967ee3a74ae"            }            OPPO {                appKey "xxxxxxxx"                appSecret "c850609d8a0f492f8b9eeca1189aaec2"            }            VIVO {                appId "xxxxxx"                appKey "9b01729c-6140-4ad3-ac79-4c4543e12130"            }        }    }}
复制代码


2.参考示例截图



##在 gradle.properties 中添加代码


MobSDK.spEdition=FP
复制代码



挂载 MobPush 如图

配置签名文件和包名

1.配置自己项目的签名文件



2.配置自己项目的包名


设置隐私授权回调

为保证您的 App 在集成MobSDK之后能够满足工信部相关合规要求,您应确保 App 安装首次冷启动且取得用户阅读您《隐私政策》授权之后,调用 Mob 提交到的隐私协议回传函数 uploadPrivacyPermissionStatus 回传隐私协议授权结果。 反之,如果用户不同意您 App《隐私政策》授权,则不能调用 uploadPrivacyPermissionStatus 回传隐私协议授权结果。 详情参考:合规指南


//隐私授权接口调用,此接口务必不能漏调用,否则导致SDK不生效mobPush.updatePrivacyPermissionStatus(true);
复制代码

推送接口

初始化和绑定监听(gameObject.GetComponent)

void Start ()    {
mobPush = gameObject.GetComponent();//初始化MobPush mobPush.onNotifyCallback = OnNitifyHandler;//消息回调监听 mobPush.onTagsCallback = OnTagsHandler;//标签处理回调监听 mobPush.onAliasCallback = OnAliasHandler;//别名处理回调监听 mobPush.onDemoReqCallback = OnDemoReqHandler;
//demo请求接口回调(为了方便测试,提供在客户端发送通知的接口,仅供测试时使用) mobPush.onRegIdCallback = OnRegIdHandler;//获取注册ID异步监听回调接口 }
void OnNitifyHandler (int action, Hashtable resulte) { Debug.Log ("OnNitifyHandler"); if (action == ResponseState.CoutomMessage) { Debug.Log ("CoutomMessage:" + MiniJSON.jsonEncode(resulte)); } else if (action == ResponseState.MessageRecvice) { Debug.Log ("MessageRecvice:" + MiniJSON.jsonEncode(resulte)); } else if (action == ResponseState.MessageOpened) { Debug.Log ("MessageOpened:" + MiniJSON.jsonEncode(resulte)); } }void OnTagsHandler (int action, string[] tags, int operation, int errorCode) {
Debug.Log ("OnTagsHandler action:" + action + " tags:" + String.Join (",", tags) + " operation:" + operation + "errorCode:" + errorCode); }void OnAliasHandler (int action, string alias, int operation, int errorCode) { Debug.Log ("OnAliasHandler action:" + action + " alias:" + alias + " operation:" + operation + "errorCode:" + errorCode); }void OnRegIdHandler (string regId) { Debug.Log ("OnRegIdHandler-regId:" + regId); }void OnDemoReqHandler (bool isSuccess) { Debug.Log ("OnDemoReqHandler:" + isSuccess); }
复制代码

发送本地通知(LocalNotifyStyle )

LocalNotifyStyle style = new LocalNotifyStyle ();style.setContent ("Text");style.setTitle ("title");
#if UNITY_ANDROIDHashtable extras = new Hashtable ();extras["key1"] = "value1";extras["key2"] = "value1";style.setExtras (extras);//指定时间,当前时间加上设置这个Timestamp的值之后下发,单位msstyle.setTimestamp(180000);#endifmobPush.setMobPushLocalNotification (style);
复制代码

自定义通知栏样式( CustomNotifyStyle)

CustomNotifyStyle style = new CustomNotifyStyle ();
#if UNITY_IPHONEstyle.setType(CustomNotifyStyle.AuthorizationType.Badge | CustomNotifyStyle.AuthorizationType.Sound | CustomNotifyStyle.AuthorizationType.Alert);
#elif UNITY_ANDROID
style.setContent ("Content");style.setTitle ("Title");style.setTickerText ("TickerText");
#endifmobPush.setCustomNotification(style);
复制代码

获取注册 ID (getRegistrationId)

mobPush.getRegistrationId();
复制代码

添加标签 (addTags)

String[] tags = { "tags1", "tags2", "tags3" };mobPush.addTags(tags);
复制代码

获取标签 (getTags)

mobPush.getTags();
复制代码

删除标签 (deleteTags)

String[] tags = { "tags1", "tags2", "tags3" };mobPush.deleteTags(tags);
复制代码

清除全部标签 (cleanAllTags )

mobPush.cleanAllTags();
复制代码

添加别名 (addAlias)

mobPush.addAlias("alias");
复制代码

获取别名 (getAlias)

mobPush.getAlias();
复制代码

清除别名 (cleanAllAlias)

mobPush.cleanAllAlias();
复制代码

停止通知服务 (stopPush)

mobPush.stopPush();
复制代码

重启通知服务 (restartPush)

mobPush.restartPush();
复制代码

判断通知是否被停止,返回值:bool 类型(isPushStopped)

mobPush.isPushStopped();
复制代码

点击通知后是否打开应用首页(setClickNotificationToLaunchPage)

mobPush.setClickNotificationToLaunchPage(false);
复制代码

添加混淆配置

为了防止二次混淆MobPush,需要在项目混淆文件中添加:


-keep class com.mob.**{*;}
-dontwarn com.mob.**
复制代码


如果同时集成了华为、小米、魅族等渠道推送,同时也需要在项目中添加防二次混淆配置:


-keep class com.huawei.**{*;}
-keep class com.meizu.**{*;}
-keep class com.xiaomi.**{*;}
-keep class android.os.SystemProperties
复制代码


发布于: 刚刚阅读数: 4
用户头像

还未添加个人签名 2019-05-08 加入

还未添加个人简介

评论

发布
暂无评论
MobPush Android For Unity_开发者_MobTech袤博科技_InfoQ写作社区