如何在Unity 游戏中添加Push 功能
支持 Android 和 iOS Push 通知的新版 Kii Unity SDK。可以帮助用户如何建立 Unity 游戏并让 Android 玩家收到 Push 通知。
操作方法
- 01
在 Kii 开发者平台上创建一个 App
- 02
在游戏后端安装 Kii Unity 库
- 03
使用提供的 App ID 和 Key 在游戏中初始化 Kii 后端
- 04
在 Google 设置 App Push
- 05
使用 Google 的 GCM API key 在 Kii 开发者平台配置游戏。
方法/步骤2
- 01
导入 KiiPushPlugin.unitypackage 的内容到Unity 游戏中
- 02
更换 Plugins/Android/AndroidManifest.xml 的包名
- 03
创建一个空的 GameObject 并附上KiiPushPlugin .cs,同时填写 Sender ID
- 04
添加以下代码来处理 Push 通知 (RegisterPush( ) 和 安装( ))
代码区
- 01
代码 //Set up push listeners KiiPushPlugin kiiPushPlugin = GameObject.Find (“KiiPushPlugin”).GetComponent<KiiPushPlugin> (); Debug.Log (“Found KiiPushPlugin object in game objects”); kiiPushPlugin.OnPushMessageReceived += (ReceivedMessage message) => { // This event handler is called when received the push message. switch (message.PushMessageType) { case ReceivedMessage.MessageType.PUSH_TO_APP: Debug.Log (“#####PUSH_TO_APP Message”); // do something to notify your app of the incomig message break; case ReceivedMessage.MessageType.PUSH_TO_USER: Debug.Log (“#####PUSH_TO_USER Message”); // your user received a message, do something break; case ReceivedMessage.MessageType.DIRECT_PUSH: Debug.Log (“#####DIRECT_PUSH Message”); // A direct push message was sent from developer.kii.com // Let’s grab the url value of the message and open that page string url = message.GetString(“url”); Debug.Log (“Url in message is: ” + url); Application.OpenURL(“http://” + url); break; } Debug.Log(“Type=” + message.PushMessageType); Debug.Log(“Sender=” + message.Sender); Debug.Log(“Scope=” + message.ObjectScope); // You can get the value of custom field using GetXXXX method. Debug.Log(“Payload=” + message.GetString(“payload”)); }; #if UNITY_IPHONE KiiPushInstallationDeviceType deviceType = KiiPushInstallation.DeviceType.IOS; #elif UNITY_ANDROID KiiPushInstallationDeviceType deviceType = KiiPushInstallation.DeviceType.ANDROID; #else KiiPushInstallationDeviceType deviceType = KiiPushInstallation.DeviceType.ANDROID; #endif kiiPushPlugin.RegisterPush((string pushToken, Exception e0)=> { if (e0 != null) { Debug.Log(“#####failed to RegisterPush”); return; } Debug.Log (“#####RegistrationId=” + pushToken); Debug.Log (“#####Install”); KiiUser.PushInstallation (true).Install (pushToken, deviceType, (Exception e3)=>{ if (e3 != null) { Debug.Log (“#####failed to Install”); return; } }); });