public function createMessages(Entity $entity, $action) {
    // Get all messages that are responsible for creating items.
    $message_types = $this->getMessageTypes($action, $entity);
    // Loop through those message types and create messages.
    foreach ($message_types as $message_type => $message_values) {
      // Create the ones applicable for this bundle.
      // Determine destinations.
      $destinations = [];
      if (!empty($message_values['destinations']) && is_array($message_values['destinations'])) {
        foreach ($message_values['destinations'] as $destination) {
          $destinations[] = ['value' => $destination];
        }
      }

      $mt_context = $message_values['context'];

      // Set the values.
      $new_message['template'] = $message_type;

      // The flagging entity does not implement getCreatedTime().
      if ($entity->getEntityTypeId() === 'flagging') {
        $new_message['created'] = $entity->get('created')->value;
      }
      else {
        $new_message['created'] = $entity->getCreatedTime();
      }

      // Get the owner or default to anonymous.
      if ($entity instanceof EntityOwnerInterface && $entity->getOwner() !== NULL) {
        $new_message['uid'] = $entity->getOwner()->id();
      }
      else {
        $new_message['uid'] = 0;
      }

      $additional_fields = [
        ['name' => 'field_message_context', 'type' => 'list_string'],
        ['name' => 'field_message_destination', 'type' => 'list_string'],
        [
          'name' => 'field_message_related_object',
          'type' => 'dynamic_entity_reference',
        ],
      ];
      $this->createFieldInstances($message_type, $additional_fields);

      $new_message['field_message_context'] = $mt_context;
      $new_message['field_message_destination'] = $destinations;

      $new_message['field_message_related_object'] = [
        'target_type' => $entity->getEntityTypeId(),
        'target_id' => $entity->id(),
      ];

      // Create the message only if it doesn't exist.
      if (!$this->checkIfMessageExist($new_message['template'], $new_message['field_message_context'], $new_message['field_message_destination'], $new_message['field_message_related_object'], $new_message['uid'])) {
        $message = Message::create($new_message);
        $message->save();
      }

    }
  }