Пример #1
0
func (s *sender) Send(request connector.Request) (interface{}, error) {
	deviceToken := request.Subscriber().Route().Get(deviceTokenKey)
	fcmMessage := fcmMessage(request.Message())
	fcmMessage.To = deviceToken
	logger.WithFields(log.Fields{"deviceToken": fcmMessage.To}).Debug("sending message")
	return s.gcmSender.Send(fcmMessage)
}
Пример #2
0
func (s sender) Send(request connector.Request) (interface{}, error) {
	deviceToken := request.Subscriber().Route().Get(deviceIDKey)
	logger.WithField("deviceToken", deviceToken).Debug("Trying to push a message to APNS")
	return s.client.Push(&apns2.Notification{
		Priority:    apns2.PriorityHigh,
		Topic:       s.appTopic,
		DeviceToken: deviceToken,
		Payload:     request.Message().Body,
	})
}
Пример #3
0
func (a *apns) HandleResponse(request connector.Request, responseIface interface{}, metadata *connector.Metadata, errSend error) error {
	logger.Debug("Handle APNS response")
	if errSend != nil {
		logger.WithError(errSend).Error("error when trying to send APNS notification")
		mTotalSendErrors.Add(1)
		if *a.IntervalMetrics && metadata != nil {
			addToLatenciesAndCountsMaps(currentTotalErrorsLatenciesKey, currentTotalErrorsKey, metadata.Latency)
		}
		return errSend
	}
	r, ok := responseIface.(*apns2.Response)
	if !ok {
		mTotalResponseErrors.Add(1)
		return fmt.Errorf("Response could not be converted to an APNS Response")
	}
	messageID := request.Message().ID
	subscriber := request.Subscriber()
	subscriber.SetLastID(messageID)
	if err := a.Manager().Update(subscriber); err != nil {
		logger.WithField("error", err.Error()).Error("Manager could not update subscription")
		mTotalResponseInternalErrors.Add(1)
		return err
	}
	if r.Sent() {
		logger.WithField("id", r.ApnsID).Debug("APNS notification was successfully sent")
		mTotalSentMessages.Add(1)
		if *a.IntervalMetrics && metadata != nil {
			addToLatenciesAndCountsMaps(currentTotalMessagesLatenciesKey, currentTotalMessagesKey, metadata.Latency)
		}
		return nil
	}
	logger.Error("APNS notification was not sent")
	logger.WithField("id", r.ApnsID).WithField("reason", r.Reason).Debug("APNS notification was not sent - details")
	switch r.Reason {
	case
		apns2.ReasonMissingDeviceToken,
		apns2.ReasonBadDeviceToken,
		apns2.ReasonDeviceTokenNotForTopic,
		apns2.ReasonUnregistered:

		logger.WithField("id", r.ApnsID).Info("trying to remove subscriber because a relevant error was received from APNS")
		mTotalResponseRegistrationErrors.Add(1)
		err := a.Manager().Remove(subscriber)
		if err != nil {
			logger.WithField("id", r.ApnsID).Error("could not remove subscriber")
		}
	default:
		logger.Error("handling other APNS errors")
		mTotalResponseOtherErrors.Add(1)
	}
	return nil
}
Пример #4
0
func (f *fcm) HandleResponse(request connector.Request, responseIface interface{}, metadata *connector.Metadata, err error) error {
	if err != nil && !isValidResponseError(err) {
		logger.WithField("error", err.Error()).Error("Error sending message to FCM")
		mTotalSendErrors.Add(1)
		if *f.IntervalMetrics && metadata != nil {
			addToLatenciesAndCountsMaps(currentTotalErrorsLatenciesKey, currentTotalErrorsKey, metadata.Latency)
		}
		return err
	}
	message := request.Message()
	subscriber := request.Subscriber()

	response, ok := responseIface.(*gcm.Response)
	if !ok {
		mTotalResponseErrors.Add(1)
		return fmt.Errorf("Invalid FCM Response")
	}

	logger.WithField("messageID", message.ID).Debug("Delivered message to FCM")
	subscriber.SetLastID(message.ID)
	if err := f.Manager().Update(request.Subscriber()); err != nil {
		logger.WithField("error", err.Error()).Error("Manager could not update subscription")
		mTotalResponseInternalErrors.Add(1)
		return err
	}
	if response.Ok() {
		mTotalSentMessages.Add(1)
		if *f.IntervalMetrics && metadata != nil {
			addToLatenciesAndCountsMaps(currentTotalMessagesLatenciesKey, currentTotalMessagesKey, metadata.Latency)
		}
		return nil
	}

	logger.WithField("success", response.Success).Debug("Handling FCM Error")

	switch errText := response.Error.Error(); errText {
	case "NotRegistered":
		logger.Debug("Removing not registered FCM subscription")
		f.Manager().Remove(subscriber)
		mTotalResponseNotRegisteredErrors.Add(1)
		return response.Error
	case "InvalidRegistration":
		logger.WithField("jsonError", errText).Error("InvalidRegistration of FCM subscription")
	default:
		logger.WithField("jsonError", errText).Error("Unexpected error while sending to FCM")
	}

	if response.CanonicalIDs != 0 {
		mTotalReplacedCanonicalErrors.Add(1)
		// we only send to one receiver, so we know that we can replace the old id with the first registration id (=canonical id)
		return f.replaceCanonical(request.Subscriber(), response.Results[0].RegistrationID)
	}
	mTotalResponseOtherErrors.Add(1)
	return nil
}