首页 > 解决方案 > 我想在应用程序的非活动模式下接收通知时在主应用程序图标上增加徽章

问题描述

我正在开发 react-native iOS 中的聊天应用程序。当收到通知、应用程序被杀死或用户强制退出时,我想在主应用程序图标上做一个徽章增量。当应用程序处于后台模式时,它运行良好。但是当应用处于非活动状态时,不会调用didReceiveRemoteNotification方法。对此有任何想法吗?我在 AppDelegate 的didReceiveRemoteNotification方法中添加了代码。

在 AppDelegate 文件中添加了以下代码集

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
      [RNNotifications didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
    }

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
      [RNNotifications didFailToRegisterForRemoteNotificationsWithError:error];
    }

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    NSLog(@"APPDELEGATE: didReceiveRemoteNotification:fetchCompletionHandler %@", userInfo);
  int badge = (int) application.applicationIconBadgeNumber;
  if ( application.applicationState == UIApplicationStateInactive ) {
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:badge+1];
  }
  else if(application.applicationState == UIApplicationStateBackground) {
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:badge+1];
  }
  else if(application.applicationState == UIApplicationStateActive) {
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:badge+1];
  }
  completionHandler(UIBackgroundFetchResultNewData);
}

标签: iosobjective-creact-nativepush-notificationapple-push-notifications

解决方案


为了在应用程序处于后台或被终止时捕获传入的通知,UNNotificationServiceExtension请在您的项目中使用 a。对于(不是主应用程序的Info.plist正常情况;主应用程序有正常的东西)可能看起来像 UNNotificationServiceExtensionInfo.plist在此处输入图像描述

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandlerUNNotificationServiceExtension,您可以通过以下方式更新徽章:

  1. self.bestAttemptContent = [request.content mutableCopy];获取请求的内容
  2. self.bestAttemptContent.badge = <desired_integer_value>,其中<desired_integer_value>将是您希望为徽章计数添加的整数。
  3. self.contentHandler(self.bestAttemptContent);完成内容的更新。

在许多情况下,徽章计数可能需要反映特定用户的值(如未读聊天消息的数量)。为此,您可以使用共享用户默认值。事实上 NSUserDefaults 支持app suite允许这种共享的概念。有关详细信息,请参阅Apple 文档。尤其是,

您可以在开发应用程序套件时使用此方法,在应用程序之间共享首选项或其他数据,或者在开发应用程序扩展程序时,在扩展程序及其包含的应用程序之间共享首选项或其他数据。

参数和注册域在所有 NSUserDefaults 实例之间共享。

在一个Constants.h文件中,有一些东西可以跟踪每个用户的个人计数,例如

#define NOTIFICATIONS_UNREAD_SHARED  [NSString stringWithFormat:@"notificationsUnread-%@",[mySharedDefaults objectForKey:USERNAME]]

在您的应用程序中,您可以将每个用户的个人计数保存到应用程序套件的共享用户默认值中,例如

NSUserDefaults *mySharedDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.yourCompany.yourAppname"];
if ([[NSUserDefaults standardUserDefaults] objectForKey:USERNAME]) {
    mySharedDefaults setObject:[[NSUserDefaults standardUserDefaults] objectForKey:USERNAME] forKey:USERNAME];
    [mySharedDefaults setObject:[NSNumber numberWithInteger:arrExistingRead.count] forKey:NOTIFICATIONS_READ_SHARED];
    [mySharedDefaults setObject:[NSNumber numberWithInteger:([AppConstant sharedconstant].countObj.arrAllMessages.count - arrExistingRead.count)] forKey:NOTIFICATIONS_UNREAD_SHARED];
    [mySharedDefaults synchronize];
}

然后在你的UNNotificationServiceExtension,你会做类似的事情

NSUserDefaults *mySharedDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.yourCompany.yourAppname"];
if ([mySharedDefaults objectForKey:NOTIFICATIONS_UNREAD_SHARED]) {
    if ([mySharedDefaults objectForKey:USERNAME]) {
        self.bestAttemptContent.badge = [NSNumber numberWithInt:[[mySharedDefaults objectForKey:NOTIFICATIONS_UNREAD_SHARED] intValue]+1];
    } else { // username somehow not set; reset badge to 0
        self.bestAttemptContent.badge = @0;
    }
} else { // notifications unread count not found for this user; reset badge to 0
    self.bestAttemptContent.badge = @0;
}

故障排除

如果扩展似乎没有收到推送通知,需要验证一些事情:

查看构建目标。除了主应用程序外,扩展程序也应该有一个。 在此处输入图像描述

在主应用程序的设置中,它应该与UNNotificationServiceExtension在此处输入图像描述


推荐阅读