我已经创建了 2 个示例(单一 View )项目来测试推送通知,除了如下通知设置代码外,我没有添加任何代码:
项目 1(Swift 2):
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
UIApplication.sharedApplication().registerForRemoteNotifications()
return true
}
func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
print("didRegisterUserNotificationSettings got called")
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let trimmedDeviceToken = deviceToken.description .stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "<>"))
.stringByReplacingOccurrencesOfString(" ", withString: "")
print("Device Token \(trimmedDeviceToken)")
}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
print("Failed to get token, error: \(error)")
}
项目 2( Objective-C ):
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIUserNotificationSettings* notificationSettings =
[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert|UIUserNotificationTypeBadge) categories:nil];
[application registerUserNotificationSettings:notificationSettings];
[application registerForRemoteNotifications];
return YES;
}
-(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
NSLog(@"didRegisterUserNotificationSettings got called");
}
-(void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSString* newToken = [deviceToken description];
newToken = [newToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
newToken = [newToken stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"Device Token %@", newToken);
}
-(void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
NSLog(@"Failed to get token, error: %@", error);
}
objective-c 项目在所有测试设备上运行良好:为远程通知注册自身并在委托(delegate)回调方法中接收设备 token ,但 swift 项目无法获得任何委托(delegate)回调s.
在将此问题标记为重复之前阅读我尝试过的内容:
1) AppID启用了推送通知
2) Push certificate type是production,Provisioning profile是AdHoc production
3) 两个项目使用相同的 AppID、推送证书和配置文件
4) 这两个项目都在 3 种不同的 iPhone(5、6 和 6Plus)上进行了测试,它们都运行 iOS 9.2,每次安装时都会删除应用程序并重新启动它们多次
5) 在两台不同的 MAC Pro 机器上用 Xcode 7.2 构建了两个项目
6) 所有 3 台 iPhone 都使用没有防火墙的互联网连接,并且所有端口都打开(如 this technical note 中所述),还将 3 台设备上的互联网连接更改为 3G 连接而不是 WIFI。
7) 在项目设置中选中/取消选中推送通知 -> 两个项目的功能(顺便说一句,这一点在我测试时没有效果)
8) 为两个项目尝试了 Xcode 运行(顶部按钮)和导出 ipa 包
9) 使用方法 registerForRemoteNotifications
10) 为两个项目尝试了另一个全新的 AppID、推送证书和配置文件。
11) 删除这两个项目,并创建另外两个新项目,代码与上面相同
经过所有这些尝试后,objective-c 项目工作正常并通过 didRegisterForRemoteNotificationsWithDeviceToken 方法接收设备 token ,但 swift 方法不起作用。
我使用 Apple 的 PersistentConnectionLogging.mobileconfig
配置文件和 uploaded them here 从 iPhone 6Plus 设备生成了 3 个 APN 日志文件,(在 apsd_2016_02_24_11_36_29+0300.log
文件中,objective-c 项目 BundleID 是 XXXXX.push.notification
swift 项目 BundleID 是 XXXXX.apnsswift
)
最佳答案
事实证明,这个奇怪问题背后的原因是 swift 的 print
方法,与 NSLog
相比,swift 应用程序没有在设备日志中显示输出没问题,它只需要使用 NSLog
来显示推送通知委托(delegate)回调方法中的消息。
//print("Device Token \(trimmedDeviceToken)")
NSLog("Device Token: %@",trimmedDeviceToken)
关于objective-c - swift 2 : no delegate callbacks for push notifications as opposed to objective-c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35554540/