1

I've been busting my head against the table al morning with this. I know it's been asked before, but I really don't understand what's the problem with my code.

Here's the code I use to register and send interactive notifications.

Thanks in advance.

#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];
}
Ranbeuer
  • 167
  • 3
  • 12
  • http://stackoverflow.com/questions/29817994/ios-8-interactive-notifications-not-showing-actions/37984024#37984024 – Zaraki Jun 23 '16 at 06:45

1 Answers1

1

Well first things first,

I don't see you actually calling your sendLocalNotification method anywhere in your didFinishLaunchingWithOptions: method. You should call it to see if it actually fires, because aesthetically, you've done everything you should have done in order to set up a category-based notification. kudos, and well written for a newcomer to StackOverflow. Additionally, if you are calling it and you just simply omitted it from the question, then your problem probably lies in how you are setting your method. Right now it's a class method, if its intended or not I'm not sure, however, my point is, if it's not working, and the notifications are showing up, just not the category actions, then you should probably call it correctly or change it to an instance method.

See the difference between the two here


You are so close. You had everything set up properly and then swayed on the end. You should call your sendLocalNotification like this:

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

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

}

And change your method to properly reflect what kind of method it is. I included a a link above to show you the difference. You will set it like this:

-(void)sendLocalNotification:

NOT

+(void)sendLocalNotification:
Community
  • 1
  • 1
soulshined
  • 9,612
  • 5
  • 44
  • 79
  • I'm using it, I just not wrote where. The thing is the notifications are showing, but the category actions are the ones missing. – Ranbeuer Sep 02 '15 at 21:34
  • show how you are calling the method then please, because as I stated in my answer, your setting it as a class method right now @Ranbeuer – soulshined Sep 02 '15 at 21:35
  • I was using it like this: `[AppDelegate sendLocalNotification:@"Estas cerca de una máquina expendedora. ¿Quieres realizar una compra?" info:@{@"type":@"near_vending"}];` I changed it to an instance method and now I use it like this: `[app sendLocalNotification:@"Estas cerca de una máquina expendedora. ¿Quieres realizar una compra?" info:@{@"type":@"near_vending"}];` No difference between them. – Ranbeuer Sep 02 '15 at 21:40
  • @Ranbeuer unless your class names is `app` then you should review my edit – soulshined Sep 02 '15 at 21:44
  • My class name is AppDelegate but I have a define (app) that references to the shared application instance. – Ranbeuer Sep 02 '15 at 21:50
  • @Ranbeuer but there is no reason to do that since it's an instance of it's self. Just call self and change the method to an instance method – soulshined Sep 02 '15 at 21:51
  • I just discovered the reason, turns out I started editting someone else's code and added `registerForNotification` at the top of `didFinishLaunchingWithOptions` but almost at the end of the method there was another call to `registerUserNotificationSettings` effectively overwriting my settings. – Ranbeuer Sep 02 '15 at 22:09
  • 1
    You should always post your code as is so conversations like this don't happen ;). Even stuff like calling the sendLocalNotification. But glad your good to go @Ranbeuer!! – soulshined Sep 02 '15 at 22:12