【问题标题】:Push Notification Device Token?推送通知设备令牌?
【发布时间】:2010-11-23 08:13:39
【问题描述】:

如何从我的 iPhone 设备获取设备令牌?

【问题讨论】:

    标签: iphone objective-c ios token apple-push-notifications


    【解决方案1】:

    此方法将在调试模式下在控制台中打印 deviceToken,如果您想查看设备令牌,您也可以在 UIAlert 中看到。

    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
        NSLog(@"APN device token: %@", deviceToken);
        NSString *deviceTokenString = [NSString stringWithFormat:@"%@",deviceToken];
        UIAlertView *deviceTokenAlert = [[UIAlertView alloc] initWithTitle:@"Device Token"
                                                                message:deviceTokenString
                                                               delegate:self
                                                      cancelButtonTitle:@"OK"
                                                      otherButtonTitles:nil];
    
    }
    

    【讨论】:

    • 感谢您的回答!你知道这个设备令牌是否会随着时间的推移而变化,对于 iPhone,还是不变的?
    • 我一直观察到设备令牌是不变的,但我认为它也可能会改变。
    • 检查这个问题的答案stackoverflow.com/questions/6927011/…,它说只有当你恢复备份时,设备令牌才会改变。
    • 请注意,只有在您先调用 [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)]; 时才会调用它。
    【解决方案2】:

    如果你实现了这个方法

    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    
    }
    

    对于推送通知,您将获得设备令牌(此方法实际上是您需要在应用程序中实现的两种方法之一)

    这可能会很有用http://urbanairship.com/docs/push.html

    你也可以看看Push Notification in Iphone application

    我希望你觉得这很有用。

    【讨论】:

      【解决方案3】:

      此方法将在控制台中显示您的设备令牌。

      - (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { 
      
          NSString *str = [NSString 
                           stringWithFormat:@"%@",deviceToken];
          NSString *newString = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
          newString = [newString stringByReplacingOccurrencesOfString:@"<" withString:@""];
          newString = [newString stringByReplacingOccurrencesOfString:@">" withString:@""];
      
      
          [[NSUserDefaults standardUserDefaults] setObject:newString forKey:@"deviceToken"];
      
      
      
          NSLog(@"Your deviceToken ---> %@",newString);
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-12-30
        • 1970-01-01
        • 2012-02-26
        • 1970-01-01
        • 1970-01-01
        • 2012-06-30
        相关资源
        最近更新 更多