Skip to content

How can I be sure that message is delivered?

Davor Komušanac edited this page Dec 5, 2022 · 7 revisions

Message fetching

Because the delivery of remote notifications is not guaranteed by Apple Push Notification service (APNs) as described in the documentation, Mobile Messaging SDK has a fetching mechanism to get messages that weren't delivered by APNs. All fetched messages will be displayed using local notifications by default, this behaviour is implemented by MMDefaultMessageHandling class.

NOTE: How to implement custom message handler?

Every message that either was pushed by APNs or fetched from Mobile Messaging server is represented by MM_MTMessage object. Use MM_MTMessage.deliveryMethod attribute to determine how the message was delivered to your application:

func handle(message: MM_MTMessage) {
  switch message.deliveryMethod {
  case .push:
    //message pushed by APNs
  case .pull:
    //message fetched from Mobile Messaging server
  default:
    break
  }
}

NOTE: As a compromise between APNs delivery restrictions and our pursuit of improved delivery rate, there is a chance that a message may appear twice. This may happen if a push message was delivered by APNs while application was killed by the user, because Apple doesn't allow the application to handle remote notifications until relaunched. Once the app launched, Mobile Messaging SDK will fetch messages that were sent, but not yet handled by the SDK. In such case Mobile Messaging SDK can't tell if a fetched message was previously delivered by APNs and will try to display them.

In order to prevent fetched messages to be displayed using local notifications, you implement your custom message handler as follows:

class CustomMessageHandler: MMMessageHandlingDelegate {
  func didReceiveNewMessage(message: MM_MTMessage) {
      switch message.deliveryMethod {
      case .generatedLocally:
        if !message.isSilent {
          UIApplication.shared.presentLocalNotificationNow(localNotification(with: message))
        }
      case .pull, .push, .undefined:
        break
      }
  }
  
  func localNotification(with message: MM_MTMessage) -> UILocalNotification {
      let localNotification = UILocalNotification()
      localNotification.alertBody = message.text
      localNotification.soundName = message.sound
      return localNotification
  }
}

//then set your handler to messageHandling property:
MobileMessaging.messageHandlingDelegate = CustomMessageHandler()
Clone this wiki locally