<pre code_snippet_id=
"1707319"
snippet_file_name=
"blog_20160604_1_563489"
name=
"code"
class
=
"objc"
>/<span style=
"font-size:18px;"
>/
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (
BOOL
)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if
([[UIApplication sharedApplication] currentUserNotificationSettings].types != UIUserNotificationTypeNone) {
[self addLocationNotification];
}
else
{
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]];
}
return
YES;
}
#pragma mark ---- 调用过用户注册通知方法之后执行(调用完 registerUserNotificationSettings:之后执行的)
-(
void
)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
if
(notificationSettings.types != UIUserNotificationTypeNone) {
[self addLocationNotification];
}
}
#pragma mark ---- 进入前台后设置消息信息
- (
void
)applicationWillEnterForeground:(UIApplication *)application
{
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
#pragma mark ---- 添加本地通知
- (
void
)addLocationNotification
{
UILocalNotification *notification = [[ UILocalNotification alloc] init];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10.0];
notification.repeatInterval = 2;
//多长时间重复:如果设置为NSCalendarUnitSecond,那么消息不会重复,每秒发送一次通知,iOS系统当然不会容许这样的存在了。
location.repeatInterval =NSCalendarUnitSecond;
notification.alertBody = @
"有好玩的东西, 快来尝试吧"
;
notification.applicationIconBadgeNumber = 1;
notification.alertAction = @
"打开应用"
;
notification.alertLaunchImage = @
"Default"
;
notification.soundName = UILocalNotificationDefaultSoundName;
// location.soundName = UILocalNotificationDefaultSoundName;
// NSString* soundPath = [[NSBundle mainBundle] pathForResource:@"key-01" ofType:@"caf"];
// location.soundName = soundPath;
notification.userInfo = @{@
"ID"
: @1, @
"user"
: @
"xiayan"
};
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
#pragma mark ---- 移除本地通知, 在不需要的此通知时移除通知
- (
void
)removeNotification
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
- (
void
)applicationWillResignActive:(UIApplication *)application {
}
- (
void
)applicationDidEnterBackground:(UIApplication *)application {
}
- (
void
)applicationDidBecomeActive:(UIApplication *)application {
}
- (
void
)applicationWillTerminate:(UIApplication *)application {
}
@end</span></pre>