我在第一个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"];
令牌为空。
答案 0 :(得分:4)
NSString *device = [deviceToken description];
device = [device stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
device = [device stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"My device is: %@", device);
这对我来说非常适合我的设备。
答案 1 :(得分:1)
答案 2 :(得分:1)
<强>第一强>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
在Application.h
答案 3 :(得分: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
}
快乐编码!!!!
答案 4 :(得分: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);
}
并确保你成功获得设备令牌。或者你没有注册ForRemote ......
答案 5 :(得分: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];
}
答案 6 :(得分: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];
}
答案 7 :(得分:0)
您应该检查您的配置文件。此外,应该在App开发人员门户上配置应用程序以进行推送通 执行这些步骤。
答案 8 :(得分:0)
尝试这个..
在您的交易完成后,您需要在视图控制器中使用令牌编号。因此,尝试通过AppDelegate.m
中的方法在appDelegate中再次制作或更改根视图控制器。
-(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中使用它。这个对我有用。你可以试试这个。祝你好运。