申明:此为本人学习笔记,若有纰漏错误之处的可留言共同探讨
/*
本地推送思路
1 先注册推送通知 本地推送
2 创建本地通知
3 设置推送内容(alertBody)、推送数量(applicationIconBadgeNumber)、推送时间(fireDate)、推送周期(repeatInterval)、推送声音
4 将推送发给系统,让系统去推送
5 设置用户点击推送后实现的方法
*/
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 注册推送通知
if ([[UIApplication sharedApplication]respondsToSelector:@selector(registerUserNotificationSettings:)])
{
// 本地推送
UIUserNotificationSettings * setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil];
[[UIApplication sharedApplication]registerUserNotificationSettings:setting];
}
// 创建本地通知
UILocalNotification *myLocalNotifaction = [[UILocalNotification alloc]init];
// 推送内容
myLocalNotifaction.alertBody = @"hhg 本地通知 today is 1024";
// 推送数量
myLocalNotifaction.applicationIconBadgeNumber = 1;
// 推送时间
myLocalNotifaction.fireDate = [NSDate dateWithTimeIntervalSinceNow:20];
// 推送周期
myLocalNotifaction.repeatInterval = NSCalendarUnitMinute;
// 推送声音
myLocalNotifaction.soundName = UILocalNotificationDefaultSoundName;
// 将推送发给系统,让系统去推送
[[UIApplication sharedApplication]scheduleLocalNotification:myLocalNotifaction];
/* 网络推送方法
//网络推送
UIRemoteNotificationType type = UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound;
[[UIApplication sharedApplication]registerForRemoteNotificationTypes:type];
if (launchOptions != nil) {
UILocalNotification *local = launchOptions[UIApplicationLaunchOptionsAnnotationKey];
//UIApplicationLaunchOptionsAnnotationKey 本地推送的消息
//UIApplicationLaunchOptionsRemoteNotificationKey 网络推送的消息
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"程序启动时的本地推送" message:local.alertBody delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
[alertView show];
}
//取消网络推送
//[[UIApplication sharedApplication]unregisterForRemoteNotifications];
*/
return YES;
}
// 设置用户点击推送后实现的方法
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
// 点击推送消息
if ([[UIApplication sharedApplication]applicationState]==UIApplicationStateInactive) {
UIAlertView *myAlertView =[ [UIAlertView alloc]initWithTitle:@"本地推送" message:@"感谢你玩这个app" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[myAlertView show];
// 取消推送
[[UIApplication sharedApplication]cancelLocalNotification:notification];
}
}