iPhone PushNotification多次注册

时间:2011-01-05 14:29:38

标签: iphone notifications push

我正在为我的应用使用PushNotification。它似乎每次加载应用程序时都会生成设备令牌。所以在我的服务器中我有许多重复的设备令牌。

我是否需要在将设备令牌添加到数据库之前检查设备令牌,或者我是否在应用程序中实现了一些错误?

在我正在使用的代码段下面。

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

 // launchOptions has the incoming notification if we're being launched after the user tapped "view"
 NSLog( @"didFinishLaunchingWithOptions:%@", launchOptions );

 // [self.viewController handleDidReceiveRemoteNotification:userInfo];


 // other setup tasks here.... 
    [[UIApplication sharedApplication] 
  registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | 
           UIRemoteNotificationTypeSound |
           UIRemoteNotificationTypeAlert)]; 

    // [self updateWithRemoteData];  // freshen your app!

 // RESET THE BADGE COUNT
    application.applicationIconBadgeNumber = 0; 

    // ... 
 // call the original applicationDidFinishLaunching method to handle the basic view setup tasks
 [self applicationDidFinishLaunching:application];

 return YES;
}



- (void)application:(UIApplication *)app 
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken { 
    [self sendDeviceTokenToRemote:devToken]; // send the token to your server 
}

有人可以帮忙吗?如何在我的服务器中存储唯一的设备令牌?

感谢, Nikil

2 个答案:

答案 0 :(得分:1)

  1. 在大多数情况下,分配给每个设备的此APN令牌是唯一且不变的。您可以将其视为另一种UDID。因此,一旦设备在服务器的数据库中注册,您就不必再次注册它。
  2. (这是一个棘手的部分)然而,根据Apple的文档,APN令牌可能会改变,比方说,如果设备已更新到更高版本的操作系统或其部分硬件被新的替换。但是,这种情况不会经常发生。
  3. 至于在您的应用和服务器中转换和存储此APNs令牌,请查看此帖iPhone pushNotification DeviceToken - How to "decrypt"
  4. 希望它有所帮助。

答案 1 :(得分:0)

应用程序应在每次启动时重新注册推送通知,作为Apple推荐的最佳做法。 (见Apple Local and Push Notifications Programming Guide

如何在服务器端域中存储设备ID取决于您。一个例子是具有表示设备最后注册时间的列。如果它是新设备ID,您将添加一行;如果设备ID已存在,则更新具有新时间戳的行。