目标C中的交互式通知

时间:2016-08-10 10:26:46

标签: ios objective-c xcode push-notification

我想用两个带操作的按钮实现交互式通知。我必须在哪里创建按钮及其动作?

我已经完成了这个,但我必须从这里调用这个方法?

- (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];
}

3 个答案:

答案 0 :(得分:0)

    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()
}

答案 1 :(得分:0)

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

答案 2 :(得分:0)

如果您想查看本地通知,请使用此

-(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