菜鸟教程小白 发表于 2022-12-12 09:15:28

ios - 应用未运行时未收到远程通知


                                            <p><p>我的应用在未运行时不会收到推送通知。
我正在尝试处理以 JSON 形式发送的远程通知,并使用给定 JSON 中的数据更新我的应用程序中的数据。
当应用程序处于事件状态或在后台时,一切顺利。
但是当应用程序未运行时,应用程序仅在我通过点击通知打开我的应用程序时处理通知,而不是在我通过点击图标打开应用程序时处理通知。
下面是 appDelegate 类的代码:</p>

<pre><code>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [Parse setApplicationId:appId
            clientKey:clKey];

   if (application.applicationState != UIApplicationStateBackground) {

   BOOL preBackgroundPush = !;
   BOOL oldPushHandlerOnly = !;
   BOOL noPushPayload = !;
   if (preBackgroundPush || oldPushHandlerOnly || noPushPayload) {
      ;
   }
   }
   [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|
                                                UIRemoteNotificationTypeAlert|
                                                UIRemoteNotificationTypeSound|
                        UIRemoteNotificationTypeNewsstandContentAvailability];
   NSDictionary *notificationPayload = launchOptions;

   ;
   return YES;
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
TFLog(@&#34;didRegisterForRemoteNotificationsWithDeviceToken&#34;);
// Store the deviceToken in the current installation and save it to Parse.
PFInstallation *currentInstallation = ;
;
;

TFLog(@&#34;deviceToken: %@, currentInstallation.badge: %ld&#34;, currentInstallation.deviceToken, (long)currentInstallation.badge);

TFLog(@&#34;deviceToken: %@, deviceType: %@&#34;, currentInstallation.deviceToken, currentInstallation.deviceType);
TFLog(@&#34;installationId: %@&#34;, currentInstallation.installationId);
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
TFLog(@&#34;didFailToRegisterForRemoteNotificationsWithError %@&#34;, error);
if (error.code == 3010) {
    TFLog(@&#34;Push notifications are not supported in the iOS Simulator.&#34;);
} else {
    // show some alert or otherwise handle the failure to register.
    TFLog(@&#34;application:didFailToRegisterForRemoteNotificationsWithError: %@&#34;, error);
}
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
TFLog(@&#34;%@&#34;, userInfo);
;
;
;
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
TFLog(@&#34;didReceiveRemoteNotification2&#34;);
;
;
}
</code></pre>

<p>因此,应用在所有状态下都会收到远程通知,但未运行时除外。
我错过了什么?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您错过了 <a href="https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103-SW4" rel="noreferrer noopener nofollow">Local and Push Notification Programming Guide</a> 中的位。它说的地方-</p>

<blockquote>
<p>If the action button is tapped (on a device running iOS), the system
launches the application and the application calls its delegate’s
application:didFinishLaunchingWithOptions: method (if implemented); it
passes in the notification payload (for remote notifications) or the
local-notification object (for local notifications).</p>

<p>If the application icon is tapped on a device running iOS, the
application calls the same method, but furnishes no information about
the notification</p>
</blockquote>

<p>另外,来自 Apple 的这份说明 - </p>

<blockquote>
<p><strong>Important:</strong> Delivery of notifications is a “best effort”, not
guaranteed. It is not intended to deliver data to your app, only to
notify the user that there is new data available.</p>
</blockquote>

<p>如果您的应用程序是从应用程序图标而不是通知启动的,则您需要检查更新的内容,而与可能已收到的任何推送通知无关。这使得应用程序在从通知打开和从应用程序图标打开时的行为有所不同。 </p>

<p>例如,Facebook 应用程序在从通知警报启动时直接打开通知中的项目,但从应用程序图标启动时不会打开 - 从用户的角度来看,这是“正确”的行为。如果我与通知交互,那么我会对它的内容感兴趣。如果我从图标启动应用程序,那么我只想使用该应用程序 - 如果需要,我可以访问应用程序中的通知。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 应用未运行时未收到远程通知,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/22962873/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/22962873/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 应用未运行时未收到远程通知