1

我是 Objective-c、xcode 和 app dev 的新手,所以请记住这一点。

我可以通过 APNS 向我的新兴应用发送推送通知。我可以看到 JSON 消息并且可以 NSSLog 它。

Payload: {
    aps = {
        alert = {
            "action-loc-key" = Reveal;
            body = "Hi Aleem, we have a new special offer just for you!";
        };
        badge = 70;
        sound = default;
    };

    myCMD = {
        "update_colour" = red;
    };
}

到目前为止一切都很好。但是,我需要能够通过采取行动来对推送消息采取行动。例如,我希望能够提取update_colour并使用值 red 将我唯一的控制器上标签的背景颜色更改为红色。

我的问题是我无法从我的 appdelegate.m 中引用我的标签。因此,我也无法更新背景颜色,甚至无法调用控制器上的方法来执行此操作。

对此的任何帮助将不胜感激。

4

1 回答 1

1

在您的委托中添加:

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

然后,当应用程序运行时收到推送通知/用户打开推送通知时,您可以访问通知有效负载并对其采取行动,然后您可以向视图控制器发送通知。

在您的视图中添加观察者:

[[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(backgroundChanged:)
                                         name:@"ChangeBackground"
                                       object:nil];

添加处理它。

- (void)backgroundChanged:(NSNotification *)notification {
    NSDictionary *dict = [notification userInfo];

    NSLog(@"%@" [[dict valueForKey:@"myCMD"] valueForKey:@"background-colour"]);

    label.backgroundColor = [UIColor xxx];
}

然后在委托中:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    if([userInfo valueForKey:@"myCMD"]) {
            NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
        [notificationCenter postNotificationName:@"ChangeBackground"
                                    object:nil
                                    userInfo:userInfo];
    }
}
于 2012-09-23T11:52:09.433 回答