Implementing a Notification Service Extension for an iOS App Using Objective C and Xcode 8.3: A Step-by-Step Guide

Notification Service Extension is not working

=====================================================

In this article, we’ll explore how to implement a Notification Service Extension for an iOS app using Objective C and xcode 8.3. We’ll also delve into the issues that can arise when integrating push notifications with rich notifications.

Introduction


Push notifications are a convenient way to notify users of events or updates in their apps. However, the standard way to display a notification is limited by Apple’s guidelines. To overcome this limitation, developers use Notification Service Extensions (NSExtentsions) to customize and extend the notification experience.

In this article, we’ll focus on implementing a Notification Service Extension for an iOS app using Objective C and xcode 8.3. We’ll cover the necessary steps, common pitfalls, and solutions to help you get started with Notification Service Extensions.

Requirements


Before we begin, make sure you have the following requirements met:

  • Xcode 8.3 or later
  • Objective C or Swift
  • Firebase for push notifications
  • iOS 11 or later (for rich notifications)

Setting Up the Notification Service Extension


To implement a Notification Service Extension, follow these steps:

Step 1: Create a new target

In xcode, create a new target under your app’s target.

Step 2: Choose Notification Service Extension as the target type

Select “Notification Service Extension” as the target type.

Step 3: Add the necessary frameworks

Add the following frameworks to your project:

  • UserNotifications
  • Photos
  • AVFoundation (for rich notifications)

Creating the Notification Service Extension


To create the Notification Service Extension, follow these steps:

Step 1: Create a category identifier

Create a unique category identifier for your app. This will be used to register the notification categories.

// Create a category identifier
NSString * const GENERAL_CATEGORY_IDENTIFIER = @"GENERAL";

Step 2: Register the notification category

Register the notification category using UNNotificationCategory.

#import <UserNotifications/UserNotifications.h>

@import Photos;
@import AVFoundation;

- (void)viewDidLoad {
    [super viewDidLoad];

    // Create a unique category identifier
    NSString * const GENERAL_CATEGORY_IDENTIFIER = @"GENERAL";

    // Register the notification categories.
    UNNotificationCategory* generalCategory = [UNNotificationCategory categoryWithIdentifier:GENERAL_CATEGORY_IDENTIFIER actions:@[] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];

    // Get the current user center
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

Step 3: Set the notification content

Set the notification content using UNMutableNotificationContent. In this example, we’ll add an image to the notification.

#import <UserNotifications/UserNotifications.h>

@import Photos;
@import AVFoundation;

- (void)viewDidLoad {
    [super viewDidLoad];

    // Create a unique category identifier
    NSString * const GENERAL_CATEGORY_IDENTIFIER = @"GENERAL";

    // Register the notification categories.
    UNNotificationCategory* generalCategory = [UNNotificationCategory categoryWithIdentifier:GENERAL_CATEGORY_IDENTIFIER actions:@[] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];

    // Get the current user center
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

    // Create a mutable notification content
    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];

Step 4: Add a rich notification

Add a rich notification to the notification content. In this example, we’ll add an image.

#import <UserNotifications/UserNotifications.h>

@import Photos;
@import AVFoundation;

- (void)viewDidLoad {
    [super viewDidLoad];

    // Create a unique category identifier
    NSString * const GENERAL_CATEGORY_IDENTIFIER = @"GENERAL";

    // Register the notification categories.
    UNNotificationCategory* generalCategory = [UNNotificationCategory categoryWithIdentifier:GENERAL_CATEGORY_IDENTIFIER actions:@[] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];

    // Get the current user center
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

    // Create a mutable notification content
    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];

Step 5: Add an image to the rich notification

Add an image to the rich notification.

#import <UserNotifications/UserNotifications.h>

@import Photos;
@import AVFoundation;

- (void)viewDidLoad {
    [super viewDidLoad];

    // Create a unique category identifier
    NSString * const GENERAL_CATEGORY_IDENTIFIER = @"GENERAL";

    // Register the notification categories.
    UNNotificationCategory* generalCategory = [UNNotificationCategory categoryWithIdentifier:GENERAL_CATEGORY_IDENTIFIER actions:@[] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];

    // Get the current user center
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

    // Create a mutable notification content
    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];

    // Add an image to the rich notification
    UIImage *image = [UIImage imageNamed:@"notification_image"];
    UNNotificationContentImageAttachment *imageAttachment = [UNNotificationContentImageAttachment.alloc] initWithURL:[NSURL URLWithString:@"https://example.com/notification_image.jpg"];

Step 6: Set the mutable content flag

Set the mutable_content flag to indicate that the notification payload contains data that can be modified by the user.

#import <UserNotifications/UserNotifications.h>

@import Photos;
@import AVFoundation;

- (void)viewDidLoad {
    [super viewDidLoad];

    // Create a unique category identifier
    NSString * const GENERAL_CATEGORY_IDENTIFIER = @"GENERAL";

    // Register the notification categories.
    UNNotificationCategory* generalCategory = [UNNotificationCategory categoryWithIdentifier:GENERAL_CATEGORY_IDENTIFIER actions:@[] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];

    // Get the current user center
    UNUserNotificationCenter *center = [UNUserCenterCurrentNotificationCenter]];

    // Create a mutable notification content
    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];

Step 7: Set the rich notification payload

Set the rich notification payload using UNMutableNotificationContent and its attachments.

#import <UserNotifications/UserNotifications.h>

@import Photos;
@import AVFoundation;

- (void)viewDidLoad {
    [super viewDidLoad];

    // Create a unique category identifier
    NSString * const GENERAL_CATEGORY_IDENTIFIER = @"GENERAL";

    // Register the notification categories.
    UNNotificationCategory* generalCategory = [UNNotificationCategory categoryWithIdentifier:GENERAL_CATEGORY_IDENTIFIER actions:@[] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction]];

    // Get the current user center
    UNUserNotificationCenter *center = [UNUserCenterCurrentNotificationCenter]];

    // Create a mutable notification content
    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];

    // Set the rich notification payload
    content.body = @"Hello World!";
    content.sound = [[UNNotificationSound defaultSound] withName:@"default"];

Integrating Push Notifications


To integrate push notifications with Firebase, follow these steps:

Step 1: Initialize Firebase

Initialize Firebase using the Firebase SDK.

#import <Firebase/Firebase.h>

// Initialize Firebase
[[FMDatabase databaseWithConfig:[FIRApp configure]] setRootURLString:@"https://your-project-id.firebaseio.com/"];

Step 2: Enable Push Notifications

Enable push notifications by registering for remote notifications.

#import <UserNotifications/UserNotifications.h>

@import Firebase;

// Register for remote notifications
UNUserNotificationCenter *center = [UNUserCenterCurrentNotificationCenter]];
 center.getNotificationSettings().addedNotificationCategories =@[GENERAL_CATEGORY_IDENTIFIER];

Step 3: Handle Remote Notifications

Handle remote notifications by implementing the UNUserNotificationCenterDelegate protocol.

#import <UserNotifications/UserNotifications.h>

@import Firebase;

@interface YourViewController : UIViewController <UNUserNotificationCenterDelegate>

@end

@implementation YourViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Register for remote notifications
    UNUserCenterCurrentNotificationCenter];
 center.getNotificationSettings().addedNotificationCategories =@[GENERAL_CATEGORY_IDENTIFIER];
}

#pragma mark - UNUserNotificationCenterDelegate

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationResponse *response))completionHandler {
    // Handle remote notification
    completionHandler(@{(UNNotificationResponse *)response});
}

Example Use Case: Sending a Rich Notification


To send a rich notification, follow these steps:

Step 1: Create the Notification Payload

Create the notification payload using UNMutableNotificationContent and its attachments.

#import <UserNotifications/UserNotifications.h>

@import Photos;
@import AVFoundation;

- (void)viewDidLoad {
    [super viewDidLoad];

    // Create a unique category identifier
    NSString * const GENERAL_CATEGORY_IDENTIFIER = @"GENERAL";

    // Register the notification categories.
    UNNotificationCategory* generalCategory = [UNNotificationCategory categoryWithIdentifier:GENERAL_CATEGORY_IDENTIFIER actions:@[] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction]];

    // Get the current user center
    UNUserNotificationCenter *center = [UNUserCenterCurrentNotificationCenter]];

    // Create a mutable notification content
    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];

    // Set the rich notification payload
    content.body = @"Hello World!";
    content.sound = [[UNNotificationSound defaultSound] withName:@"default"];
}

Step 2: Add an Image Attachment

Add an image attachment to the notification payload.

#import <UserNotifications/UserNotifications.h>

@import Photos;
@import AVFoundation;

- (void)viewDidLoad {
    [super viewDidLoad];

    // Create a unique category identifier
    NSString * const GENERAL_CATEGORY_IDENTIFIER = @"GENERAL";

    // Register the notification categories.
    UNNotificationCategory* generalCategory = [UNNotificationCategory categoryWithIdentifier:GENERAL_CATEGORY_IDENTIFIER actions:@[] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction]];

    // Get the current user center
    UNUserNotificationCenter *center = [UNUserCenterCurrentNotificationCenter]];

    // Create a mutable notification content
    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];

    // Set the rich notification payload
    content.body = @"Hello World!";
    content.sound = [[UNNotificationSound defaultSound] withName:@"default"];

    // Add an image attachment
    UIImage *image = [UIImage imageNamed:@"notification_image"];
    UNNotificationContentImageAttachment *imageAttachment = [UNNotificationContentImageAttachment alloc] initWithURL:[NSURL URLWithString:@"https://example.com/notification_image.jpg"]];
}

Step 3: Set the Mutable Content Flag

Set the mutable_content flag to indicate that the notification payload contains data that can be modified by the user.

#import <UserNotifications/UserNotifications.h>

@import Photos;
@import AVFoundation;

- (void)viewDidLoad {
    [super viewDidLoad];

    // Create a unique category identifier
    NSString * const GENERAL_CATEGORY_IDENTIFIER = @"GENERAL";

    // Register the notification categories.
    UNNotificationCategory* generalCategory = [UNNotificationCategory categoryWithIdentifier:GENERAL_CATEGORY_IDENTIFIER actions:@[] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction]];

    // Get the current user center
    UNUserNotificationCenter *center = [UNUserCenterCurrentNotificationCenter]];

    // Create a mutable notification content
    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];

    // Set the rich notification payload
    content.body = @"Hello World!";
    content.sound = [[UNNotificationSound defaultSound] withName:@"default"];

    // Add an image attachment
    UIImage *image = [UIImage imageNamed:@"notification_image"];
    UNNotificationContentImageAttachment *imageAttachment = [UNNotificationContentImageAttachment alloc] initWithURL:[NSURL URLWithString:@"https://example.com/notification_image.jpg"]];
}

Step 4: Send the Notification

Send the notification using UNMutableNotificationContent and its attachmentBody.

#import <UserNotifications/UserNotifications.h>

@import Photos;
@import AVFoundation;

- (void)viewDidLoad {
    [super viewDidLoad];

    // Create a unique category identifier
    NSString * const GENERAL_CATEGORY_IDENTIFIER = @"GENERAL";

    // Register the notification categories.
    UNNotificationCategory* generalCategory = [UNNotificationCategory categoryWithIdentifier:GENERAL_CATEGORY_IDENTIFIER actions:@[] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction]];

    // Get the current user center
    UNUserNotificationCenter *center = [UNUserCenterCurrentNotificationCenter]];

    // Create a mutable notification content
    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];

    // Set the rich notification payload
    content.body = @"Hello World!";
    content.sound = [[UNNotificationSound defaultSound] withName:@"default"];

    // Add an image attachment
    UIImage *image = [UIImage imageNamed:@"notification_image"];
    UNNotificationContentImageAttachment *imageAttachment = [UNNotificationContentImageAttachment alloc] initWithURL:[NSURL URLWithString:@"https://example.com/notification_image.jpg"]];

    // Set the mutable content flag
    content.mutableContent = YES;

    // Send the notification
    center.postNotification(content);
}

Conclusion


Notification Service Extensions are a powerful tool for customizing and extending the notification experience in iOS apps. In this article, we’ve covered the necessary steps to implement a Notification Service Extension using Objective C and xcode 8.3.

We’ve discussed how to register for remote notifications, handle remote notifications, and send rich notifications using UNMutableNotificationContent and its attachments.

By following these steps, you’ll be able to create custom notification experiences that engage your users and improve their overall app experience.

Remember to always test your Notification Service Extension thoroughly before deploying it to production.


Last modified on 2024-02-16