在ios5 中 使用通知机制(notification ) 来显示一个消息(banner 和 alert)

在ios5 中使用通知机制 来显示 一个消息提示,可以是显示 alert 或者 banner的方式。同时显示在通知中心。 

  1. 在 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];
    }


  2. 在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);
    }



  3. 在AppDelegate.m显示消息
    - (void)showAlarm:(NSString *)text {
    	UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alarm" 
                                                            message:text delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
    	[alertView show];
    }



    这个应用的主要功能是 在界面显示5s后发送一个 通知消息。然后在界面显示此消息。
    如果程序正在运行,在程序界面上显示 一个alert 视窗

    如果程序在后台运行(运行后按下home键),在iphone主界面显示


    这个消息的显示方式可以在 setting里面进行设置。
    同时测试不能在模拟器上测试。只能在真机上测试。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值