繁体   English   中英

本地通知不起作用

[英]Local notifications are not working

我正在尝试使我的应用程序可以处理本地通知。 但是我无法理解。 我正在照顾这个问题几天。 我的core database一个实体Favorites (所有我最喜欢的艺术家)和一个实体Artists (艺术家其详细信息)。

我有一个按钮可以设置本地通知。 当我按下按钮时,我将执行以下操作。

-(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");
        }

    }
}

该方法的作用如下。

 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.

现在添加了所有这些(我认为),因为它可以在数组中正确循环,并且始终为我提供正确的日期和日志中的“ ADDED”。

但是现在,当我更换设备时,它的dateTime就是我通常应该接收本地通知的时间。 我什么也没收到!

我也将日期更改为正确的时间提前2小时。 因为当我用示例本地通知进行测试时。 他们这样设置了fireDate。

 NSDate *alertTime = [NSDate date];

这导致在单击按钮后立即发送了本地通知。 但是,当我记录此消息时,我注意到这比实际时间早了2个小时...?

我非常希望任何人都可以帮助我解决这个问题! 提前致谢!

编辑

这就是我得到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;
}

我得到这个日志

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

根据您的评论(提前10秒工作),听起来一切正常。

当您更改设备的日期/时间时,该如何做? 及时转发? 向后? 您要更改时区吗?

您是否还计划使用过去的日期/时间进行通知? 如果您指定的日期是过去(或没有)的日期,则通知将立即发送。

使用UILocalNotification对象的timeZone属性评估触发日期,因此,如果未设置时区,则触发数据被视为GMT时间。 尝试:

notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];

如果您将触发日期设置为09:00,然后移到其他时区,则警报仍会在新时区的09:00触发。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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