菜鸟教程小白 发表于 2022-12-13 14:00:52

从未调用过 iOS 设备 token 回调


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

<pre><code>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {   

    //register for push notifications
    [ registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
</code></pre>

<p>然后</p>

<pre><code>-(void)application:(UIApplication *)app didRegisterForRemoteNotificationsWith:DeviceToken:(NSData *)deviceToken
{
    NSString *str = ;
    NSLog(@&#34;%@&#34;, str);
}

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
    NSLog(@&#34;Failed to get token, error: %@&#34;, error);
}
</code></pre>

<p>这是交易。</p>

<p>我的 iOS 模拟器:我明白了</p>

<pre><code> Failed to get token, error: Error Domain=NSCocoaErrorDomain Code=3010 &#34;remote notifications are not supported in the simulator&#34; UserInfo=0x5813d20 {NSLocalizedDescription=remote notifications are not supported in the simulator}
</code></pre>

<p>正如预期的那样。</p>

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

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

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

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

<p><strong>EDIT2:</strong> 在 didRegister 回调中似乎有一个额外的冒号,就在 <code>DeviceToken</code> ...</p> 之前</p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>来自 <a href="http://developer.apple.com/library/ios/technotes/tn2265/_index.html#//apple_ref/doc/uid/DTS40010376-CH1-TNTAG21" rel="noreferrer noopener nofollow">Technical Note TN2265 Troubleshooting Push Notifications</a> (强调我的):</p>

<blockquote>
<p>No Delegate Callbacks</p>

<p>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. <strong>If neither delegate callback <code>application:didRegisterForRemoteNotificationsWithDeviceToken:</code> nor <code>application:didFailToRegisterForRemoteNotificationsWithError:</code> is called, that means that this connection has not yet been established.</strong></p>

<p><strong>This is not necessarily an error condition.</strong> 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.</p>

<p>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.</p>
</blockquote></p>
                                   
                                                <p style="font-size: 20px;">关于从未调用过 iOS 设备 token 回调,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/10873877/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/10873877/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: 从未调用过 iOS 设备 token 回调