0

我第一次调用设备令牌viewcontroller。而且我无法得到结果,因为设备令牌为空。下面是我的代码appdelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge];

    return YES;
}
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    token = [[deviceToken description] stringByTrimmingCharactersInSet:      [NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSLog(@"Device Token ---%@", token);
    [[NSUserDefaults standardUserDefaults] setObject:token forKey:@"DeviceToken"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

当我在 Viewcontroller 中调用时:

NSString *token=  [[NSUserDefaults standardUserDefaults] objectForKey:@"DeviceToken"];

令牌为空。

4

9 回答 9

4
    NSString *device = [deviceToken description];
    device = [device stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    device = [device stringByReplacingOccurrencesOfString:@" " withString:@""];

    NSLog(@"My device is: %@", device);

这在我的设备中非常适合我。

于 2014-06-27T06:15:31.933 回答
1

请参阅此 SO 答案中的 Kulss 答案:

如何将我的设备令牌 (NSData) 转换为 NSString?

您应该解析字节而不是描述。

于 2014-01-09T04:17:43.383 回答
1

第一的

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

添加这个Application.h

于 2015-09-07T02:52:08.347 回答
0

您应该检查您的配置文件。此外,应用程序应配置为在开发人员门户上推送通知。为此,请执行这些步骤。

  1. 在开发人员门户上为您的应用程序标识符启用推送通知。
  2. 重新生成配置文件。
  3. 下载并安装新的配置文件。
  4. 构建并运行。它会起作用的。
于 2013-09-18T09:47:20.387 回答
0

试试这个。。

交易完成后,您需要视图控制器中的令牌号。所以为此尝试在 appDelegate 中通过类似这样的方法再次创建或更改根视图控制器AppDelegate.m

-(void)changeRootView
{
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

    // Override point for customization after application launch.
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
    //Adding navigation controller to the main view.
    [navigationController release];

    navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
    self.window.rootViewController = navigationController;
    [self.window makeKeyAndVisible];

}

在这个方法中

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
         (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

该语句有助于didRegisterForRemoteNotificationsWithDeviceToken再次调用,您可以将令牌存储在某个对象中,例如NSUserDefaults,您可以在 viewController 中使用它。这个对我有用。你可以试试这个。好运。

于 2014-06-11T12:46:27.863 回答
0

你可以试试这个

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*) deviceToken {
    NSString *pushToken = [deviceToken description];
    pushToken = [pushToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    [[NSUserDefaults standardUserDefaults] setObject:pushToken forKey:@"device_token_data"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
于 2013-09-13T13:11:53.447 回答
0

试试这个我的朋友....

    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
              #if !TARGET_IPHONE_SIMULATOR

// Prepare the Device Token for Registration (remove spaces and < >)
token = [[[[devToken description]
                        stringByReplacingOccurrencesOfString:@"<"withString:@""]
                       stringByReplacingOccurrencesOfString:@">" withString:@""]
                      stringByReplacingOccurrencesOfString: @" " withString: @""];
NSLog(@"%@",token);
[[NSUserDefaults standardUserDefaults] setObject:token forKey:@"deviceToken"];
[[NSUserDefaults standardUserDefaults] synchronize];
        #endif
}
/**
  * Failed to Register for Remote Notifications
 */
  - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    #if !TARGET_IPHONE_SIMULATOR

   NSLog(@"Error in registration. Error: %@", error);
  #endif
  }

快乐编码!!!!

于 2013-09-13T11:55:59.243 回答
0

添加两个方法 didRegister 和 didFailToRegister,并确认你是在 didRegister 还是在 didFailedToRegister 中接到电话。

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    NSLog(@"Device Token=> %@",deviceToken);

    //Parse your device toke

}

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

并确保您成功获得您的设备令牌。或者您未能注册远程...

于 2013-09-13T12:02:05.190 回答
0

可能对你有帮助。

首先注册通知然后您将获得didRegisterRemoteNotification

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {   
    self.strdeviceToken=[[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    self.strdeviceToken = [self.strdeviceToken stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    self.strdeviceToken=[self.strdeviceToken stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
}
于 2013-09-13T13:25:55.423 回答