@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,我就放心了。现象是,如果手机处于解锁状态,发生了本地推送,通知栏不激活通知,但里面还是有内容的。说是远程推送没这个问题,但没实验。