我是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引用我的标签。因此,我无法更新背景颜色,甚至无法在控制器上调用方法来执行此操作。
任何这方面的帮助将不胜感激。
在您的委托中添加:
- (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];
}
}