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

标签 ios notifications

我整个早上都在用这个把头撞在 table 上。我知道之前有人问过这个问题,但我真的不明白我的代码有什么问题。

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

提前致谢。

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

最佳答案

好吧,首先,

我没有看到您实际上在 didFinishLaunchingWithOptions: 方法中的任何地方调用了您的 sendLocalNotification 方法。您应该调用它以查看它是否真的触发,因为从美学上讲,您已经完成了为设置基于类别的通知而应该做的所有事情。荣誉,为 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"}];

}

并更改您的方法以正确反射(reflect)它是哪种方法。我在上面包含了一个链接来向您展示不同之处。您将像这样设置它:

-(void)sendLocalNotification:

+(void)sendLocalNotification:

关于ios - 按钮不显示在 iOS 中的本地交互式通知中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32362822/

相关文章:

iphone - 音频队列启动失败

ios - 如何打印 SFSafariViewController 完整网页?

ios - FetchAssetsWithLocalIdentifiers 返回空数组

ios - 如何仅在应用程序中使用 iOS 推送通知

ios - 本地通知显示两次

ios - 如何知道用户在推送通知中点击了 "Don' t 允许”

ios - 当我将高度设置得太低时,UICollectionView 单元格不显示

ios - 投影矩阵有什么用?

jsf - 如何在 JSF 组件中监视异步/后台线程状态并获取通知

android - 在收到通知时显示警报或 View