Push Notifications SDK
Register for Push Notifications and configure the SDK for your platform.
阅读时间7 分钟最后更新于 4 天前
使用 Push Notifications SDK 可以向用户发送推送通知广告系列,包括带有图像的丰富推送通知。
平台支持
此 SDK 支持 iOS 和 Android,但必须是 iOS 10+ 和 Android SDK >= 26 (Oreo)。快速入门
此 SDK 附带一个用于注册 Push Notifications 的示例脚本。请将这个脚本添加到您的项目中,并在编辑器的 Project Settings(项目设置)下进行相关设置。注册 Push Notifications
注册 Push Notifications 需要三个步骤。- 填写编辑器中的设置。这些设置可以在 Project Settings(项目设置)> Services(服务)> Push Notifications 中找到。
- 初始化 Unity Services,以便可以将所需的 Analytics 事件发送到 Unity Analytics。为了正确发送所需的事件,您还需要实现隐私流程。
- 在游戏的启动代码中注册通知,以确保不会错过任何通知。但是,在首次注册时,会向用户显示权限请求,因此请确保在游戏中方便的位置进行此调用。SDK 将处理通知内容(包括图像、标题和消息正文)的显示。
代码示例:
using Unity.Services.Analytics;using Unity.Services.Core;using Unity.Services.PushNotifications;...await UnityServices.InitializeAsync();bool userGaveConsent = ...;if (userGaveConsent){ AnalyticsService.Instance.StartDataCollection();}try{ string pushToken = await PushNotificationsService.Instance.RegisterForPushNotificationsAsync(); PushNotificationsService.Instance.OnNotificationReceived += notificationData => { Debug.Log("Received a notification!"); };}catch (Exception e){ Debug.Log("Failed to retrieve a push notification token.");}
收到通知回调
您可以注册一个委托,以便在收到通知时接收 C# 事件回调,进而在此时执行自定义行为。为此,请向PushNotifications.OnNotificationReceivedPush Notifications 设置
此 SDK 需要进行多种设置才能正常运行。某些设置仅在特定平台上使用(在设置名称中会进行指示)。您可以进行以下 Android (Firebase) 设置: 您可以在 Unity 编辑器 > Edit(编辑)> Project Settings(项目设置)> Services(服务)> Push Notifications 中找到这些设置。以下所有字段的值都可以在 Firebase 后台的 Settings(设置)页面中找到。- FirebaseWebApiKey:用于 Android Firebase Cloud Messaging API 的 Firebase 项目“Web API Key(Web API 密钥)”。如果您的项目未显示此字段,您可能需要先启用 Firebase 身份验证。
- FirebaseProjectNumber:用于 Android Firebase Cloud Messaging 的“Project Number(项目编号)”。
- FirebaseAppID:用于 Android Firebase Cloud Messaging API 的 Firebase 应用程序“App ID”。
- FirebaseProjectID:用于 Android Firebase Cloud Messaging API 的 Firebase 项目“Project ID”。

- 选择已禁用的 Cloud Messaging API (Legacy)(Cloud Messaging API(旧版))旁边的三点图标。
- 选择 ENABLE Cloud Messaging API (Legacy)(启用 Cloud Messaging API(旧版))。
- 刷新 Firebase 控制台。

Analytics
此 SDK 会记录两个 Analytics 事件:- notificationServices: 每当在客户端上注册新令牌时,都会记录此事件。此事件中包含推送令牌,用于向后端服务注册这个令牌,从 Unity Dashboard(Unity 后台)发送通知,并且是该产品正常运行的必要条件。
- notificationOpened: 每当用户打开通知时,都会记录此事件。此事件中包含有关用户所在广告系列和群组的数据,以及是否从通知中启动了应用。
仅 Analytics 模式
如果需要,可以将此 SDK 与现有推送通知服务集成。为此,请不要调用上述注册方法,而是将
PushNotificationsService.Instance.AnalyticsRecordPushTokenUpdatedPushNotificationsService.Instance.Analytics.RecordPushTokenUpdated(token);RecordNotificationOpenedPushNotificationsService.Instance.Analytics.RecordNotificationOpened(notificationData);测试 SDK 集成
您可以在应用中按照以下步骤测试 Push Notifications SDK 集成。- 在应用中,获取 PushNotificationsService 令牌,并使用 `Debug.Log` 将此令牌写入日志。
- 根据您的目标平台,按照适用于 Android 的调试指南或适用于 iOS 的构建和运行指南来运行您的应用。
-
读取应用日志以获取记录的令牌。
- 对于 Android:使用 Android Logcat 包。
- 对于 iOS:运行应用时检查 XCODE 中的日志。
- 在 UGS Dashboard(UGS 后台)中,转到现有的推送通知广告系列或创建新的虚拟推送通知广告系列。
- 在“Content(内容)”步骤中,创建通知(如有必要)并转到“Test on Device(在设备上测试)”。

- 选择目标设备并输入获取的令牌。

- 选择 Send(发送),然后查看设备是否收到通知。
External Dependency Manager for Unity (EDM4U) 支持
其他 SDK 或 Unity 包(包括 Google 的 Firebase Unity 包)使用 External Dependency Manager for Unity (EDM4U) 或 Mobile Dependency Resolver (MDR) 来解析其在 Unity 项目中的依赖项。
Push Notifications SDK 不需要使用 EDM4U 或 MDR 来解析其在 Unity 项目中的依赖项,默认情况下也不会重新分发或使用 EDM4U 或 MDR。
但是,从版本
3.0.1-pre.1Assets/Push Notifications/Editor/AndroidResolveForce ResolveDisplay Libraries
请注意,如果其他包也需要同样的依赖项,则注释可能会有所不同。如果是这种情况,EDM4U 将在相邻的注释中列出从中提取依赖项的所有文件。只要依赖文件路径 (implementation 'com.google.firebase:firebase-messaging-ktx:22.0.0' // Assets/Push Notifications/Editor/Android/PushSDKDependencies.xml:9
Assets/Push Notifications/Editor/Android/PushSDKDependencies.xml:9Assets/Push Notifications/Editor/AndroidForce Resolve