簡體   English   中英

推送通知中的設備令牌

[英]Device token in push notification

我想只向某些用戶發送推送通知。

從我在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字節並發送到服務器appdidRegisterForRemoteNotif但是我如何識別哪個設備令牌屬於哪個用戶。 所以,如果我的設備名稱是Shubhank的iPhone。 如何發送我的iPhone是這樣的信息,這是我的設備令牌。

通常,您不會在委托方法本身更新服務器上的apns標記。 保存它並在以后識別用戶時更新它。

你可以這樣做:

- (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];

}

通過這個,你在模型對象(MyModel)中保存了apns標記。 稍后當您確定用戶時(通過登錄/注冊或任何方法)

你可以調用這個方法

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

這樣您就可以將設備令牌與用戶鏈接起來。 希望這可以幫助!

在自定義方法中注冊時,需要發送設備名稱。 代碼應如下所示。 如果應用程序使用某種類型的用戶名,您可以發送適合您的上下文的任何信息,例如用戶名。 您可以決定向服務器發送哪些信息以在令牌和設備之間建立連接。

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

您可以將自己需要的任何信息發送到自己的推送服務中。

但重要的一點是:推送令牌不是設備令牌(UDID)。 推送令牌對於請求它們的每個應用程序都是唯一的,並且可以並且確實可以更改。 如果您想獲得除此之外的設備名稱,您可以調用[[UIDevice currentDevice] name] ,並將其發布到用於存儲推送令牌的任何內容中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM