|
如何建立 Unity 游戏并让 Android 玩家收到 Push 通知。与建立 iOS 游戏步骤十分相似,最大区别是 iOS 使用苹果推送通知服务 (APNS),而 Android 使用谷歌云消息 (GCM) 。
首先,需要告诉 Kii 和 Google 你想在游戏中使用 Push 通知。它采用5个不算困难的步骤。
1. 在 Kii 开发者平台上创建一个 App
2. 在您的游戏安装 Kii Unity 库
3. 使用提供的 App ID 和 Key 在游戏中初始化 Kii 后端
4. 在 Google 设置 App Push
5. 使用 Google 的 GCM API key 在 Kii 开发者平台配置您的游戏
最新的 Unity SDK (v1.8.2+) 有一份 KiiPushPlugin.unitypackage 文件,该文件提供所有 Unity 端的 Push 功能。当打开 Unity 文件后,双点击该文件可以完成导入操作。新文件将添加到您项目内的 Plugins 目录。(如果想更节约空间,可以剔除 iOS 相关文件)
随着底层的 Push 文件,你会发现一份 Plugins/KiiPushPlugin.cs 文件,它提供 Unity 层面的 Push 接口。打开 Plugins/Android/AndroidManifest.xml ,在你指定的 Unity Build 设置里更换任何一个 YOUR_APPLICATION_PACKAGE_NAME 的包名。
重要提示:在编辑清单时,不要触碰 <actionandroid:name=”PACKAGE_NAME.MESSAGE”/>
现在,将附加的 Game Object 初始化到 KiiPushPlugin.cs,然后 Google (Google API控制台 –> 仪表盘 -> Project ID) 分配给你的 App ID (仅适用于 Android)。
接下来,需要在 Unity 端编写一些处理 Push 通知的代码。代码示例如下:
//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;
}
});
});
尝试从 developer.kii.com 发送一个 Direct Push 消息给已登录的玩家。从您的控制台进入 项目中的 Push 页面并点击 “新邮件”。创建一个这样的信息:
当玩家在游戏中并你已经登录,点击 “发送…”。当设置的 URL 在 Android 客户端被打开时,表示发送成功!
简单而言,使用 Kii Unity SDK 在你 Android 游戏中添加 Push 通知的步骤如下:
1. 导入 KiiPushPlugin.unitypackage 的内容到您的 Unity 游戏中
2. 更换 Plugins/Android/AndroidManifest.xml 的包名
3. 创建一个空的 GameObject 并附上KiiPushPlugin .cs,同时填写 Sender ID
4. 添加如上的代码来处理 Push 通知 (RegisterPush( ) 和 安装( )) |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?注册
×
|