Push Notification
Push Notification Setup
Add import statement in AppDelegate file to access the methods
import Kommunicate
a) Send device token to Kommunicate server:
In your AppDelegateâs didRegisterForRemoteNotificationsWithDeviceToken method send device token registration to Kommunicate server after you get deviceToken from APNS. Sample code is as below:
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
{
print("DEVICE_TOKEN_DATA :: \(deviceToken.description)")
var deviceTokenString: String = ""
for i in 0..count
{
deviceTokenString += String(format: "%02.2hhx", deviceToken[i] as CVarArg)
}
print("DEVICE_TOKEN_STRING :: \(deviceTokenString)")
if (KMUserDefaultHandler.getApnDeviceToken() != deviceTokenString)
{
let kmRegisterUserClientService: KMRegisterUserClientService = KMRegisterUserClientService()
kmRegisterUserClientService.updateApnDeviceToken(withCompletion: deviceTokenString, withCompletion: { (response, error) in
print ("REGISTRATION_RESPONSE :: \(String(describing: response))")
})
}
}
b) Receiving push notification:
Once your app receives notification, pass it to Kommunicate handler for chat notification processing.
Add the following code in AppDelegate class, this function will be called after the app launch to register for push notifications.
func registerForNotification() {
UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
if granted {
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
}
}
UNUserNotificationCenter.current().delegate = self
registerForNotification()
KMPushNotificationHandler.shared.dataConnectionNotificationHandlerWith(Kommunicate.defaultConfiguration, Kommunicate.kmConversationViewConfiguration)
let kmApplocalNotificationHandler: KMAppLocalNotification = KMAppLocalNotification.appLocalNotificationHandler()
kmApplocalNotificationHandler.dataConnectionNotificationHandler()
c) Handling app launch on notification click:
Add the following code anywhere inside the AppDelegate class, refer to this sample for the better understanding.
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let service = KMPushNotificationService()
let dict = notification.request.content.userInfo
guard !service.isKommunicateNotification(dict) else {
service.processPushNotification(dict, appState: UIApplication.shared.applicationState)
completionHandler([])
return
}
completionHandler([.sound, .badge, .alert])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let service = KMPushNotificationService()
let dict = response.notification.request.content.userInfo
if service.isApplozicNotification(dict) {
service.processPushNotification(dict, appState: UIApplication.shared.applicationState)
}
completionHandler()
}
d) AppDelegate changes for foreground notification:
The following functions will be called by AppDelegate when the app comes to foreground(active) mode, add this anywhere inside the AppDelegate class.
An optional step that is only required if you want to reset the app icon badge count after a user opens the app.
func applicationWillEnterForeground(_ application: UIApplication) {
print("APP_ENTER_IN_FOREGROUND")
UIApplication.shared.applicationIconBadgeNumber = 0
}
e) Save Context when app terminates:
func applicationWillTerminate(application: UIApplication) {
KMDBHandler.sharedInstance().saveContext()
}
You can check the sample AppDelegate file here.
Certificates
a) Upload development and distribution APNs certificates
Upload development and distribution APNs certificates on Kommunicate dashboard, this will allow Kommunicate to send the notification for new messages to your mobile app.
b) Updating Capabilities
Post setting up APNs, the next step is to enable âPush Notificationsâ and âBackground Modesâ within your project.
Click on your project, select it from TARGETS.
Next select âSigning & Capabilitiesâ section.
Click on â+Capabilityâ
Search and select âPush Notificationsâ
Click on â+Capabilityâ again
Search and select âBackground Modesâ
Enable âBackground Fetchâ and âRemote notificationsâ under âBackground Modesâ list
Following screenshot would be of help.
Last updated
Was this helpful?