Notifications (ApplozicSwift)

This section describes the process that needs to be followed to receive push notifications for your chat within your app.

📘

Note: This is only applicable if you're using ApplozicSwift SDK

Setting up APNs Certificates

Applozic sends the payload to Apple servers which then sends the push notification to your user's device.

Creating APNs certificates

For Apple to send these notifications, you would have to create an APNs certificate in your Apple developer account.
Visit

  1. Visit this link, select Apple Push Notification service SSL (Sandbox & Production), and create.

A sample is shown below shows what your certificate should look like once you create it

The above created Apple Push Notification service will work for both Production and Development

📘

Note: Supported format for APNS certificate is .p12.

Export a .p12 file from keychains and upload it to Applozic Console

The p12 files are required for authenticating Apple servers for sending APNs push notifications to devices will guide how to export p12 file from Keychains for uploading to the Applozic console.

Follow these steps for export Apple push service certificate p12 file:

A. Open Keychain Access on your Mac and go to login > Select My Certificates.

B. Find the Apple push services certificate and which you added, right-click on the certificate, and then select the Export **** option from the menu to get a .p12 file and type a name for the file and save it with the password.

Upload APNs Certificates

Upload your push notification certificate (mentioned above-exported p12 file) to Applozic console by referring to the below-given image, in order to enable real-time notification.

Go to Applozic Console ->Click on Settings -> Chat--> Notifications -> Click Add Certificate
Upload APNS .p12 Certificate of Apple push services (Sandbox & Production) in Production and Development same p12 file in both.

Updating Capabilities

Post setting up APNs, the next step is to enable push notifications within your project. To do that
Click on your project ----> Click on Capabilities and enable

  1. Push Notifications
  2. Background Modes - Fetch and Remote notification

Following screenshots would be of help.

📘

Have Question Or Suggestion?

You can drop us your query at [email protected]

Setup Push Notifications

Push notification setup is rather straightforward, all you need to do is to find the AppDelegate file in your project and follow all the instructions below.

Add the below push notification code in your AppDelegate.swift or AppDelegate.m file

If the below method already exists, copy the code inside the method and paste it into your AppDelegate file.

🚧

Make sure your AppDelegate conforms to UNUserNotificationCenterDelegate and make sure that each of the methods or code inside the method is added in your Appdelegate file

Note: Make sure that ALChatManager.swift is added from here

Sample code is as below:

import ApplozicSwift
import UIKit
 
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
    var window: UIWindow?
 
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
 
        // Push notification register.
        UNUserNotificationCenter.current().delegate = self
        ALChatManager.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
        return true
    }
 
 
    func applicationWillEnterForeground(_ application: UIApplication) {
        // Reset the app badge count
        ALChatManager.shared.applicationWillEnterForeground(application)
    }
 
    func applicationWillTerminate(_ application: UIApplication) {
        // Save context in app will Terminate
        ALChatManager.shared.applicationWillTerminate(application: application)
    }
 
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        // Register device token to applozic server.
        ALChatManager.shared.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
    }
 
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
 
        // Pass the notification to the applozic method for processing.
        ALChatManager.shared.application(application, didReceiveRemoteNotification: userInfo) { result in
            // Process your own notification here.
            completionHandler(result)
        }
    }
 
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
 
        // Pass the notification to the applozic method for processing.
        ALChatManager.shared.userNotificationCenter(center, willPresent: notification) { options in
            // Process your own notification here.
            completionHandler(options)
        }
    }
 
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
 
        // Pass the response to the applozic method for processing notification.
        ALChatManager.shared.userNotificationCenter(center, didReceive: response) {
            // Process your own notification here.
            completionHandler()
        }
    }
}
#import "AppDelegate.h"
@import ApplozicCore;
#import "YOUR_TARGET_NAME_HERE-Swift.h" // Important: Replace 'YOUR_TARGET_NAME_HERE' with your target name.
#import <UserNotifications/UserNotifications.h>
 
/// Add this UNUserNotificationCenterDelegate in your Appdelegate file
@interface AppDelegate () <UNUserNotificationCenterDelegate>
 
@end
 
@implementation AppDelegate
 
 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 
    // Push notification register.
    UNUserNotificationCenter.currentNotificationCenter.delegate = self;
    [ALChatManager.shared application:application didFinishLaunchingWithOptions:launchOptions];
    return YES;
}
 
 
-(void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler {
 
    [ALChatManager.shared application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:^(UIBackgroundFetchResult backgroundFetchResult) {
        // Perform your other operations here
        completionHandler(backgroundFetchResult);
    }];
}
 
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification*)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
 
    [ALChatManager.shared userNotificationCenter:center willPresent:notification withCompletionHandler:^(UNNotificationPresentationOptions options) {
        // Process your own notification here.
        completionHandler(options);
    }];
}
 
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(nonnull UNNotificationResponse* )response
         withCompletionHandler:(nonnull void (^)(void))completionHandler {
 
    [ALChatManager.shared userNotificationCenter:center didReceive:response withCompletionHandler:^{
        // Process your own notification here.
        completionHandler();
    }];
}
 
- (void)applicationWillEnterForeground:(UIApplication *)application {
    [ALChatManager.shared applicationWillEnterForeground:application];
}
 
- (void)applicationWillTerminate:(UIApplication *)application {
    [ALChatManager.shared applicationWillTerminateWithApplication:application];
}
 
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    [ALChatManager.shared application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
 
@end

📘

Note: Above didFinishLaunchingWithOptions apploizc method code is required in all the cases if your using any other push notification setup like firebase or one signal or any other it needs to be added in your Appdelegate file.

Disable Notification

In order to disable the incoming push notification alert or only notification sound, you need to update notification mode. Below are the possible values of notification mode.

Notification Mode

Explanation

0

Enable notification with sound (Default)

1

Enable notification without sound

2

Disable notification (No Alert)

ALRegisterUserClientService.updateNotificationMode(modeValue, withCompletion: {
    response, error in
    if(error != nil) {
        ALUserDefaultsHandler.setNotificationMode(modeValue)
    } else {

    }
})

📘

Have Question Or Suggestion?

You can drop us your query at [email protected]


What’s Next
Did this page help you?