在ios5 中 使用通知机制(notification ) 来显示一个消息(banner 和 alert)
2012-05-30 20:41
447 查看
在ios5 中使用通知机制 来显示 一个消息提示,可以是显示 alert 或者 banner的方式。同时显示在通知中心。
在 ViewController.m中加入通知触发机制
在AppDelegate.m中注册消息接受机制
在AppDelegate.m显示消息
这个应用的主要功能是 在界面显示5s后发送一个 通知消息。然后在界面显示此消息。
如果程序正在运行,在程序界面上显示 一个alert 视窗
![](http://agilewarrior.files.wordpress.com/2012/05/ios-alert.png?w=156&h=300)
如果程序在后台运行(运行后按下home键),在iphone主界面显示
![](http://agilewarrior.files.wordpress.com/2012/05/ios-banner-notification2.png?w=156&h=300)
这个消息的显示方式可以在 setting里面进行设置。
同时测试不能在模拟器上测试。只能在真机上测试。
在 ViewController.m中加入通知触发机制
- (void)viewWillAppear:(BOOL)animated { [self setupLocalNotifications]; } - (void)setupLocalNotifications { [[UIApplication sharedApplication] cancelAllLocalNotifications]; UILocalNotification *localNotification = [[UILocalNotification alloc] init]; // current time plus 10 secs NSDate *now = [NSDate date]; NSDate *dateToFire = [now dateByAddingTimeInterval:5]; NSLog(@"now time: %@", now); NSLog(@"fire time: %@", dateToFire); localNotification.fireDate = dateToFire; localNotification.alertBody = @"Time to get up!"; localNotification.soundName = UILocalNotificationDefaultSoundName; localNotification.applicationIconBadgeNumber = 1; // increment NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:@"Object 1", @"Key 1", @"Object 2", @"Key 2", nil]; localNotification.userInfo = infoDict; [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; }
在AppDelegate.m中注册消息接受机制
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; if (notification) { [self showAlarm:notification.alertBody]; NSLog(@"AppDelegate didFinishLaunchingWithOptions"); application.applicationIconBadgeNumber = 0; } [self.window makeKeyAndVisible]; return YES; } - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { [self showAlarm:notification.alertBody]; application.applicationIconBadgeNumber = 0; NSLog(@"AppDelegate didReceiveLocalNotification %@", notification.userInfo); }
在AppDelegate.m显示消息
- (void)showAlarm:(NSString *)text { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alarm" message:text delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alertView show]; }
这个应用的主要功能是 在界面显示5s后发送一个 通知消息。然后在界面显示此消息。
如果程序正在运行,在程序界面上显示 一个alert 视窗
![](http://agilewarrior.files.wordpress.com/2012/05/ios-alert.png?w=156&h=300)
如果程序在后台运行(运行后按下home键),在iphone主界面显示
![](http://agilewarrior.files.wordpress.com/2012/05/ios-banner-notification2.png?w=156&h=300)
这个消息的显示方式可以在 setting里面进行设置。
同时测试不能在模拟器上测试。只能在真机上测试。
相关文章推荐
- 【Android开发】消息提示框与对话框-使用Notification在状态栏上显示通知
- Android--使用Notification在通知栏显示消息
- 通知栏Notification使用自定义视图方法(显示一个进度条ProgressBar)
- 使用 Notification 在状态栏中显示一个通知。
- 通知栏Notification使用自定义视图方法(显示一个进度条ProgressBar)
- 一个windows消息机制使用
- 一个windows消息机制使用
- 消息推送(使用notification来更新显示)
- Broadcast使用以及在通知栏显示消息
- Android开发IM中的消息Notification的一个小小使用.
- 使用通知机制,在dismissViewControllerAnimated后,completion传值给上一个父视图方法
- 使用NotificationListenerService监听通知栏消息
- 关于在用异步消息处理机制使用Message.Obtain()方法(而非New Message)获得一个Message对象的好处
- 通知栏发送消息Notification(可以使用自定义的布局)
- [新手上路] [原创]使用ASP.NET MVC 4 Async Action + jQuery 实现消息通知机制
- 消息提示类控件使用之Notification(状 态栏通知)详解
- 使用ASP.NET MVC 4 Async Action+jQuery实现消息通知机制的实现代码
- 使用Notification发送消息通知
- android使用NotificationListenerService监听通知栏消息
- 使用通知机制,在dismissViewControllerAnimated后,completion传值给上一个父视图方法