卸载跟踪
通过将苹果推送通知服务(APNs)与 Singular SDK 集成,跟踪应用程序的卸载情况,以衡量用户保留率并优化重新吸引用户的活动。
重要:卸载跟踪需要在应用程序中配置 APNs。有关完整的实施细节,请参阅Apple 的 UserNotifications Framework 文档。
前提条件
配置 Singular 平台
在应用程序中实施卸载跟踪之前,请按照 "设置 iOS 卸载跟踪 "指南在 Singular 平台中配置您的应用程序。
系统要求
卸载跟踪需要 Apple 推送通知服务和特定的设备配置。
APN 要求(源):
- iOS 版本:设备必须运行 iOS 10.0 或更高版本的 UserNotifications 框架
- APNs 证书:在苹果开发者门户配置 APNs 证书或令牌
- 推送功能:在 Xcode 项目设置中启用推送通知功能
- 用户权限:请求并获得推送通知的用户授权
注意:拒绝推送通知权限或使用不支持 APNs 的设备的用户将不会被跟踪卸载。
实施步骤
步骤 1:启用推送通知
配置您的 Xcode 项目以支持推送通知(如果尚未启用)。
- 在 Xcode 中打开项目
- 选择您的应用程序目标,然后转到 "签名和功能
- 单击 "+ 能力"并添加 "推送通知
- 确保在后台模式中勾选远程通知
第 2 步:请求用户授权
使用 UserNotifications 框架向用户申请接收推送通知的权限。
import UIKit
import UserNotifications
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Set notification delegate
UNUserNotificationCenter.current().delegate = self
// Request authorization for notifications
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(options: authOptions) { granted, error in
if let error = error {
print("Notification authorization error: \(error.localizedDescription)")
}
if granted {
// Register for remote notifications on main thread
DispatchQueue.main.async {
application.registerForRemoteNotifications()
}
}
}
return true
}
}
#import <UIKit/UIKit.h>
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Set notification delegate
[UNUserNotificationCenter currentNotificationCenter].delegate = self;
// Request authorization for notifications
UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert |
UNAuthorizationOptionBadge |
UNAuthorizationOptionSound;
[[UNUserNotificationCenter currentNotificationCenter]
requestAuthorizationWithOptions:authOptions
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (error) {
NSLog(@"Notification authorization error: %@", error.localizedDescription);
}
if (granted) {
// Register for remote notifications on main thread
dispatch_async(dispatch_get_main_queue(), ^{
[application registerForRemoteNotifications];
});
}
}];
return YES;
}
@end
第 3 步:注册 APNs 设备令牌
读取 APNs 设备令牌并将其发送至 Singular,以便卸载跟踪。
读取和设置令牌
实现didRegisterForRemoteNotificationsWithDeviceToken 回调,捕获 APNs 令牌并将其注册到 Singular。
import Singular
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// Register the device token with Singular for uninstall tracking
Singular.registerDeviceToken(forUninstall: deviceToken)
print("APNs device token registered with Singular")
// Also send to your server if needed for your own push notifications
sendTokenToServer(deviceToken)
}
func application(_ application: UIApplication,
didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Failed to register for remote notifications: \(error.localizedDescription)")
}
private func sendTokenToServer(_ deviceToken: Data) {
// Implement your server communication logic here
}
#import <Singular/Singular.h>
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// Register the device token with Singular for uninstall tracking
[Singular registerDeviceTokenForUninstall:deviceToken];
NSLog(@"APNs device token registered with Singular");
// Also send to your server if needed for your own push notifications
[self sendTokenToServer:deviceToken];
}
- (void)application:(UIApplication *)application
didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"Failed to register for remote notifications: %@", error.localizedDescription);
}
- (void)sendTokenToServer:(NSData *)deviceToken {
// Implement your server communication logic here
}
最佳做法:收到令牌后立即调用registerDeviceTokenForUninstall ,以确保卸载跟踪从第一个应用程序会话开始启用。
令牌数据格式
将 APNs 设备令牌以从 Apple 收到的本地二进制格式传递给 Singular。
重要:APNs 令牌是二进制数据(NSData/Data),无需转换即可直接传给 Singular。如果您的应用程序需要将令牌转换为十六进制字符串用于其他目的,请为 Singular 保留原始 NSData 对象。
十六进制格式示例:b0adf7c9730763f88e1a048e28c68a9f806ed032fb522debff5bfba010a9b052
其他配置方法
在初始化过程中设置令牌
如果您在 SDK 初始化之前就有 APNs 令牌,请在SingularConfig 对象中进行配置。
// Get cached token if available
if let cachedToken = getCachedAPNsToken() {
let config = SingularConfig(apiKey: "SDK_KEY", andSecret: "SDK_SECRET")
config?.apnsDeviceToken = cachedToken
Singular.start(config)
}
func getCachedAPNsToken() -> Data? {
// Implement your token caching logic here
return nil
}
// Get cached token if available
NSData *cachedToken = [self getCachedAPNsToken];
if (cachedToken) {
SingularConfig *config = [[SingularConfig alloc]
initWithApiKey:@"SDK_KEY"
andSecret:@"SDK_SECRET"];
config.apnsDeviceToken = cachedToken;
[Singular start:config];
}
- (NSData *)getCachedAPNsToken {
// Implement your token caching logic here
return nil;
}
方法签名:
@property (nonatomic, strong) NSData *apnsDeviceToken;
验证和故障排除
验证实施
确认卸载跟踪工作正常。
- 检查日志:验证 APN 令牌注册是否出现在日志中
- 测试令牌生成:确保首次启动应用程序时生成令牌
- 监控仪表板:检查 Singular 仪表板,查看 24-48 小时后的卸载跟踪数据
- 测试权限:验证通知授权提示是否出现并正常运行
常见问题
- 未生成令牌:验证是否在 Xcode 中启用了推送通知功能,是否在 Apple Developer 门户中配置了 APN 证书
- 用户拒绝权限:实施逻辑以从容处理拒绝的通知权限
- 数据缺失:确保设备运行 iOS 10.0 以上版本,且用户已授予通知权限
- 配置错误:确认已在 Singular 平台设置中启用卸载跟踪功能
- 模拟器限制:APN 标记在 iOS 模拟器中不可用;请在物理设备上进行测试
其他资源:有关详细的故障排除,请参阅《卸载跟踪设置指南》和Apple 的 UserNotifications 文档。