Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone : Daily local notifications

I am trying to implement local notification

This is what I have set

    // Current date
    NSDate *date = [NSDate date]; 

    // Add one minute to the current time
    NSDate *dateToFire = [date dateByAddingTimeInterval:20];

    // Set the fire date/time
    [localNotification setFireDate:dateToFire];
    [localNotification setTimeZone:[NSTimeZone defaultTimeZone]];

Instead of 20, I want to put a fixed time(daily)to start push.

For ex:I want to push notification pop up at every 6:00AM.

How can do that ?

Thanks

like image 986
Shishir.bobby Avatar asked Jul 03 '12 01:07

Shishir.bobby


People also ask

What is local notification in IOS?

Use local notifications to get the user's attention. You can display an alert, play a sound, or badge your app's icon. For example, a background app could ask the system to display an alert when your app finishes a particular task. Always use local notifications to convey important information that the user wants.

What is the difference between local notification and push notification?

The essential difference between local notifications and push notifications is simple: Local notifications are scheduled by an app locally and are delivered by the same device. Push notifications are sent by a remote server (its provider) which sends these notifications to devices on which the app is installed.


3 Answers

You just need to properly create a NSDate object to be your fire date (time). Instead of using [NSDate dateByAddingTimeInterval: 20], use something like this:

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay: 3];
[components setMonth: 7];
[components setYear: 2012];
[components setHour: 6];
[components setMinute: 0];
[components setSecond: 0];
[calendar setTimeZone: [NSTimeZone defaultTimeZone]];
NSDate *dateToFire = [calendar dateFromComponents:components];

Here are the Apple NSDateComponents API docs

And then when you add the date to the notification, set the repeat interval to one day:

[localNotification setFireDate: dateToFire];
[localNotification setTimeZone: [NSTimeZone defaultTimeZone]];
[localNotification setRepeatInterval: kCFCalendarUnitDay];

As with all date related code, make sure to test how this works during the switch to daylight savings time, if your time zone uses daylight savings time.

like image 58
Nate Avatar answered Oct 01 '22 03:10

Nate


I guess what you need is NSDayCalendarUnit.

You can check this answer. And here is another tutorial worth reading.

like image 31
Selkie Avatar answered Oct 01 '22 04:10

Selkie


 NSDate *alertTime = [[NSDate date] dateByAddingTimeInterval:10];
    UIApplication* app = [UIApplication sharedApplication];

    UILocalNotification* notifyAlarm = [[[UILocalNotification alloc] init] autorelease];
    if (notifyAlarm)
    {
        notifyAlarm.fireDate = alertTime;
        notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
        notifyAlarm.repeatInterval = 0;
        notifyAlarm.soundName = @"Glass.aiff";
        notifyAlarm.alertBody = @"Staff meeting in 30 minutes";

        [app scheduleLocalNotification:notifyAlarm];
    }
like image 43
Girish Chauhan Avatar answered Oct 01 '22 04:10

Girish Chauhan