Talk is cheap,show me the code.
- (void)configLocalNotification {
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
NSDate *date = [NSDate date];
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate: date];
NSDate *localeDate = [date dateByAddingTimeInterval: interval];
localeDate = [date dateByAddingTimeInterval:5];
NSLog(@"localeDate : %@",localeDate);
localNotif.fireDate = localeDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertAction = NSLocalizedString(@"View Details", nil);
localNotif.alertBody = @"alert body";
localNotif.repeatInterval = kCFCalendarUnitMinute;
localNotif.soundName = UILocalNotificationDefaultSoundName;
// 自定义通知声音
localNotif.soundName = @"Your ring name";
localNotif.applicationIconBadgeNumber = 0;
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"stringID" forKey:@"id"];
localNotif.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
以上为创建本地通知。
- (void)registerLocalNotificaiton {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
要想接收到通知,需要在-application:didFinishLaunchingWithOptions:中调用以上方法。
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
NSLog(@"notif : %@",notif);
}
用户滑动通知调起App后会走以上方法,App启动后需要做的操作可以在此方法中处理。
如果想要自定义通知声音需要注意以下几点:
- 音频数据格式
- 音频在工程中的存放位置
自定义的通知声音支持4种音频数据格式:
- Linear PCM
- MA4 (IMA/ADPCM)
- µLaw
- aLaw
文件后缀为aiff、wav、caf。
音频文件一定要放在app的mainBundle目录中,否则会播放系统默认的声音。可以在自己工程的Build Phases - Copy Bundle Resources中查看是否有自己想要播放的声音文件,没有加上即可。
网友评论