针对 iOS 的推送通知
先决条件
此教程假设您熟悉我们推送通知快速入门中涉及的概念。
配置 Apple 通知通道
- 确保您有有效的 iO 开发 (APNS_SANDBOX) 或生产 (APNS) 证书。 如果没有,请从 Apple Developer Portal 下的 Certificates, Identities and Profiles 中创建一个。
- 导出您选择的(开发或生产)证书:
首先从 Apple Developer Portal 的 Certificates, Identities and Profiles 下面下载证书副本。 这将创建一个 .cer 文件。
打开证书并将其安装到密钥链访问中。
在 Certificates 子类别下查看已安装的证书。
将密钥链访问证书导出为 .p12 格式。
使用以下主机命令将 .p12 文件转换为 .pem 文件:
openssl pkcs12 -in apns-dev-cert.p12 -out apns-dev-cert.pem -nodes -clcerts
有了 .pem 文件后,可通过 PlayFab Game Manager 在游戏的设置>Push Notifications 下直接上传它。
或者,使用以下生成器为
SetupPushNotification
创建一个 JSON 请求。
针对 SetupPushNotification 的 JSON 请求生成器
该请求生成器使用以下信息来创建 JSON 请求:
- Platform - 使用以下值:APNS (iOS)、APNS_SANDBOX (iOS)、GCM (Android)
- Application Name - 输入发送此消息的应用程序的名称。
注意
应用程序名称必须仅由大写和小写 ASCII 字母、数字、下划线、连字符和句点组成,并且其长度必须介于 1 到 256 个字符之间。 它们还 必须唯一。
- PEM 证书/API 密钥 - 对于 iOS(APNS 或 APNS_SANDBOX),请使用 PEM 文件的完整内容。
生成 JSON 后,使用它来执行对 SetupPushNotification
的调用。 响应看起来应与以下示例类似。
{
“code” : 200,
“status” : “OK”,
“data” :
{
“ARN” : “arn:*******/GCM/your_game_name”
}
}
恭喜! 您现在已配置了游戏的 iOS 消息传送通道。
注册您的 iOS 客户端以进行推送
对于 iOS,您必须依赖 iOS 处理推送通知的默认行为,因为现在 PlayFab 不在 Unity 上提供本机实现。
默认情况下,游戏在后台时收到的通知将路由到 Notification 区域。
或者,游戏为活动应用时收到的通知将以静默方式接收,并且在 Notification 区域中不可见。
- 在
client Start()
上运行该示例中的以下代码。
// must be called before trying to obtain the push token
// an asynchronous call with no callback into native iOS code that takes a moment or two before
// the token is available. (so spin and wait, or call this one early on)
// this will always return null if your app is not signed
UnityEngine.iOS.NotificationServices.RegisterForNotifications(UnityEngine.iOS.NotificationType.Alert | UnityEngine.iOS.NotificationType.Badge | UnityEngine.iOS.NotificationType.Sound, true);
- 此时,如果用户选择了通知,我们可调用 PlayFab API RegisterForIOSPushNotification。
byte[] token = UnityEngine.iOS.NotificationServices.deviceToken;
if(token != null)
{
RegisterForIOSPushNotificationRequest request = new RegisterForIOSPushNotificationRequest();
request.DeviceToken = System.BitConverter.ToString(token).Replace("-", "").ToLower();
PlayFabClientAPI.RegisterForIOSPushNotification(request, (RegisterForIOSPushNotificationResult result) =>
{
Debug.Log("Push Registration Successful");
}, OnPlayFabError);
}
else
{
Debug.Log("Push Token was null!");
}
- 如果未出现错误,那么恭喜! 您的 iOS 客户端已成功链接到游戏的 Apple 通知通道。
对 iOS 进行故障诊断
- 确认您具有有效 .pem 文件。
- 确保 XCode 使用
SetupPushNotification
中所用的相同证书为您的应用签名。 - 确认为 XCode 中的版本启用了推送通知 API。
- 确认您的签名证书与 PlayFab 平台相匹配。 运行 SetupPushNotification 时,请使用
OverwriteOldARN = true
将通道重新绑定到新平台。 在给定时间,只有一个 iOS 环境(APNS 或 APNS_SANDBOX)可在游戏中处于活动状态。
附加支持
如果需要帮助、要了解示例错误和相关问题,请在我们的论坛中提出。
注意
当前,我们仅支持针对本文档中所述的标准流程的服务。 如果您的团队需要其他常见推送服务或插件的更多功能,请告诉我们! 我们欢迎开发者社区的反馈。