0

私のアプリケーションでは、特定の日付の午後12時および午後6時の偽の通知をトリガーする必要があります。私は "UILocalNotification"カテゴリファイルにローカル通知を作成し、予定しています。12PMと6PMの特定の日付のローカル通知を設定する方法

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時に私はローカル通知を表示したい、だから、私はNSArrayのはNSTimeInterVal値が含まれて作成した後、NSDateに変換、文字列として静的日付を設定します。

12PMとして43200秒、また、これらの時間間隔値でスケジュールされた64800秒として6PMとなります。

しかし、デバイスと同様にローカル通知シミュレータも表示されません。

アプリケーション・デプロイメントのターゲットは、だから私はiOSの9.0以下とiOSのための10のローカル通知を管理し、ケースの上にする必要があるのiOS 9.0

から始まりますか?

私はiOS-10命令にも従う必要がありますか?

私に親切にお勧めします。

答えて

0

これは、通知を行う適切な方法ではありません。日付コンポーネントを取得するには、NSCalendarを使用します。これは、あなたの希望する日時の通知を発する機能です。

-(void)fireNotificationonDate:(NSDate *)dateEnter onTime:(NSString *)strTime 
{ 
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; 
    NSDateComponents *timeComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:dateEnter]; 
    [timeComponents setHour:[strTime integerValue]]; 
    [timeComponents setMinute:00]; 
    [timeComponents setSecond:0]; 
    [timeComponents setYear:[timeComponents year]]; 
    [timeComponents setMonth:[timeComponents month]]; 
    [timeComponents setDay:[timeComponents day]]; 
    NSDate *dtFinal = [calendar dateFromComponents:timeComponents]; 
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"]; 

    NSString *fierDate = [formatter stringFromDate:dtFinal]; 
    NSLog(@"%@",fierDate); 

    UILocalNotification *notification = [[UILocalNotification alloc] init]; 
    notification.fireDate = dtFinal; 
    notification.alertBody = @""; 
    notification.timeZone = [NSTimeZone defaultTimeZone]; 
    notification.soundName = UILocalNotificationDefaultSoundName; 
    notification.applicationIconBadgeNumber = 1; 

    [[UIApplication sharedApplication] scheduleLocalNotification:notification]; 
} 

このような方法をこのように呼び出します。 2日後にDateComponentを使用して日付を取得します。あなたはあなたの日付を渡すことができます。

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

これは、FireDate for Notificationの出力です。 12 PM & 6 PM通知。

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

ご回答いただきありがとうございます。よろしくお願いします。 は、iOS 9.0からの展開を開始した場合でも、すべてのデバイスをUILocalNotificationで起動できますか?それ以外の場合は、別の方法でiOS 10.0のローカル通知を統合する必要がありますか? – Ram

+0

ローカル通知は、 {fire date = 2017年7月29日土曜日午後6時00分(インド標準時、タイムゾーン=アジア/カルカッタ(GMT + 5:30)オフセット19800 、繰り返し間隔= 0、繰り返し回数= UILocalNotificationInfiniteRepeatCount、次の発砲日= 2017年7月29日土曜日午後6時00分(インド標準時、ユ​​ーザー情報=(ヌル)) ローカル通知のアラートは表示されません。私はシミュレータでテストしました。 デバイスでのみローカル通知を表示できますか? – Ram

+0

ローカル通知は、シミュレータとデバイスの両方で動作しています。AppDelegateのdidReceiveLocalNotificationメソッドでチェックする必要があります。 –

関連する問題