Search code examples
iosnotificationcenter

Premeditated Notifications


Is it possible to setup an app to send notifications from it's self at specified times? Perhaps by putting them in a queue? As of now I'm having to go through a push notification server and that seems like over kill when I'm sending messages out every Tuesday morning. Also, what is this called (so I can better research it on the web)


Solution

  • You can schedule notification like this:

    - (void)scheduleNotification
    {
        //Cancel all previous Local Notifications
        [[UIApplication sharedApplication] cancelAllLocalNotifications];
    
        //Set new Local Notifications
        Class cls = NSClassFromString(@"UILocalNotification");
        if (cls != nil)
        {
            UILocalNotification *notif = [[cls alloc] init];
            //3 days
            notif.fireDate = [NSDate dateWithTimeInterval:60.0f*60.0f*24.0f*3.0f sinceDate:[NSDate date]];
            notif.timeZone = [NSTimeZone defaultTimeZone];
            notif.alertBody = @"Come Back Again! Lets blast All Zombie!";
            notif.alertAction = @"PLAY";
            notif.soundName = UILocalNotificationDefaultSoundName;
            notif.applicationIconBadgeNumber = 1;
            [[UIApplication sharedApplication] scheduleLocalNotification:notif];
            }    
    }
    
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        [application registerForRemoteNotificationTypes:
         UIRemoteNotificationTypeBadge |
         UIRemoteNotificationTypeAlert |
         UIRemoteNotificationTypeSound];
    
        [self scheduleNotification];
        ..
    }