菜鸟教程小白 发表于 2022-12-11 19:19:48

iOS10 - 延迟 10 秒调用 userNotificationCenter didReceiveNotificationResponse


                                            <p><p>我刚刚升级了注册 iOS 10 的整个 iOS 推送通知,使用以下代码:</p>

<pre><code>-(void)registerForNotifications{
if(SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(@&#34;10.0&#34;)){
    UNUserNotificationCenter *center = ;
    center.delegate = self;
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
      dispatch_async(dispatch_get_main_queue(), ^(void){
            if(!error){
                [ registerForRemoteNotifications];
            }
      });
    }];
}
else {
    if ([ respondsToSelector:@selector(registerUserNotificationSettings:)])
    {
      UIUserNotificationType types = (UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound);
      UIUserNotificationSettings *settings = ;
      [ registerUserNotificationSettings:settings];
      [ registerForRemoteNotifications];
    }
}
</code></pre>

<p>}</p>

<p>我的所有委托(delegate)都设置在我的 AppDelegate 中。</p>

<p><strong>编辑</strong>:我现在能够进一步确定问题所在。当应用在通知推送后返回前台时,委托(delegate):</p>

<pre><code>- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
</code></pre>

<p>}</p>

<p> 仅在大约 10-15 秒后调用,而通常它显然是立即调用的。这怎么可能?</p>

<p>我现在正在使用 Localytics 测试推送通知并实现:</p>

<pre><code>- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
</code></pre>

<p>在我的 AppDelegate 中用于深度链接。当我添加断点时,我看到发生了这种情况:
我正确收到了推送通知,点击它,应用程序 UI 卡住了大约 10 秒。最后,调用 didReceiveNotificationResponse 并进行深度链接。</p>

<p>如何避免导致应用卡住的巨大延迟?</p>

<p>编辑:它甚至比我更糟糕。当我将 iPhone 连接到 xCode 并在手机上运行构建时,它会在工作前卡住十秒钟。然后,如果我只是运行完全相同的构建而不在 xCode 上运行它(所以没有断点),应用程序会卡住 10 秒然后崩溃。</p>

<p><strong>编辑</strong>:这是我的主线程在 xCode 卡住时暂停时的屏幕截图:
<a href="/image/WkQ40.png" rel="noreferrer noopener nofollow"><img src="/image/WkQ40.png" alt="enter image description here"/></a> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您的堆栈跟踪中有一些奇怪的东西。 <strong>semaphore_wait_trap</strong>、<strong>semaphore_wait_slow</strong>。试试看<a href="https://stackoverflow.com/questions/13473448/ios-semaphore-wait-trap-on-main-thread-causing-hang-in-ui" rel="noreferrer noopener nofollow">here</a>和 <a href="https://stackoverflow.com/questions/33885416/semaphore-wait-trap-blocking-the-ui" rel="noreferrer noopener nofollow">here</a>和 <a href="https://stackoverflow.com/questions/27096023/semaphore-wait-trap-when-accessing-class-singleton-using-swift" rel="noreferrer noopener nofollow">here</a> .话虽这么说,我的<strong>猜测</strong>是你从错误的线程调用你的 <code>(void)registerForNotifications</code> 并且它导致了这个问题。</p>

<p>另外,我不明白为什么您的实现中有 <code>dispatch_async(dispatch_get_main_queue</code>。它<em>似乎</em>没有必要。尝试查看 <a href="https://stackoverflow.com/questions/37956482/registering-for-push-notifications-in-xcode-8-swift-3-0/37956783#comment71862534_37956783" rel="noreferrer noopener nofollow">here</a> 中的答案</p></p>
                                   
                                                <p style="font-size: 20px;">关于iOS10 - 延迟 10 秒调用 userNotificationCenter didReceiveNotificationResponse,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/42951136/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/42951136/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iOS10 - 延迟 10 秒调用 userNotificationCenter didReceiveNotificationResponse