您的位置:首页 > 产品设计 > UI/UE

UILocalNotification 本地通知的应用实例

2014-04-24 17:37 246 查看
1.单机按钮触发这个本地通知

- (IBAction)sendnotificationClick:(id)sender {

UILocalNotification *notification = [[UILocalNotificationalloc]init];//初始化一个本地通知
if (notification!=nil) {
NSDate *theTime = [[NSDatealloc]init];//实例一个时间对象
notification.fireDate = [theTimedateByAddingTimeInterval:5];//5秒后执行这个通知
notification.timeZone = [NSTimeZonedefaultTimeZone];//设置时区

notification.soundName =UILocalNotificationDefaultSoundName; //设置提示音
notification.alertAction =@"确定";//提示框按钮;

notification.applicationIconBadgeNumber=1;//设置App有上角的数字;
//发送通知

[[UIApplicationsharedApplication]scheduleLocalNotification:notification];

//下面设置本地通知发送的消息,这个消息可以接受

// NSDictionary* infoDic = [NSDictionary dictionaryWithObject:@"value" forKey:@"key"];

// notification.userInfo = infoDic;

//这我没有做操作所以就
注掉啦!

}

}
以上本地通知就设置好啦,但是发送这个通知我要干什么,,什么事都是有因有果的,所以接下来 就是处理他的结果
2.
- (void)applicationDidBecomeActive:(UIApplication *)application
{

NSLog(@"将要进入后台");

application.applicationIconBadgeNumber -=1;

}

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

//设置这个提示框

UIAlertView
*alert = [[UIAlertViewalloc]
initWithTitle:@"消息"message:notification.alertBodydelegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"确定",nil];
[alertshow];

// NSDictionary* dic = [[NSDictionary alloc]init];

//这里可以接受到本地通知中心发送的消息

// dic = notification.userInfo;

// NSLog(@"user info = %@",[dic objectForKey:@"key"]);

// NSDictionary* dic = [[NSDictionary alloc]init];

// 这里可以接受到本地通知中心发送的消息

// dic = notification.userInfo;

// NSLog(@"user info = %@",[dic objectForKey:@"key"]);

//
图标上的数字减1

application.applicationIconBadgeNumber -=1;
}
3.如果你想单机UIAlertView上的按钮干一些事的的话,你就要实现他的代理

3.1 ZYAppDelegate遵循UIAlertViewDelegate

#import <UIKit/UIKit.h>

@interface
ZYAppDelegate : UIResponder <UIApplicationDelegate,UIAlertViewDelegate>

@property (strong,nonatomic)
UIWindow *window;

@end
在 .m文件添加如下代码:

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
switch (buttonIndex) {
case
1:

NSLog(@"你可以干些别的事
比如跳转页面");
break;
default:
break;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: