Objective C中的交互式通知



我想用两个带有动作的按钮实现交互式通知。我需要在哪里创建按钮和它们的动作?

我已经这样做了,但是从哪里我必须调用这个方法?

- (void)registerForNotification
{
    UIMutableUserNotificationAction *action1;
    action1 = [[UIMutableUserNotificationAction alloc] init];
    [action1 setActivationMode:UIUserNotificationActivationModeBackground];
    [action1 setTitle:@"Action 1"];
    [action1 setIdentifier:NotificationActionOneIdent];
    [action1 setDestructive:NO];
    [action1 setAuthenticationRequired:NO];
    UIMutableUserNotificationAction *action2;
    action2 = [[UIMutableUserNotificationAction alloc] init];
    [action2 setActivationMode:UIUserNotificationActivationModeBackground];
    [action2 setTitle:@"Action 2"];
    [action2 setIdentifier:NotificationActionTwoIdent];
    [action2 setDestructive:NO];
    [action2 setAuthenticationRequired:NO];
    UIMutableUserNotificationCategory *actionCategory;
    actionCategory = [[UIMutableUserNotificationCategory alloc] init];
    [actionCategory setIdentifier:NotificationCategoryIdent];
    [actionCategory setActions:@[action1, action2]
                    forContext:UIUserNotificationActionContextDefault];
    NSSet *categories = [NSSet setWithObject:actionCategory];
    UIUserNotificationType types = (UIUserNotificationTypeAlert|
                                    UIUserNotificationTypeSound|
                                    UIUserNotificationTypeBadge);
    UIUserNotificationSettings *settings;
    settings = [UIUserNotificationSettings settingsForTypes:types
                                                 categories:categories];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
    func showInterActiveNotification() {
        let category = UIMutableUserNotificationCategory()
        let restartAction = UIMutableUserNotificationAction()
        restartAction.identifier = "xx"
        restartAction.destructive = false
        restartAction.title = "Restart"
        restartAction.activationMode = .Background
        restartAction.authenticationRequired = false
        let categoryIdentifier = "category.identifier"
        category.identifier = categoryIdentifier
        category.setActions([restartAction], forContext: .Minimal)
        category.setActions([restartAction], forContext: .Default)
        let categories = Set(arrayLiteral: category)
        let settings = UIUserNotificationSettings(forTypes: [.Alert, .Sound], categories: categories)
        UIApplication.sharedApplication().registerUserNotificationSettings(settings)

        let localNotif = UILocalNotification()
        localNotif.alertBody = "testBody"
        localNotif.category = categoryIdentifier
        // Notification will be shown after 10 second (IMPORTANT: if you want to see notification you have to close or put app into background)
        localNotif.fireDate = NSDate().dateByAddingTimeInterval(10)
        UIApplication.sharedApplication().scheduleLocalNotification(localNotif)
    }

处理通知
     func application(application: UIApplication, handleActionWithIdentifier identifier: String?,
                forLocalNotification notification: UILocalNotification, completionHandler: () -> Void) {
  completionHandler()
}

在AppDelegate中的didFinishLaunchingWithOptions方法中调用它。你不需要做其他事情。要处理这些动作,请调用处理程序。

如果你想检查本地通知PLZ使用这个

-(void)localnotificationGenrated
{
    UILocalNotification* localNotification = [[UILocalNotification alloc] init];
    localNotification.soundName = @"Default";
    localNotification.alertBody = @"my notification;)";
    localNotification.fireDate = [NSDate date];
    localNotification.category = NotificationActionOneIdent;
    localNotification.repeatInterval = kCFCalendarUnitMinute;
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

,如果你想用推送通知创建有效负载与类别。

这样的

{"aps":{"alert":"Hello Testing","badge":1,"sound":"default","category":"your_category_key"}}
并调用你的方法
 - (void)registerForNotification

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

最新更新