当app在前台运行时,显示与UIRemoteNotification相同的UIAlertView

时间:2012-10-28 10:44:40

标签: iphone uialertview apple-push-notifications

我知道我们可以通过以下方法处理推送通知:

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

我们可以检查应用是否在前台运行:

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

我们如何通过本地化显示完全相同的通知?

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

这显示原始的非本地化文本,例如“你在%2 @%@上有一个新警告。”

我的问题是,当应用程序在前台运行时,如何将loc-args放在UIAlertView中?

2 个答案:

答案 0 :(得分:1)

我提出了一个不那么简单的解决方法(假设3是所有本地化字符串中的最大变量数):

    // 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];

答案 1 :(得分:1)

我建议您按照此处的说明创建假va_listfake va_list in ARC

这将给出一个看起来像这样的代码:

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

告诉我这是否适合你...