按钮不会显示在iOS中的本地交互式通知中

时间:2015-09-02 20:47:29

标签: ios notifications

我早上用这个把头撞到了桌子上。我知道之前已被问过,但我真的不明白代码的问题是什么。

这是我用来注册和发送交互式通知的代码。

提前致谢。

#define kNotificationActionNo @"ActionKeyNo"
#define kNotificationActionYes @"ActionKeyYes"
#define kNotificationCategoryVendingMachine @"VendingMachineNotification"

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

    [self registerForNotification];

    ...
}

+ (void)sendLocalNotification:(NSString*)message info:(NSDictionary*)infoDict {
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    localNotification.alertBody = message;
    localNotification.fireDate = [NSDate date];
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    localNotification.category = kNotificationCategoryVendingMachine;
    localNotification.userInfo = infoDict;
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

- (void)registerForNotification {

    UIMutableUserNotificationAction *action1;
    action1 = [[UIMutableUserNotificationAction alloc] init];
    [action1 setActivationMode:UIUserNotificationActivationModeBackground];
    [action1 setTitle:NSLocalizedString(@"YES_LEGEND", nil)];
    [action1 setIdentifier:kNotificationActionYes];
    [action1 setDestructive:NO];
    [action1 setAuthenticationRequired:NO];

    UIMutableUserNotificationAction *action2;
    action2 = [[UIMutableUserNotificationAction alloc] init];
    [action2 setActivationMode:UIUserNotificationActivationModeBackground];
    [action2 setTitle:NSLocalizedString(@"NO_LEGEND", nil)];
    [action2 setIdentifier:kNotificationActionNo];
    [action2 setDestructive:NO];
    [action2 setAuthenticationRequired:NO];

    UIMutableUserNotificationCategory *actionCategory;
    actionCategory = [[UIMutableUserNotificationCategory alloc] init];
    [actionCategory setIdentifier:kNotificationCategoryVendingMachine];
    [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];
}

1 个答案:

答案 0 :(得分:1)

先做好第一件事,

我没有看到您实际在sendLocalNotification方法中的任何位置调用didFinishLaunchingWithOptions:方法。您应该调用它来查看它是否实际触发,因为在美学上,您已经完成了为完成基于类别的通知而应该完成的所有操作。荣幸,并为StackOverflow的新人写得很好。另外,如果你正在调用它而你只是在问题中省略它,那么你的问题可能在于你如何设置你的方法。现在它是一个类方法,如果它的意图或不是我不确定,但是,我的观点是,如果它不起作用,通知 显示,只是没有类别操作,那么你应该正确地调用它或将其更改为实例方法。

See the difference between the two here

你是如此亲密。你把所有东西都设置得恰到好处,然后在最后摇摆不定。你应该像这样打电话给sendLocalNotification

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

  [self sendLocalNotification:@"Estas cerca de una máquina expendedora. ¿Quieres realizar una compra?" info:@{@"type":@"near_vending"}];

}

并更改您的方法以正确反映它是什么类型的方法。我在上面添加了一个链接,向您展示其中的差异。您将这样设置:

-(void)sendLocalNotification:

+(void)sendLocalNotification: