0

I am calling device token in my first viewcontroller. And I cant get result because Device token is null. Here below is my code in 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];
}

When I am calling in Viewcontroller :

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

token is null.

Fluffeh
  • 33,228
  • 16
  • 67
  • 80
santa
  • 147
  • 1
  • 4
  • 16

9 Answers9

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

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

This worked perfectly for me in my device.

Anu Padhye
  • 615
  • 1
  • 6
  • 16
1

Refer to Kulss' answer in this SO Answer:

How can I convert my device token (NSData) into an NSString?

You should be parsing the bytes not the description.

Community
  • 1
  • 1
HCdev
  • 550
  • 1
  • 6
  • 20
1

First

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

Add this in Application.h

Nazik
  • 8,696
  • 27
  • 77
  • 123
imjaydeep
  • 878
  • 1
  • 11
  • 34
0

try this one my friend....

    - (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
  }

Happy Coding!!!!

NiravPatel
  • 3,260
  • 2
  • 21
  • 31
0

Add two methods didRegister and didFailToRegister, and confirm are u getting a call in didRegister or in 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);
}

and assure are u getting succesfully ur device token. Or u r failed to registerForRemote...

Satish Azad
  • 2,302
  • 1
  • 16
  • 35
  • I have added this method as well. And Im getting successfully. But the problem is that my viewcontroller is loading before than getting device token . For this ,device token is null? Is it more clear now? – santa Sep 13 '13 at 12:16
  • why not u load ur viewcontroller after registering remote notification. – Satish Azad Sep 13 '13 at 14:47
0

You can try this one

- (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];
}
Hemant Chittora
  • 3,152
  • 3
  • 19
  • 25
0

May be help you.

First register notification in then you will get toke in 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];
}
0

You should check your provisioning profile. Also the App should be configured for push notification on developer portal. Perform these steps for doing so.

  1. Enable push notification for your Application Identifier on Developer portal.
  2. regenerate the provisioning profile.
  3. Download and install new provisioning profile.
  4. Build and run. It will work.
Hemant Chittora
  • 3,152
  • 3
  • 19
  • 25
0

try this one..

after your transactions completed, you want token number in the view controller. so for that try to make or change root view controller again in appDelegate by the method like this in 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];

}

in this method

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

this statement is helped to call didRegisterForRemoteNotificationsWithDeviceToken again and you can store the token in some object like NSUserDefaults and you can use it in your viewController. It works for me. you can try this. Best luck.

Max
  • 2,269
  • 4
  • 24
  • 49