gpt4 book ai didi

iphone - 推送通知中的设备 token

转载 作者:可可西里 更新时间:2023-11-01 05:10:21 24 4
gpt4 key购买 nike

我只想向某些用户发送推送通知。

根据我在 Apple 文档中所经历的内容。注册推送通知的代码是这样的

- (void)applicationDidFinishLaunching:(UIApplication *)app {
// other setup tasks here....
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
}

// Delegation methods
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
const void *devTokenBytes = [devToken bytes];
self.registered = YES;
[self sendProviderDeviceToken:devTokenBytes]; // custom method
}

- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
NSLog(@"Error in registration. Error: %@", err);
}

在方法 appdidRegisterForRemoteNotif 中..我只看到创建并发送到服务器的 devToken 字节..但是我将如何识别哪个设备 token 属于哪个用户。因此,如果我的设备名称是 Shubhank 的 iPhone。我如何发送我的 iPhone 是这个并且这是我的设备 token 的信息。

最佳答案

通常您不会在委托(delegate)方法本身中更新服务器上的 apns token 。您保存它并在识别用户后更新它。

你可以这样做:

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {

const unsigned *tokenBytes = [deviceToken bytes];
NSString *hexToken = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",
ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
[[MyModel sharedModel] setApnsToken:hexToken];

}

这样您就可以将 apns token 保存在模型对象 (MyModel) 中。稍后当您确定您的用户时(通过登录/注册或任何方法)

你可以调用这个方法

[self sendProvidedDeviceToken: [[MyModel sharedModel] apnsToken] forUserWithId: userId];  //Custom method

这样您就可以将设备 token 与用户关联起来。希望这对您有所帮助!

关于iphone - 推送通知中的设备 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9141757/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com