ios - iPhone 应用程序中的通知

标签 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"];
       }

最佳答案

您好,请尝试以下代码:-

    [[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"];
       }

关于ios - iPhone 应用程序中的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15268593/

相关文章:

ios - 征求用户的许可使用本地通知

ios - 将 NSArray 属性添加到子类 PFObject

ios - 关闭 SFSafariViewController 上的半透明条

使用 PARSE 平台的 Android 多行通知

android - 如何创建即使在手机重启后也始终运行的通知?

ruby-on-rails-3 - 轨道 3 : Strategy for system notifications

ios - 如何在此扩展中创建一个按钮,在不使用操作按钮的情况下打开主应用程序中的特定 View ?

ios - iOS UIBarButton和应用拒绝

ios - 如何将详细 View Pane 标题设置为当前所选单元格的名称?

iphone - 如何检查 iOS 分发配置文件是否启用了推送通知?