Unity+XiaoMiPush 对接小米推送sdk

来源:互联网 发布:剑桥大学研究生 知乎 编辑:程序博客网 时间:2024/04/29 23:47

登录小米服务平台注册应用

http://dev.xiaomi.com

客户端:

IOS客户端调试未成功,希望有调试成功的朋友可以知会一下我

Android端

1.打开AndroidManifest.xml修改包名,修改这三个位置就可以


2.打开test.cs修改AppID和AppKey,这两个参数从小米平台注册应用后获得:



3.在Unity3d游戏场景中,新建一个空的 Gameobject,将其名称修改为MiPushBinding,将使用推送的脚本test.cs挂载到这个GameObject上。

4使用推送的脚本要实现recvMessage(string s)方法,用来接收推送的信息。

5.发布运行后点击“register"按钮注册推送,然后可以通过小米推送工具测试推送功能。




Java服务端:

其实Java服务端非常简单:

private byte[] AndroidPush(@RequestParam String packagename,@RequestParam String alias, @RequestParam String messagePayload,@RequestParam String title,@RequestParam String description) throws Exception {     Constants.useOfficial();     JSONObject rt = new JSONObject();     rt.put("method", "androidpush");     Appsecret _appsecret=new Appsecret();     _appsecret.setPackagename(packagename);     _appsecret.setType(true);     Appsecret appsecret=appsecretServer.findByAppId(_appsecret);     if(appsecret==null)     {     rt.put("result", false);     rt.put("msg", "不存在appid");     return rt.toString().getBytes();     }          Sender sender = new Sender(appsecret.getAppsecret());     notifyId++;     if(notifyId>1000)     notifyId=0;     Message message = new Message.Builder()                .title(title)                .description(description).payload(messagePayload)                .restrictedPackageName(appsecret.getPackagename())                .passThrough(0)//设置消息是否通过透传的方式送给app,1表示透传消息,0表示通知栏消息。                .notifyType(1)     // 使用默认提示音提示                .notifyId(notifyId)                .extra(Constants.EXTRA_PARAM_NOTIFY_EFFECT,Constants.NOTIFY_LAUNCHER_ACTIVITY)                .extra(Constants.EXTRA_PARAM_NOTIFY_FOREGROUND, "0")//当app在前台时不想让客户端弹出通知                .build()                ;     Result result= sender.sendToAlias(message, alias, 0); //根据alias,发送消息到指定设备上,不重试。     rt.put("pushresult", result);     rt.put("result", true);     rt.put("msg", "不存在appid");     return rt.toString().getBytes();}/*** *  * @param appid 应用ID * @param alias  推送手机号,多个手机号用“,”隔开 * @param description 推送通知简介 * @return * @throws Exception */@RequestMapping(value="/iospush", method=RequestMethod.POST)@ResponseBodyprivate byte[] IOSPush(@RequestParam String packagename,@RequestParam String alias,@RequestParam String description) throws Exception { Constants.useOfficial();     JSONObject rt = new JSONObject();     rt.put("method", "androidpush");     Appsecret _appsecret=new Appsecret();     _appsecret.setPackagename(packagename);     _appsecret.setType(false);     Appsecret appsecret=appsecretServer.findByAppId(_appsecret);     if(appsecret==null)     {     rt.put("result", false);     rt.put("msg", "不存在appid");     return rt.toString().getBytes();     }     Sender sender = new Sender(appsecret.getAppsecret());     Message message = new Message.IOSBuilder()                .description(description)                .extra(Constants.EXTRA_PARAM_NOTIFY_EFFECT,Constants.NOTIFY_LAUNCHER_ACTIVITY)                .extra(Constants.EXTRA_PARAM_NOTIFY_FOREGROUND, "0")//当app在前台时不想让客户端弹出通知                .build()                ;     Result result= sender.sendToAlias(message, alias, 0); //根据alias,发送消息到指定设备上,不重试。     rt.put("pushresult", result);     rt.put("result", true);     rt.put("msg", "不存在appid");     return rt.toString().getBytes();}


发布运行时你会发现应用图标为默认安卓图标:res\drawable-hdpi下的图标,如果想使用unity BuildSetting里设置的图标那就需要修改一下:AndroidManifest.xml

android:icon="@drawable/ic_launcher"

改为:

android:icon="@drawable/app_icon"

然和就可以把res\drawable相关的图标删掉就可以了,如果你想偷懒也可以不删。






0 0