Sandeep Kumar
Sandeep Kumar

Reputation: 617

How to Show Push Notifications on another ViewController. I am receiveing it on Appdelegate page on all the three cases

Please tell me how to send the appdelegate to another viewcontroller using NSdoctionary and receive it on the new view controller and show it.

-(void)application:(UIApplication*)application didReceiveRemoteNotification: (NSDictionary*)userInfo
{
    NSLog(@"Push received: %@", userInfo);
}

Upvotes: 0

Views: 530

Answers (2)

Oleg Shulakov
Oleg Shulakov

Reputation: 916

There are few ways/ But I prefer using https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/index.html

Somrewere in constants

NSString *const NOTIFICATION_ID = @"com.yourapp.notificationID";

ViewController.m

- (void)viewDidLoad
{
    NSNotificationCenter * notificationCenter = [NSNotificationCenter defaultCenter];
    [notificationCenter addObserver:self
                               selector:@selector(notificationRecieved:)
                                   name:NOTIFICATION_ID
                                 object:nil];
}

- (void)notificationRecieved:(NSNotification*)notification
{
    NSLog(@"Push received: %@", notification.userInfo);
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:NOTIFICATION_ID object:nil];
}

AppDelegate.m

-(void)application:(UIApplication*)application didReceiveRemoteNotification: (NSDictionary*)userInfo 
{
    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_ID object:nil userInfo:userInfo];
}

Upvotes: 4

David Hoerl
David Hoerl

Reputation: 41632

Use Notifications. The app delegate will post the notification with the notification dictionary, the view controller will be listening for it.

Upvotes: 0

Related Questions