# 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

{% hint style="info" %}
Configuration with auth keys is recommended as they are the more current method for sending notifications to iOS.
{% endhint %}

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](https://developer.apple.com/account/) , then select **Certificates, IDs & Profiles**.

![](https://4266585843-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LfVupFSqh_qZAY9OiCO%2F-MHLfyg9-9f845ZhQihH%2F-MHM1K3FUgFsfkRCW8d1%2F42b7a4c-Screen_Shot_2019-08-19_at_11.58.10.png?alt=media\&token=b5feef10-0255-4fc8-9a83-9cfc107a35d9)

**2.** Next, select **Keys** tab.

![](https://4266585843-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LfVupFSqh_qZAY9OiCO%2F-MHLfyg9-9f845ZhQihH%2F-MHM1bGF5370xRVjzceC%2Fa38dcd6-keys.png?alt=media\&token=8b975a7d-beb8-422f-a863-6059261d82f4)

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

![](https://4266585843-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LfVupFSqh_qZAY9OiCO%2F-MHLfyg9-9f845ZhQihH%2F-MHM1qPK9uC0LyyH00oW%2Fe99649f-Screen_Shot_2019-08-21_at_11.55.01.png?alt=media\&token=3e6d7104-3c09-4c8a-a277-879d47fd4e80)

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

![](https://4266585843-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LfVupFSqh_qZAY9OiCO%2F-MHLfyg9-9f845ZhQihH%2F-MHM1xv9KSCvpPYAoSHp%2Fbae3f0a-Screen_Shot_2019-08-21_at_11.55.43.png?alt=media\&token=1c64a1f9-7993-4b2d-ad6c-d3b873215f18)

**5.** Confirm your key information and click **Register**.

![](https://4266585843-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LfVupFSqh_qZAY9OiCO%2F-MHLfyg9-9f845ZhQihH%2F-MHM2CsOly0Yt_mLGyIo%2F1fc0b84-Screen_Shot_2019-08-21_at_11.55.53.png?alt=media\&token=ad4ca038-b6d0-4985-9d23-5db45f289e96)

Then proceed to download the key file by clicking **Download**.

![](https://4266585843-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LfVupFSqh_qZAY9OiCO%2F-MHLfyg9-9f845ZhQihH%2F-MHM2JfHtGEep0gbQRPu%2Ffc005ad-Push_key.png?alt=media\&token=39c49e73-d134-43d3-85c4-ab9641cbd3f9)

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](https://taptalk.io/), and login to your dashboard.Select Tab **Development** -> **Push Certificates**\
**2.** Select Tab **Development** -> **Push Certificates**

![](https://4266585843-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LfVupFSqh_qZAY9OiCO%2F-MHLfyg9-9f845ZhQihH%2F-MHM3-pMUJKl4Hpkj6WA%2Fac55f15-Screenly_-_12.1_Development_-_Push_Certificates_-_Enabled_-_Empty_State2x.png?alt=media\&token=361497b9-b255-4f8d-bbd6-a9765981debf)

**3.** Next, select **Add APNs Authentication Key**

![](https://4266585843-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LfVupFSqh_qZAY9OiCO%2F-MHLfyg9-9f845ZhQihH%2F-MHM32wQQIhSApWNV_O_%2F7a20ffc-Screenly_-_12.2.1_Development_-_Push_Certificates_-_Add_APNs2x.png?alt=media\&token=6cdf1c22-8149-4655-830d-b806f29846e2)

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

{% hint style="info" %}
You can find your Team ID in the Apple Member Center under the [membership tab](https://developer.apple.com/account/).
{% endhint %}

![](https://4266585843-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LfVupFSqh_qZAY9OiCO%2F-MHLfyg9-9f845ZhQihH%2F-MHM37yKfZGFsMqhynxe%2F7ca993b-Screenly_-_12.2.3_Development_-_Push_Certificates_-_Add_APNs_-_Active2x.png?alt=media\&token=ff11f9a8-a7cc-4e8a-b190-f578a2375ff4)

{% hint style="info" %}
**Show Unread Count**

You can choose either to **show unread count by** **chat room** or **not show unread count**
{% endhint %}

![](https://4266585843-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LfVupFSqh_qZAY9OiCO%2F-MHLfyg9-9f845ZhQihH%2F-MHM3lHfmjGIp_ZY2oyr%2F05665be-Screenly_-_12.3_Development_-_Push_Certificates_-_Filled2x.png?alt=media\&token=cfad231b-fc44-43be-bd50-73231f934c71)

### Step 3: Implement push notification in your app

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

{% tabs %}
{% tab title="Objective-C" %}
{% code title="AppDelegate.m" %}

```csharp
@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];
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

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

{% tabs %}
{% tab title="Objective-C" %}
{% code title="AppDelegate.m" %}

```csharp
- (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) {
    }];  
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

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

{% tabs %}
{% tab title="Objective-C" %}
{% code title="AppDelegate.m" %}

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

@end

@implementation AppDelegate

#pragma mark - UNUserNotificationCenter
// Implement UNUserNotificationCenter delegate
// This method lets the app know when a notification is delivered to 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 lets 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
```

{% endcode %}
{% endtab %}
{% endtabs %}

**4.** Implement **`TapTalkDelegate`** in **appDelegate** class.

{% tabs %}
{% tab title="Objective-C" %}
{% code title="AppDelegate.m" %}

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

@end

@implementation AppDelegate

#pragma mark - Delegate
#pragma mark - TapTalkDelegate

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

{% endcode %}
{% endtab %}
{% endtabs %}

### Add In-App Notification

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

![](https://4266585843-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LfVupFSqh_qZAY9OiCO%2F-MHLfyg9-9f845ZhQihH%2F-MHM54N-E4ZY08FRMWN7%2Fff558ef-Screen_-_iPhone_X_-_Notification_-_No_Image2x.png?alt=media\&token=0b2a3dd5-ddc1-4bf1-90eb-fb50ae6d38d0)

{% tabs %}
{% tab title="Objective-C" %}
{% code title="AppDelegate.m" %}

```csharp
#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];
  
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

{% hint style="info" %}
**Note:** You will not receive any in-app notification if you don't implement this method.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.taptalk.io/powertalk-chat-sdk-documentation/powertalk-ios/push-notification.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
