iOS

Messaging Module Integration (CocoaPods)

CocoaPods is the easiest way to add devtodev into your iOS project.

1. Firstly, install CocoaPods using:

sudo gem install cocoapods

2. In the project directory execute the command:

pod init

3. In the created Podfile add the dependency:

platform :ios, '9.0'

target 'TargetName' do
  use_frameworks!
  pod 'DTDAnalytics', '~> 2.0.0'
  pod 'DTDMessaging', '~> 2.0.0'
end

4. Finally, run the command in your Xcode project directory:

pod install

CocoaPods should download and install the devtodev library, and create a new Xcode workspace. Open this workspace in Xcode.

Messaging Module Integration (Manual Installation)

To connect the module for processing push notifications, you need to:

1. Download the latest devtodev SDK from the repository

2. Add DTDAnalytics.xcframework to the project (check Do Not Embed)

The DTDMessaging plugin will not work without the DTDAnalytics main analytics plugin.

3.Add DTDMessaging.xcframework to the project (check Do Not Embed)

4. In the Xcode project settings, open the tab, and add: "Push Notifications" and "Background Modes" respectively

5. In the "Background Modes" section, enable "Remote notifications"

6. Add initialization to didFinishLaunchingWithOptions method:

let config = DTDAnalyticsConfiguration()
config.logLevel = .error
DTDAnalytics.initialize(applicationKey: "App ID", configuration: config)

DTDMessaging.delegate = self
DTDMessaging.pushNotificationsOptions = [.DTDNotificationOptionAlert,
                                         .DTDNotificationOptionSound,
                                         .DTDNotificationOptionBadge]
DTDMessaging.startPushService()

7. To handle SDK delegate methods, you need to add the implementation of the DTDMessagingDelegate protocol

extension AppDelegate: DTDMessagingDelegate {
    func didRegisterForRemoteNotifications(with deviceToken: Data) {
        // your code
    }

    func didFailToRegisterForRemoteNotifications(with error: Error) {
        // your code
    }

    func didReceiveInvisibleNotification(with message: DTDMessage) {
        // your code
    }

    func didReceiveForegroundNotification(with message: DTDMessage) {
        // your code
    }

    func didOpenRemoteNotification(with message: DTDMessage, and buttonClicked: DTDActionButton?) {
        // your code
    }
}

The DTDMessaging module provides support for notifications with attachments. These notifications are available since iOS 10. Attachments support images, animated gifs and videos. To use this function, you will need to create a “Notification Service Extension“, for this create a new target in your application settings:

  • Open Xcode (File -> New -> Target). Select Notification Service Extension.

The next step is to modify the Notification Extension class as follows:

  • Delete all auto-generated code.

  • Inherit Notification Extension class from DTDMediaAttachmentExtension

Example:

import UserNotifications
import DTDMessaging

class NotificationService: DTDMediaAttachmentExtension {

}

External interface of the DTDMessaging module

Optional DTDMessagingDelegate methods

Class for receiving Notification data (DTDMessage)

Class for handling pressed buttons on a notification (DTDActionButton)

Notes

Foreground Notification

Displaying push notifications in Foreground state is available since iOS 10.0.

By default, according to Apple Push Notification Guidelines, the display of push notification in the Foreground state is disabled. In order to set the display method, you must:

  • Assign delegate for UNUserNotificationCenter

  • Or pass a parameter in the push notification constructor (_fg = 1). In this case, the display method will be formed from the Notification settings.

  • Or delegate the system method userNotificationCenter willPresent notification. In this case, the display properties will be taken from the developer.

An example of a delegated method implementation:

func userNotificationCenter(_ center: UNUserNotificationCenter, 
                            willPresent notification: UNNotification, 
                            withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
  completionHandler([.alert, .sound])
}

DTDNotificationOptions

It is an OptionSet that is used to pass push notification authorization and set up interactions with users.

The user can change the allowed parameters at any time in the notification settings.

Possible values:

DTDNotificationOptionProvisional - Provisional push notifications appear in the user's Notification Center, but not on the lock screen. This type of push notification does not have to be explicitly allowed by the user. Start submitting them as soon as the user installs and runs your application. However, the user can also opt in/opt out of notifications, but they will need to do it explicitly.

This setting is used to prevent receiving push notification permission requests at the start, which is intrusive and most users refuse to receive them.

It is also important that when using the DTDNotificationOptionProvisional setting, the user will be able to subscribe to explicit notifications only from the notification center settings.

DTDMediaAttachmentExtension

To handle the application attitude to displaying push notifications, you need to add a Notifications service Extension. And inherit NotificationService from DTDMediaAttachmentExtension

@available(iOSApplicationExtension 10.0, *)
class NotificationService: DTDMediaAttachmentExtension {
  // nothing
}

DTDMessaging integration with swizzling disabled

DTDMessaging automatically swizzles notification tracking and APNS-token usage methods by default. If you want to disable the function, add the flag DTDMessagingSwizzlingEnabled (boolean) in the app’s Info.plist file and set it to NO (0). However, if you disable notification swizzling, you will need additional integration for accurate analytics:

For sending APNS-token to DTDMessaging:

func application(_ application: UIApplication, 
                didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  DTDMessaging.apnsToken = deviceToken
}i

For sending information about the application:

func application(_ application: UIApplication,
                didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
  DTDMessaging.didReceiveMessage(userInfo: userInfo, actionIdentifier: nil)
}

@available(iOS 10.0, *)
extension AppDelegate: UNUserNotificationCenterDelegate {
  func userNotificationCenter(_ center: UNUserNotificationCenter, 
                              didReceive response: UNNotificationResponse, 
                              withCompletionHandler completionHandler: @escaping () -> Void) {
    let userInfo = response.notification.request.content.userInfo
    let actionIdentifier = response.actionIdentifier
    DTDMessaging.didReceiveMessage(userInfo: userInfo, actionIdentifier: actionIdentifier)
  }

  func userNotificationCenter(_ center: UNUserNotificationCenter, 
                              willPresent notification: UNNotification, 
                              withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    let userInfo = notification.request.content.userInfo
    DTDMessaging.willPresentMessage(userInfo: userInfo)
  }
}

Note: If you disable automatic notification swizzling, DTDMessagingDelegate methods will not be called.

Last updated

#989: best practices - improved readability

Change request updated