本地推送

- (void)viewDidLoad {

[super viewDidLoad];

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

btn.frame = CGRectMake(20, 20, 40, 40);

btn.backgroundColor = [UIColor blackColor];

[self.view addSubview:btn];

[btn addTarget:self action:@selector(ButtonClick) forControlEvents:UIControlEventTouchUpInside];

//创建第一个通知选项

UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc] init];

//设置选项按钮标题

action1.title = @"回复";

//设置选项按钮的标识

action1.identifier = @"a1";

//创建第二个通知选项

UIMutableUserNotificationAction * action2 = [[UIMutableUserNotificationAction alloc] init];

//设置选项按钮标题

action2.title = @"收藏";

//设置选项按钮的标识

action2.identifier = @"a2";

//需要把上面的两个按钮放到下面这个类里面

UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc] init];

//设置标识用来绑定本地通知的

category.identifier = @"category";

//在类中设置行为选项按钮

[category setActions:@[action1,action2] forContext:UIUserNotificationActionContextDefault];

//通知的一些设置

UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:[NSSet setWithObjects:category,nil]];

//本地通知需要注册(请求用户授权)

[[UIApplication sharedApplication] registerUserNotificationSettings:settings];

}

- (void)ButtonClick {

//对于没有设置回调方法的,可以command+L锁屏就可以看到推送消息了,也可以command+shift+H回到桌面也可以看到推送消息

//创建通知

UILocalNotification *loc = [[UILocalNotification alloc] init];

//通知声音

loc.soundName = UILocalNotificationDefaultSoundName;

//通知内容

loc.alertBody = @"it's rainning";

////设置通知给通知添加额外的信息

loc.userInfo = @{@"who":@"self"};

//什么时候发起通知36秒之后

loc.fireDate = [NSDate dateWithTimeIntervalSinceNow:3.0];

//这个category要和之前设置的一致

loc.category = @"category";

//设置角标(在原来的基础上+1)

loc.applicationIconBadgeNumber= [UIApplication sharedApplication].applicationIconBadgeNumber+ 1;

//安排通知

[[UIApplication sharedApplication] scheduleLocalNotification:loc];

//立马执行通知

//[[UIApplication sharedApplication] presentLocalNotificationNow:loc];

//取消某个通知

//[[UIApplication sharedApplication] cancelLocalNotification:loc];

//取消所有通知

//[[UIApplication sharedApplication] cancelAllLocalNotifications];

}


@implementation AppDelegate

//应用程序关闭了点击APP图标打开程序,launchOptions是没有通知消息的;

//点击通知推送的消息,launchOptions里面有推送的消息

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"message" message:[launchOptions description] delegate:nil cancelButtonTitle:nil otherButtonTitles:@"ok",nil];

[alert show];

UILocalNotification *loc = launchOptions[UIApplication LaunchOptionsLocalNotificationKey];

if(loc) {

//处理数据

[UIApplication sharedApplication].applicationIconBadgeNumber = 0;

}

return YES;

}

//收到本地通知调用的方法

//如果程序处于运行状态,不管前台后台(如果点击APP不调用,只有点击推送的消息).才都会调用这个方法:如果程序关闭,这个方法不调用

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"message" message:@"hello" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"ok",nil];

[alert show];

//可以根据返回的数据做响应的处理

NSDictionary *dic = notification.userInfo;

NSLog(@"%@",dic);

//角标置0

[UIApplication sharedApplication].applicationIconBadgeNumber = 0;

}

你可能感兴趣的:(本地推送)