1

メソッドを介してプッシュ通知を処理できることを理解しています。

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

これは、ローカライズされていない生のテキストを示しています。たとえば、「%1@ から %2@ に新しいアラートがあります。

私の質問は、loc-argsアプリがフォアグラウンドで実行されているときに、UIAlertView 内に配置するにはどうすればよいですか?

4

2 に答える 2

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];
于 2012-10-28T10:57:41.763 に答える
1

ここで説明されているように、偽物を作成することをお勧めしますva_list:偽の 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];
    }
}

これがうまくいくかどうか教えてください…</p>

于 2013-10-17T15:05:44.327 に答える