Configure reporting for deleted iOS notifications

更新时间:
复制 MD 格式

Overview

In iOS 10 and later, the APIs for notifications are unified in the UserNotifications.framework. This framework lets you manage and use all notifications. This topic describes how to use custom actions with the software development kit (SDK) to report data when a user deletes a notification.

Related classes

  • UNNotificationAction (a specific action)

  • UNNotificationCategory (a collection of actions)

  • UNUserNotificationCenter (the notification center)

Code implementation

  1. Create an action

    /**
     *  Create and register a notification category (iOS 10+).
     */
    - (void)createCustomNotificationCategory {
        // Define custom `action1` and `action2`.
        UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:UNNotificationDefaultActionIdentifier title:@"Normal_EnterForeground" options: UNNotificationActionOptionForeground];
        UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:UNNotificationDismissActionIdentifier title:@"Delete" options: UNNotificationActionOptionDestructive];
        // Create a category with the ID `test_category` and register the two actions with the category.
        // UNNotificationCategoryOptionCustomDismissAction indicates that the notification's dismiss callback can be triggered.
        UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"test_category" actions:@[action1, action2] intentIdentifiers:@[] options:
                                            UNNotificationCategoryOptionCustomDismissAction];
        // Register the category with the notification center.
        [self.userNotificationCenter setNotificationCategories:[NSSet setWithObjects:category, nil]];
    }
  2. Handle action events in the notification delegate

    - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
        NSString *userAction = response.actionIdentifier;
        if ([userAction isEqualToString:UNNotificationDefaultActionIdentifier]) {
            // Handle the iOS 10 notification and report the notification open receipt.
            [self handleiOS10Notification:response.notification];
        }
        // Notification dismiss. This is triggered only if UNNotificationCategoryOptionCustomDismissAction was passed when the category was created.
        if ([userAction isEqualToString:UNNotificationDismissActionIdentifier]) {
            // Handle reporting for user-deleted notifications.
        // The Alibaba Cloud Push SDK supports reporting deleted data.
        [CloudPushSDK sendDeleteNotificationAck:response.notification.request.content.userInfo]
        }
        completionHandler();
    }

Payload

Key: category

Value: The category ID registered by the client. In this example, the registered ID is 'test_category'.

{
  "aps":{
    "alert":"Custom Action",
    "sound":"default",
    "badge":1,
    "category":"test_category"
  }
}

Notes

  • Use the UNNotificationCategoryOptionCustomDismissAction type when you register a UNNotificationCategory.

  • To listen only for delete events, register a UNNotificationCategory with zero actions.

  • You cannot listen for the 'Clear All' action in the notification center.