【问题标题】:Is it possible to show the latest Push Notification in a Label in the StoryBoard?是否可以在 StoryBoard 的标签中显示最新的推送通知?
【发布时间】:2012-12-23 11:02:12
【问题描述】:

我喜欢在我的主 StoryBoard 的标签中显示最新的推送通知我使用此代码在我的 AppDelegate.m 中显示警报消息:

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

    NSDictionary *test =(NSDictionary *)[userInfo objectForKey:@"aps"];
    NSString *alertString =(NSString *) [test objectForKey:@"alert"];
    NSLog(@"String recieved: %@",alertString);


    UIApplicationState state = [[UIApplication sharedApplication] applicationState];

    if (state == UIApplicationStateActive) {
        UIAlertView *alertmessage=[[UIAlertView alloc]initWithTitle:@"Geier"
                                                            message:alertString                                                   delegate:self
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];


        [alertmessage show];

        AudioServicesPlaySystemSound(1002);


    }

}

我在我的 ViewController.m 文件 latestpush.text = @"%@",alertString; 中尝试过这个,但它不起作用。

有人可以帮我吗?

谢谢:)

【问题讨论】:

    标签: iphone ios uilabel apple-push-notifications appdelegate


    【解决方案1】:

    您需要使文本可用于视图控制器。

    您可以通过从application:didReceiveRemoteNotification: 内部发送带有警报消息的自定义 NSNotification 来做到这一点

    [[NSNotificationCenter defaultCenter] 
            postNotificationName:@"PushAlertNotification" 
            object:self
            userInfo:@{@"alertString":alertString}];
    

    在视图控制器的 viewDidLoad 方法中,注册为观察者:

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

    并在视图控制器中创建updateStoryboard: 方法:

    - (void) updateStoryboard:(NSNotification *) notification {
        self.latestpush.text = notification.userInfo[@"alertString"];
    }
    

    另一种解决方案是在您的 AppDelegate 中创建一个属性,该属性将 ViewController 作为观察者。

    AppDelegate.h(将 ViewController 更改为您的 VC 的实际类型)。

    @property (nonatomic, weak) ViewController *observer;

    在 ViewController 中创建一个接受 NSString 的方法并让该方法更新您的 Storyboard。

    ViewController.m

    -(void)updateStoryboard(NSString *alertString) {
       self.latestpush.text = alertString;
    }
    

    另外,在 ViewContoller 的 viewDidLoad 方法中,向 appDelegate 注册自己:

    - (void)viewDidLoad {
        [super viewDidLoad];
        AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
        delegate.observer = self;
    }
    

    application:didReceiveRemoteNotification: 方法中调用 updateStoryboard:

    [self.observer updateStoryboard:alertString];

    【讨论】:

    • @j_Shapiro 这两种方法都不适合我:(但我完全按照你说的做了。
    • 给我一些更多的信息继续......他们怎么不工作?在您创建通知之前,alertString 是否为零?您看到了什么错误?
    • Push进来没有错误但是标签没有填写Push消息。
    • 在你发布消息之前......它是零吗?
    • 如果你的意思是 viewDidUnload 中的标签是它被设置为 nil: latestpush = nil;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多