推送通知中的设备令牌 [英] Device token in push notification

查看:198
本文介绍了推送通知中的设备令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to send push notification to certain users only.

从我在Apple文档中经历的内容。
注册推送通知的代码是这个

From what I have gone through in Apple docs. The code to register for push notification is this

- (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字节并发送到服务器..但是我如何识别哪个设备令牌属于哪个用户。所以,如果我的设备名称是Shubhank的iPhone。如何发送我的iPhone是这样的信息,这是我的设备令牌。

In the method appdidRegisterForRemoteNotif..I see only devToken bytes created and send to the server..but how will I identify which device token belongs to which user. So if my device name is Shubhank's iPhone. How can I send the information that my iPhone is this and this is my device token.

推荐答案

一般情况下你不更新在委托方法本身的服务器上使用apns标记。保存它并在以后识别用户时更新它。

generally you don't update the apns token on server in the delegate method itself. you save it and update it later when you have the user identified.

你可以这样做:

- (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标记(为MyModel)。稍后当您确定用户时(通过登录/注册或任何方法)

with this you saved the apns token in the Model object (MyModel). And later when you have your user identified (by login/sign up or whatever method)

您可以调用此方法

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

这样您就可以将设备令牌与用户链接起来。希望这有帮助!

This way you have linked the device token with user. Hope this helps!

这篇关于推送通知中的设备令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆