繁体   English   中英

如何通过执行有效负载中包含的操作来对APNS推送消息采取行动

[英]How can I able to act upon the APNS push message by taking an action contained in the payload

我是Objective-c,xcode和应用开发人员的新手,因此请记住这一点。

我可以通过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中引用我的标签。 因此,我既无法更新背景色,也​​无法调用控制器上的方法来做到这一点。

任何帮助,将不胜感激。

在您的代表中添加:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM