0

Dans mon application, je dois déclencher une fausse notification à une date particulière avec l'heure 12h et 18h. J'ai créé et planifié une notification locale dans le fichier de catégorie "UILocalNotification".Comment définir une notification locale pour une date particulière avec l'heure 12h et 18h

UILocalNotification + fichier 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 + fichier 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]; 
    } 

En classe Appdelegate de didFinishLaunchingWithOptions méthode, je dois créer et prévu notification locale en appelant la catégorie de catégorie.

#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)]; 
} 

Par exemple, le 31 Juillet 24:00 et je veux montrer 18 heures la notification locale, donc je mets la date statique comme une chaîne puis convertir en NSDate, après avoir créé NSArray contient des valeurs NSTimeInterVal.

12PM en 43200 secondes et également 6PM en 64800 secondes programmées avec ces valeurs d'intervalle de temps.

Mais je ne peux pas voir le simulateur de notification local ainsi que le périphérique aussi.

cible de déploiement d'application commence à partir de iOS 9.0

donc je dois gérer une notification locale pour iOS 9.0 et iOS ci-dessous et 10. et au-dessus des cas?

Je dois suivre également les instructions iOS-10?

Veuillez me le suggérer.

Répondre

0

Ce n'est pas une façon correcte d'envoyer une notification. Utilisez NSCalendar pour obtenir les composants de date. C'est la fonction que le feu annonce pour votre date et heure désirées.

-(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]; 
} 

Appelez la méthode ci-dessus. J'utilise DateComponent pour obtenir la date après 2 jours. Vous pouvez passer votre date.

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

Ceci est la sortie de FireDate de notification. 12 PM & 6 PM notification.

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

Merci pour votre réponse et d'accord laissez-moi suivre votre chemin. est le UILocalNotification peut déclencher tous les appareils même mon déploiement à partir de iOS 9.0? Sinon, j'ai besoin d'intégrer la notification locale pour iOS 10.0 de manière séparée? – Ram

+0

La notification locale a été planifiée comme ceci, {date du tir = samedi 29 juillet 2017 à 18:00:00 heure normale de l'Inde, fuseau horaire = Asie/Kolkata (GMT + 5: 30) offset 19800 , intervalle de répétition = 0, nombre de répétition = UILocalNotificationInfiniteRepeatCount, date de déclenchement suivante = samedi, 29 juillet 2017 à 18:00 heure normale de l'Inde, informations utilisateur = (null)} Mais je ne peux pas voir l'alerte de notification locale. J'ai testé sur simulateur. nous pouvons afficher une notification locale uniquement sur l'appareil? – Ram

+0

La notification locale fonctionne dans le simulateur et le périphérique.Vous devriez le vérifier dans la méthode didReceiveLocalNotification de AppDelegate. –