1

I understand that we can handle push notifications via the method:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

and we can check if the app was running in the foreground:

if (application.applicationState == UIApplicationStateActive ) { ... }

How do we show the exact same notification with localisation?

NSString *message = [[[userInfo valueForKey:@"aps"] valueForKey:@"alert"] valueForKey:@"loc-key"];
NSString *trueMessage = NSLocalizedString(message, nil);
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert"
                                                            message:trueMessage
                                                   cancelButtonItem:@"OK"
                                                   otherButtonItems:@"Show", nil];
[alertView show];

This shows the raw unlocalized text, e.g. "You have a new alert from %1@ on %2@."

My question is, how can we place the loc-args inside the UIAlertView as well, when the app is running in the foreground?

Sidwyn Koh
  • 1,742
  • 2
  • 21
  • 29

2 Answers2

1

A not so simple workaround I came up with (assuming 3 is the maximum number of variables you have across all localized strings):

    // Max is 3 variables
    NSString *variableOne = @"";
    NSString *variableTwo = @"";
    NSString *variableThree = @"";

    int i = 0;
    for (NSString *eachVariable in [[[userInfo valueForKey:@"aps"] valueForKey:@"alert"] valueForKey:@"loc-args"]) {
        switch (i) {
            case 0:
                variableOne = eachVariable;
                break;
            case 1:
                variableTwo = eachVariable;
                break;
            case 2:
                variableThree = eachVariable;

            default:
                break;
        }
        i++;
    }

    NSString *message = [[[userInfo valueForKey:@"aps"] valueForKey:@"alert"] valueForKey:@"loc-key"];

    NSString *trueMessage = [NSString stringWithFormat:NSLocalizedString(message, nil), variableOne, variableTwo, variableThree];

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert"
                                                        message:trueMessage
                                               cancelButtonItem:@"Cancel"
                                               otherButtonItems:@"Show", nil];
    [alertView show];
Sidwyn Koh
  • 1,742
  • 2
  • 21
  • 29
1

I'd propose you to create a fake va_list as explained here: fake va_list in ARC.

This will give a code that looks a bit like this:

NSString *pushBody;
id alert = userInfo[@"aps"][@"alert"];
if ([alert isKindOfClass:[NSString class]]) pushBody = alert;
if ([alert isKindOfClass:[NSDictionary class]])
{
    pushBody = alert[@"loc-key"];
    if (pushBody == nil)
    {
        pushBody = alert[@"body"];
    }
    else
    {
        // Build a fake va_list from the parameters.
        NSArray *locArgs = alert[@"loc-args"];
        NSRange range = NSMakeRange(0, [locArgs count]);
        NSMutableData* fakeVaList = [NSMutableData dataWithLength: sizeof(id) * [locArgs count]];
        [locArgs getObjects:(__unsafe_unretained id *)fakeVaList.mutableBytes range:range];

        pushBody = StrLoc(pushBody, @"Remote Notif");
        pushBody = [[NSString alloc] initWithFormat:pushBody arguments:fakeVaList.mutableBytes];
    }
}

Tell me if this can work for you…

Community
  • 1
  • 1
MonsieurDart
  • 6,005
  • 2
  • 43
  • 45