Skip to content

APNS

Introduction

Make sure that you already created certificate on your Apple account via previous title.

  • After opening the project in Xcode, click on the project on the left side.

  • Select Capabilities and Activate push notification

  • Add Background Modes also

Sgf Push Settings

Permission Info

Used to send deviceToken of users who give push permission.

// MARK: - APNs Token
    func application(_ application: UIApplication,
                     didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        let tokenString = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
        print("APNs device token: \(tokenString)")

        UserDefaults.standard.set(tokenString, forKey: "apnsToken")

        // Permission/registration info
        let obj = NotificationModel()
        obj.deviceToken = tokenString
        obj.type = NotificationType.PERMISSION_INFO
        obj.providerType = ProviderType.APNS
        SegmentifyManager.sharedManager().sendNotification(segmentifyObject: obj)
    }

View/Delivered

// MARK: - UNUserNotificationCenterDelegate (Foreground Delivery)
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

        let userInfo = notification.request.content.userInfo
        let instanceId = userInfo["instanceId"] as? String ?? ""

        let obj = NotificationModel()
        obj.type = NotificationType.VIEW
        obj.providerType = ProviderType.APNS
        obj.instanceId = instanceId
        SegmentifyManager.sharedManager().sendNotification(segmentifyObject: obj)

        completionHandler([.banner, .sound, .badge])
    }

Click

It will be sent when the user clicks notification.

We need to send data to the page after clicking :

// MARK: - UNUserNotificationCenterDelegate (Tap / Click)
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void) {

        let userInfo = response.notification.request.content.userInfo
        print("Notification tapped (APNs), userInfo: \(userInfo)")

        let instanceId = userInfo["instanceId"] as? String ?? ""
        let productId = userInfo["productId"] as? String ?? ""

        let obj = NotificationModel()
        obj.type = NotificationType.CLICK
        obj.providerType = ProviderType.APNS
        obj.instanceId = instanceId
        obj.productId = productId
        SegmentifyManager.sharedManager().sendNotification(segmentifyObject: obj)

        completionHandler()
    }   

instanceId and productId will be in the notification data


See our sample application : Example

Example AppDelegate : Example AppDelegate