OGeek|极客世界-中国程序员成长平台

标题: 从未调用过 iOS 设备 token 回调 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 14:00
标题: 从未调用过 iOS 设备 token 回调

尝试在我的应用程序中提取设备 token 时,我感到非常难过。到目前为止,我有

- (BOOL)applicationUIApplication *)application didFinishLaunchingWithOptionsNSDictionary *)launchOptions {    

    //register for push notifications
    [[UIApplication sharedApplication] registerForRemoteNotificationTypesUIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

然后

-(void)applicationUIApplication *)app didRegisterForRemoteNotificationsWitheviceTokenNSData *)deviceToken 
{
    NSString *str = [deviceToken description]; 
    NSLog(@"%@", str); 
}

- (void)applicationUIApplication*)application didFailToRegisterForRemoteNotificationsWithErrorNSError*)error
{
    NSLog(@"Failed to get token, error: %@", error);
}

这是交易。

我的 iOS 模拟器:我明白了

 Failed to get token, error: Error Domain=NSCocoaErrorDomain Code=3010 "remote notifications are not supported in the simulator" UserInfo=0x5813d20 {NSLocalizedDescription=remote notifications are not supported in the simulator}

正如预期的那样。

在运行 iOS 4.2 的第二代 iPod Touch 上,从未调用过任何回调。我的同事运行 iOS 5.1.1 的 iPhone 4s 上也会发生同样的情况

最后,在运行 iOS.5.1.1 的另一位同事 iPhone 4 上,它似乎返回设备 token 是“com.apple.mobileconfig”......这很奇怪。

我已验证我的设备上的应用已启用通知。我还验证了我的应用标识符包在 xCode 中的设置是否正确。

编辑:我不知道发生了什么,但是,在发布这个帖子几天后,我将 iPhone 4 插入我的 Mac 并且设备 token 回调刚刚开始在 Debug模式下工作,并且然后也在其他人的设备上。我对配置文件进行了一些更改(必须添加新设备的 UDID 等),这样可能会出现一些故障。不太确定。

EDIT2: 在 didRegister 回调中似乎有一个额外的冒号,就在 DeviceToken ...

之前



Best Answer-推荐答案


来自 Technical Note TN2265 Troubleshooting Push Notifications (强调我的):

No Delegate Callbacks

When the first push-capable app is installed, iOS or Mac OS X attempts to establish a persistent network connection to the push service that will be shared by all push-capable apps on the system. If neither delegate callback application:didRegisterForRemoteNotificationsWithDeviceToken: nor application:didFailToRegisterForRemoteNotificationsWithError: is called, that means that this connection has not yet been established.

This is not necessarily an error condition. The system may not have Internet connectivity at all because it is out of range of any cell towers or Wi-Fi access points, or it may be in airplane mode. Instead of treating this as an error, your app should continue normally, disabling only that functionality that relies on push notifications.

Keep in mind that network availability can change frequently. Once the persistent connection to the push service succeeds, one of the previously-mentioned application delegate methods will be called.

关于从未调用过 iOS 设备 token 回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10873877/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://ogeek.cn/) Powered by Discuz! X3.4