iOS自定义推送消息提示框

2020-01-18 15:40:50刘景俊

然后再appDelegate 调用注册远程推送的方法  


/** 注册用户通知 */
- (void)registerUserNotification {
 /*
  注册通知(推送)
  申请App需要接受来自服务商提供推送消息
  */
 // 判读系统版本是否是“iOS 8.0”以上
 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0 ||
  [UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {
  // 定义用户通知类型(Remote.远程 - Badge.标记 Alert.提示 Sound.声音)
  UIUserNotificationType types = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
  // 定义用户通知设置
  UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
  // 注册用户通知 - 根据用户通知设置
  [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
  [[UIApplication sharedApplication] registerForRemoteNotifications];
 } else { // iOS8.0 以前远程推送设置方式
  // 定义远程通知类型(Remote.远程 - Badge.标记 Alert.提示 Sound.声音)
  UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
  // 注册远程通知 -根据远程通知类型
  [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
 }
} 

然后再设置了窗口的跟控制器 之后 调用:addPushView方法 添加 消息提示框STPushView:  addPushView实现代码如下 


#pragma mark 推送信息展示
//添加推送view
- (void)addPushView
{
 STPushView *topView = [STPushView shareInstance];
 topView.frame = CGRectMake(0, -pushViewHeight, SCREEN_WIDTH, pushViewHeight);
 [_window addSubview:topView];
 self.topView = topView;
 topView.hidden = YES;
 
 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hudClick)];
 UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
 [topView addGestureRecognizer:tap];
 [tap requireGestureRecognizerToFail:pan];
 topView.gestureRecognizers = @[tap,pan];
 
}

#pragma mark addPushView相关事件
- (void)hudClick
{
 self.topView.userInteractionEnabled = NO;
 
 [UIView animateWithDuration:0.25 animations:^{
  self.topView.frame = CGRectMake(0, -pushViewHeight, SCREEN_WIDTH, pushViewHeight);
 }completion:^(BOOL finished) {
  [UIApplication sharedApplication].statusBarHidden = NO;
  [self hudClickOperation];
  
 }];
}

- (void)hudClickOperation
{
 [self push:nil];
 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  self.topView.userInteractionEnabled = YES;
 });
}


- (void)pan:(UIPanGestureRecognizer*)pan
{
 CGFloat distance = pushViewHeight-(pushViewHeight-[pan translationInView:self.window].y);
 if (distance<-20) {
  [UIView animateWithDuration:0.25 animations:^{
   self.topView.frame = CGRectMake(0, -pushViewHeight, SCREEN_WIDTH, pushViewHeight);
  }completion:^(BOOL finished) {
   [UIApplication sharedApplication].statusBarHidden = NO;
  }];
 }
}

//显示pushView
- (void)displayPushView
{
 [STPushView show];
}