Abierto Específicos de la Vista al Abrir la Aplicación desde la fecha de Notificación

Acabo de agregar notificaciones push a mi aplicación. Estoy queriendo tener, de manera que cuando un usuario abre la aplicación de una notificación, se abrirá una vista específica del controlador y no mi rootViewController. Aquí está mi AppDelegate:

#import "KFBAppDelegate.h"
#import "KFBViewController.h"
#import "AboutUs.h"
#import "ContactUs.h"
#import "KYFB.h"
#import "KFBNavControllerViewController.h"
#import "KFBTabBarViewController.h"
#import "RSFM.h"
#import "LegislatorInfo.h"
#import "Events.h"
#import "ActionAlertsViewController.h"
#import "UAirship.h"
#import "UAPush.h"
#import "UAAnalytics.h"

@implementation KFBAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    //This prevents the UA Library from registering with UIApplcation by default when
    //registerForRemoteNotifications is called. This will allow you to prompt your
    //users at a later time. This gives your app the opportunity to explain the benefits
    //of push or allows users to turn it on explicitly in a settings screen.
    //If you just want everyone to immediately be prompted for push, you can
    //leave this line out.
    //[UAPush setDefaultPushEnabledValue:NO];

    //Create Airship options dictionary and add the required UIApplication launchOptions
    NSMutableDictionary *takeOffOptions = [NSMutableDictionary dictionary];
    [takeOffOptions setValue:launchOptions forKey:UAirshipTakeOffOptionsLaunchOptionsKey];

    //Call takeOff (which creates the UAirship singleton), passing in the launch options so the
    //library can properly record when the app is launched from a push notification. This call is
    //Populate AirshipConfig.plist with your app's info from https://go.urbanairship.com
    [UAirship takeOff:takeOffOptions];

    //Set the icon badge to zero on startup (optional)
    [[UAPush shared] resetBadge];

    //Register for remote notfications with the UA Library. This call is required.
    [[UAPush shared] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                         UIRemoteNotificationTypeSound |

    //Handle any incoming incoming push notifications.
    //This will invoke `handleBackgroundNotification` on your UAPushNotificationDelegate.
    [[UAPush shared] handleNotification:[launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey]

    //self.tabBarController = [[UITabBarController alloc] initWithNibName:@"KFBViewController" bundle:nil];
    KFBViewController *rootView = [[KFBViewController alloc] initWithNibName:@"KFBViewController" bundle:nil];
    KFBNavControllerViewController *navController = [[KFBNavControllerViewController alloc] initWithRootViewController:rootView];
    navController.delegate = rootView;
    UIViewController *aboutUs = [[AboutUs alloc] initWithNibName:@"AboutUs" bundle:nil];

    KFBNavControllerViewController *navController1 = [[KFBNavControllerViewController alloc] initWithRootViewController:aboutUs];

    UIViewController *contactUs = [[ContactUs alloc] initWithNibName:@"ContactUs" bundle:nil];
    KFBNavControllerViewController *navController2 = [[KFBNavControllerViewController alloc] initWithRootViewController:contactUs];
    UIViewController *kyfb = [[KYFB alloc] initWithNibName:@"KYFB" bundle:nil];

    KFBNavControllerViewController *navController3 = [[KFBNavControllerViewController alloc] initWithRootViewController:kyfb];

    //UIViewController *rsfm = [[RSFM alloc] initWithNibName:@"RSFM" bundle:nil];
    //KFBNavControllerViewController *navController4 = [[KFBNavControllerViewController alloc] initWithRootViewController:rsfm];

    //UIViewController *li = [[LegislatorInfo alloc] initWithNibName:@"LegislatorInfo" bundle:nil];
    //KFBNavControllerViewController *navController5 = [[KFBNavControllerViewController alloc] initWithRootViewController:li];

    //UIViewController *events = [[Events alloc] initWithNibName:@"Events" bundle:nil];
    //KFBNavControllerViewController *navController6 = [[KFBNavControllerViewController alloc] initWithRootViewController:events];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    //self.viewController = [[KFBViewController alloc] initWithNibName:@"KFBViewController" bundle:nil];
    //self.window.rootViewController = self.viewController;
    self.tabBarController = [[KFBTabBarViewController alloc] init];
    self.tabBarController.viewControllers = @[navController, navController1, navController2, navController3];
    //self.tabBarController.customizableViewControllers = nil;

    self.window.rootViewController = self.tabBarController;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    return YES;

- (void)applicationWillResignActive:(UIApplication *)application
    //Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    //Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

- (void)applicationDidEnterBackground:(UIApplication *)application
    //Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    //If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

- (void)applicationWillEnterForeground:(UIApplication *)application
    //Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.

- (void)applicationDidBecomeActive:(UIApplication *)application
    //Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    UA_LDEBUG(@"Application did become active.");

    //Set the icon badge to zero on resume (optional)
    [[UAPush shared] resetBadge];

- (void)applicationWillTerminate:(UIApplication *)application
    //Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    [UAirship land];

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    //Updates the device token and registers the token with UA.
    [[UAPush shared] registerDeviceToken:deviceToken];

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *) error
    UA_LERR(@"Failed To Register For Remote Notifications With Error: %@", error);

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

    UA_LINFO(@"Received remote notification: %@", userInfo);

    //Send the alert to UA so that it can be handled and tracked as a direct response. This call
    //is required.
    [[UAPush shared] handleNotification:userInfo applicationState:application.applicationState];

    //Optionally provide a delegate that will be used to handle notifications received while the app is running
    //[UAPush shared].delegate = your custom push delegate class conforming to the UAPushNotificationDelegate protocol

    //Reset the badge after a push received (optional)
    [[UAPush shared] resetBadge];

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    return UIInterfaceOrientationMaskAllButUpsideDown;


Y aquí es el punto de vista controlador quiero abrir al abrir una notificación:

#import "ActionAlertsViewController.h"
#import "RSSChannel.h"
#import "RSSItem.h"
#import "WebViewController.h"
#import "CustomCellBackground.h"

@implementation ActionAlertsViewController
    UIActivityIndicatorView *loadingIndicator;
@synthesize webViewController;

- (void)viewDidLoad
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.title = @"Action Alerts";
    loadingIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    loadingIndicator.center = CGPointMake(160, 160);
    loadingIndicator.hidesWhenStopped = YES;
    [self.view addSubview:loadingIndicator];
    [loadingIndicator startAnimating];
    //UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
    //refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to Refresh"];
    //[refresh addTarget:self action:@selector(refreshView:)forControlEvents:UIControlEventValueChanged];
    //self.refreshControl = refresh;

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
    NSLog(@"%@ found a %@ element", self, elementName);
    if ([elementName isEqual:@"channel"])
        //If the parser saw a channel, create new instance, store in our ivar
        channel = [[RSSChannel alloc]init];

        //Give the channel object a pointer back to ourselves for later
        [channel setParentParserDelegate:self];

        //Set the parser's delegate to the channel object
        [parser setDelegate:channel];

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    //return 0;
    NSLog(@"channel items %d", [[channel items]count]);
    return [[channel items]count];

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    //return nil;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if (cell == nil)
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
        cell.textLabel.font=[UIFont systemFontOfSize:16.0];
    RSSItem *item = [[channel items]objectAtIndex:[indexPath row]];
    [[cell textLabel]setText:[item title]];
    cell.backgroundView = [[CustomCellBackground alloc] init];
    cell.selectedBackgroundView = [[CustomCellBackground alloc] init];
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.textLabel.highlightedTextColor = [UIColor darkGrayColor];

    return cell;

- (void)fetchEntries
    //Create a new data container for the stuff that comes back from the service
    xmlData = [[NSMutableData alloc]init];

    //Construct a URL that will ask the service for what you want -
    //note we can concatenate literal strings together on multiple lines in this way - this results in a single NSString instance
    NSURL *url = [NSURL URLWithString:@"http://kyfbnewsroom.com/category/public-affairs/notifications/feed/"];

    //Put that URL into an NSURLRequest
    NSURLRequest *req = [NSURLRequest requestWithURL:url];

    //Create a connection that will exchange this request for data from the URL
    connection = [[NSURLConnection alloc]initWithRequest:req delegate:self startImmediately:YES];

- (id)initWithStyle:(UITableViewStyle)style
    self = [super initWithStyle:style];

    if (self)
        [self fetchEntries];

    return self;

//This method will be called several times as the data arrives
- (void)connection:(NSURLConnection *)conn didReceiveData:(NSData *)data
    //Add the incoming chunk of data to the container we are keeping
    //The data always comes in the correct order
    [xmlData appendData:data];

- (void)connectionDidFinishLoading:(NSURLConnection *)conn
    /* We are just checking to make sure we are getting the XML
     NSString *xmlCheck = [[NSString alloc]initWithData:xmlData encoding:NSUTF8StringEncoding];
     NSLog(@"xmlCheck = %@", xmlCheck);*/

    [loadingIndicator stopAnimating];

    //Create the parser object with the data received from the web service
    NSXMLParser *parser = [[NSXMLParser alloc]initWithData:xmlData];

    //Give it a delegate - ignore the warning here for now
    [parser setDelegate:self];

    //Tell it to start parsing - the document will be parsed and the delegate of NSXMLParser will get all of its delegate messages sent to it before this line finishes execution - it is blocking
    [parser parse];

    //Get rid of the XML data as we no longer need it
    xmlData = nil;

    //Reload the table.. for now, the table will be empty
    NSMutableArray *notActionAlerts = [NSMutableArray array];
    for (RSSItem *object in channel.items) {
        if (!object.isActionAlert) {
            [notActionAlerts addObject:object];

    for (RSSItem *object in notActionAlerts) {
        [channel.items removeObject:object];

    [[self tableView]reloadData];

    NSLog(@"%@\n %@\n %@\n", channel, [channel title], [channel infoString]);

- (void)connection:(NSURLConnection *)conn didFailWithError:(NSError *)error
    //Release the connection object, we're done with it
    connection = nil;

    //Release the xmlData object, we're done with it
    xmlData = nil;

    //Grab the description of the error object passed to us
    NSString *errorString = [NSString stringWithFormat:@"Fetch failed: %@", [error localizedDescription]];

    //Create and show an alert view with this error displayed
    UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"Error" message:errorString delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [av show];

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    //Push the web view controller onto the navigation stack - this implicitly creates the web view controller's view the first time through
    //[[self navigationController]pushViewController:webViewController animated:YES];
    [self.navigationController pushViewController:webViewController animated:NO];

    //Grab the selected item
    RSSItem *entry = [[channel items]objectAtIndex:[indexPath row]];

    //Construct a URL with the link string of the item
    NSURL *url = [NSURL URLWithString:[entry link]];

    //Construct a request object with that URL
    NSURLRequest *req = [NSURLRequest requestWithURL:url];

    //Load the request into the web view
    [[webViewController webView]loadRequest:req];
    webViewController.hackyURL = url;
    //Set the title of the web view controller's navigation item
    //[[webViewController navigationItem]setTitle:[entry title]];

 -(void)refreshView:(UIRefreshControl *)refresh
    refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"Refreshing data..."];

    //custom refresh logic would be placed here...

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MMM d, h:mm a"];
    NSString *lastUpdated = [NSString stringWithFormat:@"Last updated on %@",[formatter stringFromDate:[NSDate date]]];
    refresh.attributedTitle = [[NSAttributedString alloc] initWithString:lastUpdated];
    [refresh endRefreshing];


Aquí está el fragmento de código que he añadido a mi método didFinishLaunchingWithOptions. Me he metido casi todo el trabajo. La web de las vistas ahora cuando se selecciona una fila y la barra de navegación es la que hay y aparentemente funciona como debería. El único problema que tengo ahora es conseguir la barra de pestañas para mostrar. Aquí está el fragmento de código que estoy utilizando actualmente.

UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

    if (notification)
        ActionAlertsViewController *actionAlerts = [[ActionAlertsViewController alloc] initWithStyle:UITableViewStylePlain];
        WebViewController *wvc = [[WebViewController alloc]init];
        [actionAlerts setWebViewController:wvc];
        KFBNavControllerViewController *navController7 = [[KFBNavControllerViewController alloc] initWithRootViewController:actionAlerts];
        [self.window.rootViewController presentViewController:navController7 animated:NO completion:nil];
InformationsquelleAutor raginggoat | 2013-03-19

3 Kommentare

  1. 41

    Puede publicar una notificación a usted mismo cuando usted recibe una notificación remota y mediante el registro de la viewcontroller a esta notificación, puede abrir un particular viewController una vez que se recibe una notificación.

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

    En su FirstViewController.m registro para escuchar a la presente notificación.

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

    Dentro del método se puede abrir particular viewController

     [self presentViewController:self.secondViewController animated:YES completion:nil];

    Finalmente anular el registro actual viewController a partir de la notificación en el método dealloc

    [[NSNotificationCenter defaultCenter] removeObserver:self];
    • No, esto sólo funciona si el usuario se ha pasado de mirar su FirstView? No creo que esto funciona si los que estaban viendo un punto de vista diferente, se fue a la pantalla de inicio, recibió una notificación y, a continuación, haga clic en él. No es sólo abrir la aplicación en la página que la dejaron, y que el observador nunca volvería a ver a la notificación?
    • una pregunta adicional, si la aplicación es asesinado, el iphone obtener el empuje de la información, haga clic en el empuje de la información, es abrir la aplicación y, a continuación, podría ir a la especial vc?
  2. 10

    Si se abre la aplicación de una notificación, cualquiera de los dos métodos de delegado de la aplicación será llamado

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


    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

    En caso de la segunda, la launchOptions te permitirá saber si la aplicación fue lanzada debido a una notificación remota o por algún otro motivo (ver el Lanzamiento de las Teclas de Opción aquí)

    Poner en un cheque en estos métodos, de modo que el viewcontroller se abrirá si la aplicación se inicia a partir de una notificación.

  3. 2

    Puede hacer algo como esto

      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
       //If application is launched due to  notification,present another view controller.
    UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if (notification)
        NotificationViewController *viewController = [[NotificationViewController alloc]initWithNibName:NSStringFromClass([NotificationViewController class]) bundle:nil];
        [self.window.rootViewController presentModalViewController:viewController animated:NO];
        [viewController release];
    return YES;
     -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
    NotificationViewController *viewController = [[NotificationViewController alloc]initWithNibName:NSStringFromClass([NotificationViewController class]) bundle:nil];
    [self.window.rootViewController presentModalViewController:viewController animated:NO];
    [viewController release];
    • Usted está utilizando una notificación local. Estoy tratando de hacer esto con mando a las notificaciones push. He probado tu código y, cuando recibí una notificación remota y aprovechado de ti, mi aplicación se empezó a abrir, a continuación, se estrelló.
    • Esto funciona, pero yo siento la necesidad de incluir la barra de navegación y barra de pestañas.

Kommentieren Sie den Artikel

Bitte geben Sie Ihren Kommentar ein!
Bitte geben Sie hier Ihren Namen ein

Pruebas en línea