Push Notification

Push notification is a very important aspect in a chat application. Taptalk.io provides a convenient way to implement push notifications so you can easily manage them according to your needs. If you implement Taptalk.io with the UI implementation method, incoming notifications will be handled within our library and will be automatically shown to the user. If you prefer having full control over the push notifications, the core implementation method can be used.

Push notification for iOS client apps are sent using Apple Push Notification service (APNs). They are used for keeping your users informed with timely and relevant content, whether your client app is running in the background or inactive on a device. They can also perform the types of user interactions such as alert, sound, or badge, and include custom data your app needs to respond to the notifications.

Our iOS SDK enables you to build a client app that can receive push notifications. When a message is sent to TapTalk.io server through the SDK, the server communicates with APNs regarding the message and then APNs delivers a push notification for the message to an iOS device where your client app is installed.

Step 1: Generating your .p8 key file

Configuration with auth keys is recommended as they are the more current method for sending notifications to iOS.

A .p8 key file is required to send push notifications to your apps. There are some several ways to generate key for push notification, but Apple recommends to use APNs Auth Key (.p8 key file) to register for push notifications.

1. To generate a .p8 key file, go to Apple developer account page , then select Certificates, IDs & Profiles.

2. Next, select Keys tab.

3. Click "+" button or click Create a key button to add new key.

4. In the new key page, type in your key name and check the Apple Push Notification service (APNs) box, then click Continue.

5. Confirm your key information and click Register.

Then proceed to download the key file by clicking Download.

Please remember the Key ID provided in the information above or you can see the Key ID from the downloaded file. The Auth Key filename will look like this : AuthKey_ABC12DE34.p8, the ABC12DE34 is the Key ID for this key, we will need this Key ID later.

Step 2: Using .p8 key file in TapTalk.io dashboard to send push notification

1. Go to TapTalk.io Dashboard page, and login to your dashboard.Select Tab Development -> Push Certificates 2. Select Tab Development -> Push Certificates

3. Next, select Add APNs Authentication Key

4. Upload .p8 Authentication Key, add APN Key ID, add Team ID, and select to show unread count option

You can find your Team ID in the Apple Member Center under the membership tab.

Show Unread Count

You can choose either to show unread count by chat room or not show unread count

Step 3: Implement push notification in your app

1. The first step is to implement UIApplicationDelegate & TapTalkApplicationDelegate in your AppDelegate class.

AppDelegate.m
//AppDelegate.m

@interface AppDelegate () <TapTalkDelegate, UNUserNotificationCenterDelegate>

@end

 //Implement didRegisterForRemoteNotificationsWithDeviceToken: method to tells the delegate that the app successfully registered with Apple Push Notification service (APNs).
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  
  //Call TapTalk didRegisterForRemoteNotificationsWithDeviceToken: method
    [[TapTalk sharedInstance] application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

//Implement didReceiveRemoteNotification: method to tells the app that a remote notification arrived that indicates there is data to be fetched.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
  
   //Call TapTalk didReceiveRemoteNotification: method
    [[TapTalk sharedInstance] application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}

2. Request authorization to Apple Push Notification Service (APNs) by using this method in your appDelegate class with application:didFinishLaunchingWithOptions: method.

AppDelegate.m
//AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  
  //Register for notification in didFinishLaunchingWithOptions: method  
  [UNUserNotificationCenter currentNotificationCenter].delegate = self;
  UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge
    
  [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error) {
  }];  
}

3. Implement UNUserNotificationCenterDelegate to handle notification in your appDelegate class.

AppDelegate.m
//AppDelegate.m

//Add UNUserNotificationCenterDelegate
@interface AppDelegate () <UNUserNotificationCenterDelegate>

@end

@implementation AppDelegate
#pragma mark UNUserNotificationCenter
//Implement UNUserNotificationCenter delegate
//This method let the app knows when a notification is delivered to a foreground.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
  
  //Add TapTalk.io method to handle willPresentNotification method
    [[TapTalk sharedInstance] userNotificationCenter:center willPresentNotification:notification withCompletionHandler:completionHandler];
}

//This method let your app know which action was selected by the user for a given notification.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler {
  
  //Add TapTalk.io method to handle didReceiveNotificationResponse method
  [[TapTalk sharedInstance] userNotificationCenter:center didReceiveNotificationResponse:response withCompletionHandler:completionHandler];
}
  
@end

4. Implement TapTalkDelegate in appDelegate class.

AppDelegate.m
//AppDelegate.m

//Add TapTalkDelegate and previous UNUserNotificationCenterDelegate
@interface AppDelegate () <UNUserNotificationCenterDelegate, TapTalkDelegate>

@end

@implementation AppDelegate
#pragma mark - Delegate
#pragma mark TapTalk

//Implement TapTalk Delegate and add code below
- (void)tapTalkDidRequestRemoteNotification {
  //Register to receive remote notifications via Apple Push Notification service
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}
  
@end

Add In-App Notification

TapTalk.io provides in-app notification handling. This following code enables you to activate and show in-app notification.

AppDelegate.m
//  AppDelegate.m

//import TapUI
#import <TapTalk/TapUI.h>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 
    //To activate in-app push notification with your current window
    [[TapUI sharedInstance] activateTapTalkInAppNotification:YES];
  
}

Note: You will not receive any in-app notification if you don't implement this method.

Last updated