UILocalNotification 本地通知的应用实例

来源:互联网 发布:java时间字符串转整形 编辑:程序博客网 时间:2024/05/18 22:52

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) {

       case1:

            NSLog(@"你可以干些别的事 比如跳转页面");

           break;

       default:

           break;

    }

}






0 0
原创粉丝点击