简体   繁体   中英

Local notifications are not working

I am trying to get my app working with local notifications. But I can't get my head arround. I'm looking after the problem for days. I have in my core database an entity Favorites (all my favorite artists) and an entity Artists (artist his detail information.

I have a button to set the local notification on. When I press the button, I do the following.

-(void)addLocalNotifications{
    GenkonStageDataModel *model = [[GenkonStageDataModel alloc]init];
    NSMutableArray *allFavorites = [model getAllFavorites];
    NSLog(@"allFavoriets count is %d",allFavorites.count);
    UIApplication* app = [UIApplication sharedApplication];
    for (int i = 0; i<allFavorites.count; i++){
        Favorites *favorite = [allFavorites objectAtIndex:i];
        int artId = [favorite.fav_art_id intValue];
        Artists *artist = [model getArtistById:artId];
        UILocalNotification* notifyAlarm = [[UILocalNotification alloc]
                                            init];
        if (notifyAlarm)
        {
            NSDate *datePush = [self getDateForArtist:artist];
            NSLog(@"Push notification should send on: %@",datePush);
            notifyAlarm.fireDate = datePush;
            NSDictionary *dicNotification = [[NSDictionary alloc]initWithObjectsAndKeys:artist.art_id,@"pushKey", nil];
            notifyAlarm.userInfo = dicNotification;
            notifyAlarm.repeatInterval = 0;
            notifyAlarm.soundName = @"Glass.aiff";
            notifyAlarm.alertBody = [NSString stringWithFormat:@"%@ starts in 15 minutes",artist.art_name];
            [app scheduleLocalNotification:notifyAlarm];
            NSLog(@"Added");
        }

    }
}

What this method does is the following.

 1. Get all the favorites
 2. Loop through the favorites and get the artists linked with that favorite by ID
 3. Get the date for when the local notification should be sent
 4. Set in the userInfo dictionary the artist ID (I do this for deleting the local notification when I want to)
 5. Shedule the local notification.

Now all these are added (I think) because it loops correctly through the array and also always gives me the correct date and "ADDED" in my log.

But now when I change my device it's dateTime to the time that I normally should receive the local notification. I do not receive anything!!!!

I also changed my date to 2hours earlier for the correct time. Because when I was testing with an example local notification. They setted the fireDate like this.

 NSDate *alertTime = [NSDate date];

This caused that the local notifcation was sent immedialty after that I clicked the button. But when I logged this I noticed that is was 2 hours before the actual time... ?

I seriously hope that anybody can help me with this problem! Thanks in advance!

EDIT

This is how I get my fireDate

-(NSDate *)getDateForArtist:(Artists *)artist{
    int day = [artist.art_day intValue];
    int timeStart = [artist.art_timestart intValue];
    NSString *timeStart2 = [self getTimeStamp:artist.art_timestart];
    int hours = [[timeStart2 substringWithRange:NSMakeRange(0,2)]intValue];
    int minutes = [[timeStart2 substringWithRange:NSMakeRange(2,2)]intValue];
    int day2;

    if(timeStart >=2400 ){
        day = day++;
    }
    if(day == 1){
        NSLog(@"day is 28");
        day2 = 28;
    }else if (day == 2){
        NSLog(@"day is 29");
        day2 = 29;
    }else{
        NSLog(@"day is 30");
        day2 = 30;
    }
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setDay:day2];
    [comps setMonth:06];
    [comps setYear:2013];
    [comps setHour:hours];
    [comps setMinute:minutes];
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *date = [gregorian dateFromComponents:comps];
    NSLog(@"date is %@",date);


    NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
    [offsetComponents setMinute:-15];

    NSDate *date2 = [gregorian dateByAddingComponents:offsetComponents toDate:date options:0];
    NSLog(@"date -15 min %@", date2);
    return date2;
}

And I get this LOG

2013-06-30 14:37:13.910 genkonstage[1633:907] date is 2013-06-30 16:50:00 +0000
2013-06-30 14:37:13.913 genkonstage[1633:907] Push notification should send on: 2013-06-30 16:35:00 +0000

From your comment (works 10 seconds ahead) it sounds like it's working fine.

When you change the device's date/time how are you doing it? forwards in time? backwards? are you altering the timezone?

Also are you scheduling notifications with a date/time that is in the past? If you specify a date that is in the past (or nil) the notification is delivered immediately.

The fire date is evaluated using the timeZone property of the UILocalNotification object, so if you do not set the timezone the fire data is considered to be GMT time. try:

notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];

If you set a fire date of 09:00 and then move to a different time zone the alarm will still fire at 09:00 in the new time zone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM