0

In my application, I need to trigger a fake notification on a particular date with time 12PM and also 6PM. I have created and scheduled local notification in "UILocalNotification" category file.

UILocalNotification+TZT.h file :

#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 file :

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

In Appdelegate class didFinishLaunchingWithOptions method, I have to create and scheduled local notification by calling category class.

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

For example, On 31st July 12PM and 6PM I want show the local notification, So I set static date as a string then convert to NSDate, after I have created NSArray contains NSTimeInterVal values.

12PM as a 43200 seconds and also 6PM as a 64800 seconds scheduled with these time interval values.

But I can't see the Local notification Simulator as well as Device also.

Application deployment target starts from iOS 9.0

So I need to manage local notification for iOS 9.0 and below and iOS 10. and above cases ?

I need to follow iOS-10 instructions also ?

Kindly suggest me.

Nirmalsinh Rathod
  • 5,079
  • 4
  • 26
  • 56
Ram
  • 764
  • 1
  • 7
  • 20

1 Answers1

0

This is not proper way to fire notification. Use NSCalendar for getting Date Components. This is the function that fire Notification for your desired Date and Time.

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

Call above method like this. I use DateComponent for getting date after 2 days. You can pass your 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"];

This is Output for FireDate of Notification. 12 PM & 6 PM notification.

2017-07-31 06:00:00 +0530
2017-07-31 12:00:00 +0530
Harshal Shah
  • 427
  • 3
  • 5
  • Thanks for your response and Okay let me follow your way. is the UILocalNotification can fire all devices even my deployment start from iOS 9.0 ? Otherwise I need integrate local notification for iOS 10.0 on separate way ? – Ram Jul 29 '17 at 11:22
  • The local notification was scheduled like this, {fire date = Saturday, July 29, 2017 at 6:00:00 PM India Standard Time, time zone = Asia/Kolkata (GMT+5:30) offset 19800, repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Saturday, July 29, 2017 at 6:00:00 PM India Standard Time, user info = (null)} But I can't see local notification alert. I have tested on Simulator. we can show local notification only on device ? – Ram Jul 29 '17 at 12:21
  • Local Notification is working in Both Simulator and Device. You should check it in AppDelegate's didReceiveLocalNotification Method. – Harshal Shah Jul 29 '17 at 12:27
  • I called method like [UILocalNotification fireNotificationonDate:[NSDate date] onTime:@"18" withBody:NSLocalizedString(@"title", nil) withBtnTitle:NSLocalizedString(@"Sub title", nil)]; on didFinishLaunchingWithOptions. Is it right way ? because why I'm asking this I can't see the notification alert even the app is on Active State or In Active on simulator. – Ram Jul 29 '17 at 12:40
  • It may help you. https://stackoverflow.com/questions/3588964/iphone-local-notification-in-simulator – Harshal Shah Jul 29 '17 at 12:47
  • Kindly check and update my latest comment (4th one) and also 1st one. – Ram Jul 29 '17 at 12:48
  • https://stackoverflow.com/questions/6047117/how-to-create-local-notifications-in-iphone-app – Harshal Shah Jul 29 '17 at 12:50