简体   繁体   中英

local notifications issue delivering on ipad vs iphone

I have issues on the local notifications for my app. when I test my app for its notifications, on the iphone it is firing once daily but on the ipad 5x time daily. here's my code :

NSDateComponents *comp = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:[NSDate date]];

NSInteger day = [comp day];
NSInteger month = [comp month];
NSInteger year = [comp year];
NSInteger hour = [comp hour];
NSInteger min = [comp minute];
NSInteger sec = [comp second];

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay: day];
[components setMonth: month];
[components setYear: year];
[components setHour: hour];
[components setMinute: min];
[components setSecond: sec];
[calendar setTimeZone: [NSTimeZone defaultTimeZone]];
NSDate *dateToFire = [calendar dateFromComponents:components];

// Schedule the notification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = dateToFire;
int i = arc4random() % 2;
if (i == 1) {
    localNotification.alertBody = @"Collect your FREE coins!";
} else {
    localNotification.alertBody = @"It's time to win";
}
localNotification.alertAction = @"View";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[localNotification setRepeatInterval: kCFCalendarUnitDay];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
NSLog(@"local notif = %@", localNotification);

What is my mistake in this code? why it seems that it correctly gives notification on iPhone but not on the iPad?

If in your iPad OS version > 8.0 then you have to take permission from user. Write below code in "didFinishLaunchingWithOptions" (AppDelegate.m)

if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){
    [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM