将从 AppDelegate 的推送通知接收的数据传递到 ViewController [英] Passing data received from a push notification from the AppDelegate to a ViewController

查看:20
本文介绍了将从 AppDelegate 的推送通知接收的数据传递到 ViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,有一个集合视图显示一组从网络服务中检索到的图像.每个图像都有标签.因此,该应用还可以使用标签过滤图像.

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.

我已经完成了一半.我成功收到带有消息的推送通知到 AppDelegate.m 文件中的 didReceiveRemoteNotification 方法.现在我需要将它传递给集合视图所在的视图控制器.我被困在这一点上.我不知道如何将它发送到视图控制器.

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.

我尝试在 App 委托中声明一个属性,将消息值分配给它并从视图控制器中引用它,但它不起作用.我绑定了代表、通知中心、用户默认设置,但没有任何效果.

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?

谢谢.

这是我的代码.我尝试的最后一种方法是本地通知.

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

推荐答案

案例 1:如果您的应用程序处于后台并且用户通过单击通知启动应用程序,那么您可以检查应用程序是否启动表单通知或正常

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:如果你的应用处于前台,将会在 didReceiveRemoteNotification 中收到通知

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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆