我已经集成了Apple推送通知,并在我的应用程序中遇到了一个奇怪的问题。当我通过USB连接直接通过Xcode安装应用程序时,设备令牌正在生成正确存储在数据库中,并且推送通知正常工作。但是当我创建IPA并通过创建的ipa在同一设备中安装应用程序时,设备令牌生成错误并且推送通知无效。以下是我的代码:
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
[[UIApplication sharedApplication] registerUserNotificationSettings: [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
} else {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}
application.applicationIconBadgeNumber = 0;
#ifdef __IPHONE_8_0
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings
*)notificationSettings {
[application registerForRemoteNotifications];
}
#endif
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData
*)deviceToken {
const unsigned *tokenData = deviceToken.bytes;
NSString *deviceTokenString = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x", ntohl(tokenData[0]),ntohl(tokenData[1]),ntohl(tokenData[2]),ntohl(tokenData[3]),ntohl(tokenData[4]),ntohl(tokenData[5]),ntohl(tokenData[6]),ntohl(tokenData[7])];
[[NSUserDefaults standardUserDefaults]setObject:deviceTokenString forKey:@"devicetoken"];
NSLog(@"Device Token = %@", deviceTokenString);
}
//Failed to Register for Remote Notifications
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"Error in registration. Error: %@", error);
}
答案 0 :(得分:2)
设备令牌取决于您为应用程序签名的证书。如果您直接安装它是开发证书,而在签署AdHoc时它是分发证书。对于推送,您需要在服务器上打包相应的分发或开发证书。