'Initializes this object from an existing notification.
Args:
notification: A rdfvalues.flows.Notification object.
is_pending: Indicates whether the user has already seen
this notification or not.
Returns:
The current instance.'

def InitFromNotification(self, notification, is_pending=False):

    self.timestamp = notification.timestamp
    self.message = notification.message
    self.subject = str(notification.subject)
    self.is_pending = is_pending
    reference_type_enum = ApiNotificationReference.Type
    components = self._GetUrnComponents(notification)
    if (notification.type == 'Discovery'):
        self.reference.type = reference_type_enum.DISCOVERY
        self.reference.discovery = ApiNotificationDiscoveryReference(client_id=components[0])
    elif (notification.type == 'ViewObject'):
        if ((len(components) >= 2) and (components[0] == 'hunts')):
            self.reference.type = reference_type_enum.HUNT
            self.reference.hunt.hunt_urn = rdfvalue.RDFURN(os.path.join(*components[:2]))
        elif ((len(components) >= 2) and (components[0] == 'cron')):
            self.reference.type = reference_type_enum.CRON
            self.reference.cron.cron_job_urn = rdfvalue.RDFURN(os.path.join(*components[:2]))
        elif ((len(components) >= 3) and (components[1] == 'flows')):
            self.reference.type = reference_type_enum.FLOW
            self.reference.flow.flow_id = components[2]
            self.reference.flow.client_id = components[0]
        elif ((len(components) == 1) and rdf_client.ClientURN.Validate(components[0])):
            self.reference.type = reference_type_enum.DISCOVERY
            self.reference.discovery.client_id = components[0]
        else:
            path = notification.subject.Path()
            for prefix in rdf_paths.PathSpec.AFF4_PREFIXES.values():
                part = ('/%s%s' % (components[0], prefix))
                if path.startswith(part):
                    self.reference.type = reference_type_enum.VFS
                    self.reference.vfs.client_id = components[0]
                    self.reference.vfs.vfs_path = (prefix + path[len(part):])
                    break
            if (self.reference.type != reference_type_enum.VFS):
                self.reference.type = reference_type_enum.UNKNOWN
                self.reference.unknown.subject_urn = notification.subject
    elif (notification.type == 'FlowStatus'):
        if ((not components) or (not rdf_client.ClientURN.Validate(components[0]))):
            self.reference.type = reference_type_enum.UNKNOWN
            self.reference.unknown.subject_urn = notification.subject
        else:
            self.reference.type = reference_type_enum.FLOW
            self.reference.flow.flow_id = notification.source.Basename()
            self.reference.flow.client_id = components[0]
    elif (notification.type == 'GrantAccess'):
        if rdf_client.ClientURN.Validate(components[1]):
            self.reference.type = reference_type_enum.CLIENT_APPROVAL
            self.reference.client_approval.client_id = components[1]
            self.reference.client_approval.approval_id = components[(-1)]
            self.reference.client_approval.username = components[(-2)]
        elif (components[1] == 'hunts'):
            self.reference.type = reference_type_enum.HUNT_APPROVAL
            self.reference.hunt_approval.hunt_id = components[2]
            self.reference.hunt_approval.approval_id = components[(-1)]
            self.reference.hunt_approval.username = components[(-2)]
        elif (components[1] == 'cron'):
            self.reference.type = reference_type_enum.CRON_JOB_APPROVAL
            self.reference.cron_job_approval.cron_job_id = components[2]
            self.reference.cron_job_approval.approval_id = components[(-1)]
            self.reference.cron_job_approval.username = components[(-2)]
    else:
        self.reference.type = reference_type_enum.UNKNOWN
        self.reference.unknown.subject_urn = notification.subject
        self.reference.unknown.source_urn = notification.source
    return self
