Return to WrightsCS.com   |   Check out WrightsCSApps.com   |   How to Post Snippets  

Not registered? Thats okay! You can Submit a Snippet without being a member of this site!


*****1vote

Here is a fun way to show local notifications. The first showLocalNotificationWithTitle: andAlertAction: will show a notification to the user via the banner as well as place the message in the Notification Center. The second method application: didReceiveLocalNotification: demonstrates how to direct the user based on the action of the notification. For example, if you want to notify the user about a new feature in the app and direct them to that view once they acknowledge the notification.

Banner Notification

Notification Center Item

#pragma mark - Local Notifications

- (void)showLocalNotificationWithTitle:(NSString *)title andAlertAction:(NSString *)action
{
    UIApplication * app = [UIApplication sharedApplication];
    NSArray * existingNotifications = [app scheduledLocalNotifications];
	
    // Clear any old notifications before scheduling a new one.
    if ( [existingNotifications count] > 0 )
         [app cancelAllLocalNotifications];
	
    // Create a new notification
    UILocalNotification * notification = [[UILocalNotification alloc] init];
    if ( notification )
    {
        NSDate *date = [NSDate date];
        NSDate *futureDate = [date dateByAddingTimeInterval:3];
        notification.fireDate = futureDate;
        notification.timeZone = [NSTimeZone defaultTimeZone];
        notification.repeatInterval = 0;
        notification.applicationIconBadgeNumber = 0;
        notification.alertBody = title;
        notification.alertAction = action; // See application:didReceiveLocalNotification:
        notification.userInfo = nil;
        [notification scheduleLocalNotification:alarm];
    }
}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    /*
        open app: open the app to the dashboard.
        rate the app: redirect the user to the App Store to rate.
     */
    
    if ( [notification.alertAction isEqualToString:@"open app"] ) {
         SomeViewController *svc = [[SomeViewController alloc] init];
         self.navigationController pushViewController:svc animated:NO];
    }
    
    if ( [notification.alertAction isEqualToString:@"rate the app"] ) {
         [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=546965677"]];
    }
    
    NSString * state = @"Unknown";
    
    if ( application.applicationState == UIApplicationStateInactive ) {
        //The application received the notification from an inactive state, i.e. the user tapped the "View" button for the alert.
        //If the visible view controller in your view controller stack isn't the one you need then show the right one.
        state = @"UIApplicationStateInactive";
    }
    
    if( application.applicationState == UIApplicationStateActive ) {
        //The application received a notification in the active state, so you can display an alert view or do something appropriate.
        state = @"UIApplicationStateActive";
    }
    
    NSLog(@"State: %@, Notification: %@",state, notification.alertAction);
}

/*
 * Usage
 */

[self showLocalNotificationWithTitle:@"Like using my app?\nPlease rate it in the iTunes App Store!" andAlertAction:@"rate the app"];

Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

© Copyright 2011 Wright's Computer Services.   All rights reserved Suffusion theme by Sayontan Sinha