在iPhone应用程序中的通知

时间:2013-03-07 10:22:58

标签: ios notifications

我已经阅读了很多通知帖子,但不知何故我在某个地方出错了所以我发布这个问题的原因。我想在我的应用程序上每天早上9点收到通知。我在上午9点正确地得到它,没有任何问题,但困难的是我也在凌晨2点得到相同的通知。我尝试使用以下代码。谁能告诉我哪里出错了。或者是ios6的问题。任何形式的帮助将不胜感激。谢谢。

    NSString *day =@"9:00 AM";
    NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init]autorelease];
    [dateFormat setDateFormat:@"hh:mm a"];
    //NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
    //[dateFormat setTimeZone:gmt];
    NSDate *today=[dateFormat dateFromString:day];
    NSLog(@"string %@ & date %@",day,today);
    Class cls = NSClassFromString(@"UILocalNotification");
    if (cls != nil)
    {
        // delObj.QCouter=delObj.QCouter+1;

        //[[UIApplication sharedApplication] cancelAllLocalNotifications];
        notif = [[cls alloc] init];
        notif.fireDate =today;
        notif.timeZone = [NSTimeZone systemTimeZone];
         NSLog(@"timeZone %@ ",[NSTimeZone systemTimeZone]);
        notif.alertBody = @"You have a new letter ";
        notif.alertAction = NSLocalizedString(@"View", nil);;
        notif.soundName = @"Ding3.wav";
        notif.applicationIconBadgeNumber = 1;
        notif.repeatInterval = NSDayCalendarUnit;
        [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"Status"];
        NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"You have a notifiaction"
                                                                       forKey:kRemindMeNotificationDataKey];
        notif.userInfo = userDict;
        // NSLog(@"userInfo %@",notif.userInfo);
        [[UIApplication sharedApplication] scheduleLocalNotification:notif];
        [notif release];
        [[NSUserDefaults standardUserDefaults] setObject:@"CurrentDay" forKey:@"DayChange"];
       }

2 个答案:

答案 0 :(得分:1)

模拟器还是真实设备?

模拟器有一个已知错误,它会生成单个通知的两个“火”。如果这是正在发生的事情,请在物理设备上尝试并查看是否发生了相同的行为。这很麻烦,但不是你的应用程序的实际问题。 (当然假设它是模拟器!)

请参阅此问题:iOS – UILocalNotification fired twice for same notification

根据“不在模拟器中”进行编辑:

在您安排通知后尝试添加对此的调用,并查看代码的其他部分是否在您不知道的其他预定项目中滑落:

- (void) _debug_logExistingToConsole
{
    if (LOG) NSLog(@"Notifications set is now: \n");
    UIApplication *Ap = [UIApplication sharedApplication];
    NSArray* arr = [Ap scheduledLocalNotifications];
    if (LOG) NSLog(@"%@", arr);
}

答案 1 :(得分:1)

您可以尝试以下代码: -

    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    NSDate *date = [NSDate date];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
    NSDateComponents *components = [gregorian components: NSUIntegerMax fromDate: date];
    [components setHour: 9];
    [components setMinute: 0];
    [components setSecond: 0];

    NSDate *today = [gregorian dateFromComponents: components];
    [gregorian release];

    Class cls = NSClassFromString(@"UILocalNotification");
    if (cls != nil)
    {
        notif = [[cls alloc] init];
        notif.fireDate =today;
        notif.alertBody = @"You have a new letter ";
        notif.alertAction = NSLocalizedString(@"View", nil);;
        notif.soundName = @"Ding3.wav";
        notif.applicationIconBadgeNumber = 1;
        notif.repeatInterval = NSDayCalendarUnit;
        [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"Status"];
        NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"You have a notifiaction"
                                                                       forKey:kRemindMeNotificationDataKey];
        notif.userInfo = userDict;
        // NSLog(@"userInfo %@",notif.userInfo);
        [[UIApplication sharedApplication] scheduleLocalNotification:notif];
        [notif release];
        [[NSUserDefaults standardUserDefaults] setObject:@"CurrentDay" forKey:@"DayChange"];
       }
相关问题