简体   繁体   English

在iPad和iPhone上传递本地通知问题

[英]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. 当我测试我的应用是否有通知时,在iphone上每天触发一次,但在ipad上每天触发5次。 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? 为什么似乎可以在iPhone上正确发出通知,但在iPad上却不能发出通知?

If in your iPad OS version > 8.0 then you have to take permission from user. 如果您的iPad OS版本> 8.0,则必须征得用户的许可。 Write below code in "didFinishLaunchingWithOptions" (AppDelegate.m) 在“ didFinishLaunchingWithOptions”(AppDelegate.m)中编写以下代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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