• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-12 09:15:28 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

- (BOOL)applicationUIApplication *)application didFinishLaunchingWithOptionsNSDictionary *)launchOptions
{
    [Parse setApplicationId:appId
              clientKey:clKey];

   if (application.applicationState != UIApplicationStateBackground) {

     BOOL preBackgroundPush = ![application respondsToSelectorselector(backgroundRefreshStatus)];
     BOOL oldPushHandlerOnly = ![self respondsToSelectorselector(application:didReceiveRemoteNotification:fetchCompletionHandler];
     BOOL noPushPayload = ![launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
     if (preBackgroundPush || oldPushHandlerOnly || noPushPayload) {
        [PFAnalytics trackAppOpenedWithLaunchOptions:launchOptions];
     }
   }
   [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|
                                                UIRemoteNotificationTypeAlert|
                                                UIRemoteNotificationTypeSound|
                        UIRemoteNotificationTypeNewsstandContentAvailability];
   NSDictionary *notificationPayload = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];

   [self processPushNotification:notificationPayload foreground:YES];
   return YES;
}

- (void)applicationUIApplication *)application didRegisterForRemoteNotificationsWithDeviceTokenNSData *)deviceToken
{
  TFLog(@"didRegisterForRemoteNotificationsWithDeviceToken");
// Store the deviceToken in the current installation and save it to Parse.
  PFInstallation *currentInstallation = [PFInstallation currentInstallation];
  [currentInstallation setDeviceTokenFromData:deviceToken];
  [currentInstallation saveInBackground];

  TFLog(@"deviceToken: %@, currentInstallation.badge: %ld", currentInstallation.deviceToken, (long)currentInstallation.badge);

  TFLog(@"deviceToken: %@, deviceType: %@", currentInstallation.deviceToken, currentInstallation.deviceType);
  TFLog(@"installationId: %@", currentInstallation.installationId);
}

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

- (void)applicationUIApplication *)application didReceiveRemoteNotificationNSDictionary *)userInfo {
  TFLog(@"%@", userInfo);
  [PFPush handlePush:userInfo];
  [self processPushNotification:userInfo foreground:YES];
  [PFAnalytics trackAppOpenedWithRemoteNotificationPayload:userInfo];
}

- (void)applicationUIApplication *)application didReceiveRemoteNotificationNSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  TFLog(@"didReceiveRemoteNotification2");
  [self processPushNotification:userInfo foreground:YES];
  [PFAnalytics trackAppOpenedWithRemoteNotificationPayload:userInfo];
}

因此,应用在所有状态下都会收到远程通知,但未运行时除外。 我错过了什么?



Best Answer-推荐答案


您错过了 Local and Push Notification Programming Guide 中的位。它说的地方-

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).

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

另外,来自 Apple 的这份说明 -

Important: 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.

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

例如,Facebook 应用程序在从通知警报启动时直接打开通知中的项目,但从应用程序图标启动时不会打开 - 从用户的角度来看,这是“正确”的行为。如果我与通知交互,那么我会对它的内容感兴趣。如果我从图标启动应用程序,那么我只想使用该应用程序 - 如果需要,我可以访问应用程序中的通知。

关于ios - 应用未运行时未收到远程通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22962873/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap