如何在12PM和6PM之间设置仅特定日期的本地通知

时间:2017-07-29 09:37:53

标签: ios objective-c notifications uilocalnotification unusernotificationcenter

在我的申请中,我需要在特定日期触发假通知,时间为12PM和6PM。我已经在" UILocalNotification "中创建并安排了本地通知。类别文件。

UILocalNotification + TZT.h文件:

#import <UIKit/UIKit.h>

@interface UILocalNotification (TZT)

//Create LocalNotification
+ (void)createLocalNotificaitonForDateForDate:(NSDate *)date
                                     withBody:(NSString *)body
                                 withUserInfo:(NSDictionary *)userInfo
                         withTimeInterValInfo:(NSArray *)timeInterValInfo
                                 withBtnTitle:(NSString *)btnTitle;

//Cancel All Local notificaitons
+ (void)cancelAllLocalNotifications;

@end

UILocalNotification + TZT.m文件:

#import "UILocalNotification+TZT.h"

@implementation UILocalNotification (TZT)

    + (void)createLocalNotificaitonForDateForDate:(NSDate *)date
                                         withBody:(NSString *)body
                                     withUserInfo:(NSDictionary *)userInfo
                             withTimeInterValInfo:(NSArray *)timeInterValInfo
                                     withBtnTitle:(NSString *)btnTitle
    {
        UILocalNotification * localNotification = [[UILocalNotification alloc] init];

        localNotification.alertBody = body;
        localNotification.alertAction = btnTitle;
        localNotification.userInfo = userInfo;
        localNotification.timeZone = [NSTimeZone defaultTimeZone];
        localNotification.repeatInterval = 0;
        localNotification.soundName = UILocalNotificationDefaultSoundName;

        for (int i = 0; i < [timeInterValInfo count]; i++) {

            NSLog(@"timeInterValInfo = %ld",(long)[[timeInterValInfo objectAtIndex:i] integerValue]);

            date = [date dateByAddingTimeInterval:(long)[[timeInterValInfo objectAtIndex:i] integerValue]];

            localNotification.fireDate = date;

            [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
        }
        //localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
    }

    + (void)cancelAllLocalNotifications
    {
        [[UIApplication sharedApplication] cancelAllLocalNotifications];
    }

在Appdelegate类 didFinishLaunchingWithOptions 方法中,我必须通过调用类别类来创建和调度本地通知。

#pragma mark - UILocalNotification
- (void)setUpLocalNotification
{
    NSDate * firedDate = [TZTNSDateHandlings getDateMonthYearHyPhenFormat:@"31-07-2017 00:00:00" withHourMinSec:YES];

    NSArray * timeInterValArray = @[@"43200",@"64800"];

    NSDictionary * userInfoDic = @{@"receiverType":@"localNotification"
                                  };

    [UILocalNotification createLocalNotificaitonForDateForDate:firedDate
                                                      withBody:@"Sample local notification"
                                                  withUserInfo:userInfoDic
                                          withTimeInterValInfo:timeInterValArray
                                                  withBtnTitle:NSLocalizedString(@"localPush.btnTitle", nil)];
}

例如,7月31日下午12点和下午6点我想显示本地通知,所以我将静态日期设置为字符串然后转换为NSDate,在我创建NSArray后包含NSTimeInterVal值。

12PM作为43200秒,也是6PM作为64800秒计划的这些时间间隔值。

但我也看不到本地通知模拟器以及设备。

应用程序部署目标从iOS 9.0开始

所以我需要管理iOS 9.0及以下以及iOS 10.及以上版本的本地通知吗?

我还需要遵循iOS-10说明吗?

请建议我。

1 个答案:

答案 0 :(得分:0)

这不是点火通知的正确方法。使用NSCalendar获取日期组件。这是为您所需的日期和时间触发通知的功能。

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps = [NSDateComponents new];
comps.day = 2;
NSDate *twoDaysAfter = [calendar dateByAddingComponents:comps toDate:[NSDate date] options:0];

[self fireNotificationonDate:twoDaysAfter onTime:@"6"];
[self fireNotificationonDate:twoDaysAfter onTime:@"12"];

像这样调用上面的方法。我使用DateComponent在2天后获取日期。你可以通过你的日期。

2017-07-31 06:00:00 +0530
2017-07-31 12:00:00 +0530

这是FireDate of Notification的输出。 12 PM&amp;下午6点通知。

server {
    listen 80;
    server_name example.com www.example.com;

    location / {
        proxy_pass http://0.0.0.0:9000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}