[oc实战练习三]本地推送练习

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end


@implementation AppDelegate

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

    // Override point for customization after application launch.

    float sysVersion=[[UIDevice currentDevice]systemVersion].floatValue;

    if (sysVersion>=8.0) {

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

        action1.identifier = @"action1";

        action1.title = @"接受";

        action1.activationMode = UIUserNotificationActivationModeForeground;

        

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

        action2.identifier = @"action2";

        action2.title = @"拒绝";

        action2.activationMode = UIUserNotificationActivationModeBackground;

        action2.authenticationRequired= YES;

        action2.destructive = YES;

        

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

        categorys.identifier = @"pushTest";

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

             

        UIUserNotificationType type=

        UIUserNotificationTypeBadge |

        UIUserNotificationTypeAlert |

        UIUserNotificationTypeSound;

        UIUserNotificationSettings *setting=[UIUserNotificationSettings settingsForTypes:type categories:[NSSet setWithObject:categorys]];

        [[UIApplication sharedApplication]registerUserNotificationSettings:setting];

    }

    return YES;

}


#import "ViewController.h"

@interface ViewController ()

@end


@implementation ViewController

{

    NSTimer* _timer1;

    NSInteger _Times;

    UIButton* btn1;

}


-(void)timer1Action

{

    if (!btn1.enabled) {

        if (_Times > 0) {

            [btn1 setTitle:[NSString stringWithFormat:@"%ld",_Times] forState:UIControlStateNormal];

            _Times--;

        }

        else{

           [btn1 setEnabled:YES];

           [btn1 setTitle:@"发送推送" forState:UIControlStateNormal];

            _Times = 5;

        }

    }

}


-(void)btn1Click:(UIButton*)Sender

{

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

    notifation.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];

    notifation.timeZone = [NSTimeZone defaultTimeZone];

    notifation.alertBody = @"您有1单据待审批";

    notifation.alertTitle = @"审批";

    notifation.category = @"pushTest";

    notifation.applicationIconBadgeNumber = 1;

    notifation.soundName = UILocalNotificationDefaultSoundName;

    [[UIApplication sharedApplication] scheduleLocalNotification:notifation];

    Sender.enabled = false;

    [_timer1 setFireDate:[NSDate date]];

    

}

- (void)viewDidLoad {

    [super viewDidLoad];

    _Times = 5;

    btn1 = [UIButton buttonWithType:UIButtonTypeCustom];

    [btn1 setFrame:CGRectMake(20, 64, 80, 40)];

    [btn1 setBackgroundColor:[UIColor brownColor]];

    [btn1 setTitle:@"发送推送" forState:UIControlStateNormal];

    [btn1 addTarget:self action:@selector(btn1Click:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn1];

    

    _timer1 = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timer1Action) userInfo:nil repeats:YES];

    [_timer1 setFireDate:[NSDate distantFuture]];

    

    // Do any additional setup after loading the view, typically from a nib.

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


1,没有处理推送。

2,发现个缺点,网上找了半天,说是苹果的bug,我就放心了。现象是,如果手机处于解锁状态,发生了本地推送,通知栏不激活通知,但里面还是有内容的。说是远程推送没这个问题,但没实验。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值