Подтвердить что ты не робот

Как реализовать интерактивное уведомление iOS8

Я разрабатываю приложение iOS8, которое поддерживает интерактивное уведомление. Но у меня нет ясности в функциях, поддерживаемых интерактивным уведомлением, и о том, как отправлять/обрабатывать интерактивные уведомления. Если кто-нибудь может привести пример, это будет очень полезно для меня.

Спасибо в Advance:)

4b9b3361

Ответ 1

  • Сначала вам нужно создать уведомление.
  • Во-вторых, вам нужно создать категорию уведомлений и установить его действия. Вы можете установить для двух контекстов. UIUserNotificationActionContextDefault или UIUserNotificationActionContextMinimal
  • В-третьих, вам необходимо создать настройку уведомлений и назначить вышеуказанные категории.
  • Четвертый шаг - создать локальное уведомление и присвоить ему идентификатор категории.

UIMutableUserNotificationAction *notificationAction1 = [[UIMutableUserNotificationAction alloc] init];
notificationAction1.identifier = @"Accept";
notificationAction1.title = @"Accept";
notificationAction1.activationMode = UIUserNotificationActivationModeBackground;
notificationAction1.destructive = NO;
notificationAction1.authenticationRequired = NO;

UIMutableUserNotificationAction *notificationAction2 = [[UIMutableUserNotificationAction alloc] init];
notificationAction2.identifier = @"Reject";
notificationAction2.title = @"Reject";
notificationAction2.activationMode = UIUserNotificationActivationModeBackground;
notificationAction2.destructive = YES;
notificationAction2.authenticationRequired = YES;

UIMutableUserNotificationAction *notificationAction3 = [[UIMutableUserNotificationAction alloc] init];
notificationAction3.identifier = @"Reply";
notificationAction3.title = @"Reply";
notificationAction3.activationMode = UIUserNotificationActivationModeForeground;
notificationAction3.destructive = NO;
notificationAction3.authenticationRequired = YES;

UIMutableUserNotificationCategory *notificationCategory = [[UIMutableUserNotificationCategory alloc] init];
notificationCategory.identifier = @"Email";
[notificationCategory setActions:@[notificationAction1,notificationAction2,notificationAction3] forContext:UIUserNotificationActionContextDefault];
[notificationCategory setActions:@[notificationAction1,notificationAction2] forContext:UIUserNotificationActionContextMinimal];

NSSet *categories = [NSSet setWithObjects:notificationCategory, nil];

UIUserNotificationType notificationType = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:notificationType categories:categories];

[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];

UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
localNotification.alertBody = @"Testing";
localNotification.category = @"Email"; //  Same as category identifier
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

Ответ 2

Я хотел бы немного расширить Сурав Гупта. После того, как вы сделали то, что объяснил Сурав, вы должны реализовать делегатов для получения действий push-уведомлений. Делегаты:

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler {

// Handle actions of local notifications here. You can identify the action by using "identifier" and perform appropriate operations

     if(completionHandler != nil)    //Finally call completion handler if its not nil
         completionHandler();
}

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler {

// Handle actions of remote notifications here. You can identify the action by using "identifier" and perform appropriate operations

     if(completionHandler != nil)    //Finally call completion handler if its not nil
         completionHandler();
}

Вы можете сослаться на образец полезной нагрузки удаленных уведомлений здесь iOS8 Push alert Payload