从传递推送通知收到的AppDelegate到数据的ViewController [英] Passing data received from a push notification from the AppDelegate to a ViewController
问题描述
在我的应用程序,有显示一组从Web服务检索到的图像集合视图。每个图像都有标记。因此该应用使用标签和过滤图像的能力。
现在我尝试推送通知添加到该应用程序。当新图像已被添加到服务器的推送通知被发送。被标记这些图片说,最新的。我路过该标签的消息通过推送通知和我需要的是当用户点击了推送通知打开应用程序,它应该加载最新的图像集合视图。
我一半的方式完成的。我收到推送通知与消息成功到 didReceiveRemoteNotification
方法在 AppDelegate.m
文件。现在我需要它传递给视图控制器,其中收集的观点是。我被困在这一点上。我无法弄清楚如何通过发送到视图控制器。
我试着在应用程序的委托声明一个属性,该消息值分配给它,并从视图控制器提到它,但它没有工作。我绑代表,通知中心,用户的默认值,但毫无效果。
谁能告诉我如何做到这一点?
感谢您。
编辑:
下面是我的code。我想最后一个方法是本地通知。
AppDelegate.m
- (无效)应用:(*的UIApplication)的应用didReceiveRemoteNotification:(NSDictionary的*)USERINFO
{
[NSNotificationCenter defaultCenter] postNotificationName:@PushNotificationMessageReceivedNotification的对象:无用户信息:用户信息];
}
ViewController.m
- (无效)viewWillAppear中:(BOOL)动画
{
[超级viewWillAppear中:动画] [NSNotificationCenter defaultCenter]的addObserver:自我
选择:@选择(remoteNotificationReceived :)名:@PushNotificationMessageReceivedNotification
对象:无];
} - (无效)remoteNotificationReceived:(NSNotification *)的通知
{
的NSLog(@通知:%@,notification.userInfo);
* NSString的味精= [[notification.userInfo valueForKey:@APS] valueForKey:@警报];
self.label.text =味精;
}
案例1:如果你的应用程序是背景和用户启动应用程序,并发出通知点击,那么你必须检查,如果应用程序推出形式通知或正常
- (BOOL)应用:(*的UIApplication)的应用didFinishLaunchingWithOptions:(NSDictionary的*)launchOptions {
self.window = [[一个UIWindow页头] initWithFrame:方法[UIScreen mainScreen]界限]];
*的NSDictionary = remoteNotificationPayload [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
如果(remoteNotificationPayload){
[NSNotificationCenter defaultCenter] postNotificationName:@通知的对象:无用户信息:remoteNotificationPayload];
}
返回YES; }
2种情况:如果您的应用程序是forground通知将在didReceiveRemoteNotification收到
- (无效)应用:(*的UIApplication)的应用didReceiveRemoteNotification:(NSDictionary的*){用户信息
的NSLog(@用户信息%@,用户信息); [NSNotificationCenter defaultCenter] postNotificationName:@通知的对象:无用户信息:用户信息];
}
现在你并与本地通知任何控制器添加一个观察者,做你的魔杖做什么
In my app, there's a collection view displaying a set of images retrieved from a web service. Each image has tags. So the app has the ability to filter images using tags as well.
Now I'm trying to add push notifications to this app. A push notification is sent when new images have been added to the server. These images are tagged say, latest. I'm passing that tag as the message via a push notification and what I need is when the user taps on the push notification to open the app, it should load the latest new images to the collection view.
I'm half way done. I receive the push notification with the message successfully to the didReceiveRemoteNotification
method in the AppDelegate.m
file. Now I need to pass it on to the view controller where the collection view is. I'm stuck at this point. I can't figure out how to send it over to the view controller.
I tried declaring a property in the App delegate, assign the message value to it and referring it from the view controller but it didn't work. I tied delegates, notification center, user defaults but nothing worked.
Can anyone please tell me how to accomplish this?
Thank you.
Edit:
Here's my code. The last method I tried was the local notifications.
AppDelegate.m
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"PushNotificationMessageReceivedNotification" object:nil userInfo:userInfo];
}
ViewController.m
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(remoteNotificationReceived:) name:@"PushNotificationMessageReceivedNotification"
object:nil];
}
- (void)remoteNotificationReceived:(NSNotification *)notification
{
NSLog(@"Notification: %@", notification.userInfo);
NSString *msg = [[notification.userInfo valueForKey:@"aps"] valueForKey:@"alert"];
self.label.text = msg;
}
Case 1: if your app is background and user launches app with notification click then you have the check if app launched form notification or normal
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
NSDictionary *remoteNotificationPayload = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotificationPayload) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"notification" object:nil userInfo:remoteNotificationPayload];
}
return YES; }
Case2: If your app is in forground notification will be received in didReceiveRemoteNotification
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(@"userinfo %@",userInfo);
[[NSNotificationCenter defaultCenter] postNotificationName:@"notification" object:nil userInfo:userInfo];
}
Now you and add a observer in any controller with Local notification and do what you wand to do
这篇关于从传递推送通知收到的AppDelegate到数据的ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!