iOS自定义推送消息提示框

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

看到标题你可能会觉得奇怪 推送消息提示框不是系统自己弹出来的吗? 为什么还要自己自定义呢? 

因为项目需求是这样的:最近需要做 远程推送通知 和一个客服系统 包括店铺客服和官方客服两个模块 如果有新的消息推送的时候 如果用户当前不在客服界面的时候  要求无论是在app前台 还是app退到后台 顶部都要弹出系统的那种消息提示框

这样的需求 我们就只能自定义一个在app内 弹出消息提示框  

实现步骤如下: 

1.我们自定义一个view 为 STPushView 推送消息的提示框view 


#import <UIKit/UIKit.h>
#import "STPushModel.h"

@interface STPushView : UIView

/** *推送数据模型 */
@property(nonatomic,strong) STPushModel *model;

+(instancetype)shareInstance;
+ (void)show;
+ (void)hide;

@end 


#import "STPushView.h"
#import "AppDelegate.h"

@interface STPushView()

@property (nonatomic, weak) UIImageView *imageV;
@property (nonatomic,weak ) UILabel *timLabel;
@property (nonatomic,weak ) UILabel *content;

@end

@implementation STPushView

static STPushView *_instance = nil;

+(instancetype)shareInstance
{
 static dispatch_once_t onceToken;
 dispatch_once(&onceToken, ^{
  _instance = [[STPushView alloc] init];
 });
 return _instance;
}

+(instancetype) allocWithZone:(struct _NSZone *)zone
{
 if (!_instance) {
  _instance = [super allocWithZone:zone];
 }
 return _instance ;
}

- (instancetype)initWithFrame:(CGRect)frame
{
 if (self = [super initWithFrame:frame]) {
  
  self.backgroundColor = CUSTOMCOLOR(15, 14, 12);
  
  CGFloat margin = 12;
  UIImageView *imageV = [[UIImageView alloc] init];
  imageV.userInteractionEnabled = NO;
  imageV.image = [UIImage imageNamed:@"logo"];
  imageV.layer.cornerRadius = 5;
  [self addSubview:imageV];
  self.imageV = imageV;
  [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
   make.left.equalTo(self).offset(margin);
   make.centerY.equalTo(self.mas_centerY);
   make.width.mas_equalTo(30);
   make.height.mas_equalTo(30);
  }];
  
  
  UILabel *titleLabel = [[UILabel alloc] init];
  titleLabel.textColor = [UIColor whiteColor];
  titleLabel.font = [UIFont boldSystemFontOfSize:12];
  titleLabel.text = @"121店官方客服";
  [self addSubview:titleLabel];
  [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
   make.left.equalTo(imageV.mas_right).offset(margin);
   make.top.equalTo(self.mas_top).offset(margin);
   make.height.mas_equalTo(16);
  }];
  [titleLabel sizeToFit];
  
  UILabel *timLabel = [[UILabel alloc] init];
  timLabel.font = [UIFont systemFontOfSize:12];
  timLabel.userInteractionEnabled = NO;
  timLabel.textColor = [UIColor whiteColor];
  timLabel.text = @"刚刚";
  timLabel.textColor = [UIColor lightGrayColor];
  [self addSubview:timLabel];
  self.timLabel = timLabel;
  [timLabel mas_makeConstraints:^(MASConstraintMaker *make) {
   make.left.equalTo(titleLabel.mas_right).offset(margin);
   make.top.equalTo(self.mas_top).offset(margin);
   make.width.mas_lessThanOrEqualTo(40);
   make.height.mas_equalTo(16);
  }];
  
  
  UILabel *content = [[UILabel alloc] init];
  content.numberOfLines = 2;
  content.font = [UIFont systemFontOfSize:13];
  content.textColor = [UIColor whiteColor];
  content.userInteractionEnabled = NO;
  [self addSubview:content];
  self.content = content;
  [content mas_makeConstraints:^(MASConstraintMaker *make) {
   make.left.equalTo(imageV.mas_right).offset(margin);
   make.top.equalTo(titleLabel.mas_bottom).offset(-3);
   make.right.equalTo(self.mas_right).offset(-margin);
   make.height.mas_equalTo(35);
  }];
  
  
  UIView *toolbar = [[UIView alloc] init];
  toolbar.backgroundColor = CUSTOMCOLOR(121, 101, 81);
  toolbar.layer.cornerRadius = 3;
  [self addSubview:toolbar];
  [toolbar mas_makeConstraints:^(MASConstraintMaker *make) {
   make.width.mas_equalTo(35);
   make.height.mas_equalTo(6);
   make.centerX.equalTo(self.mas_centerX);
   make.bottom.equalTo(self.mas_bottom).offset(-2);
  }];
  
 }
 return self;
}

- (void)setModel:(STPushModel *)model
{
 _model = model;
 self.timLabel.text = @"刚刚";
 self.content.text = model.content;
}

+ (void)show
{
 
 [UIApplication sharedApplication].statusBarHidden = YES;
 STPushView *pushView = [STPushView shareInstance];
 pushView.hidden = NO;
 
 AppDelegate *app = (AppDelegate*)[UIApplication sharedApplication].delegate;
 [app.window bringSubviewToFront:pushView];

 [UIView animateWithDuration:0.25 animations:^{
  
  pushView.frame = CGRectMake(0, 0, SCREEN_WIDTH, pushViewHeight);
  
  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
   
   [UIView animateWithDuration:0.25 animations:^{
    
    pushView.frame = CGRectMake(0, -pushViewHeight, SCREEN_WIDTH, pushViewHeight);
    
    
   } completion:^(BOOL finished) {
    
    [UIApplication sharedApplication].statusBarHidden = NO;
    pushView.hidden = YES;
    
   }];
   
  });
  
 }];
}

+ (void)hide
{
 STPushView *pushView = [STPushView shareInstance];

 [UIView animateWithDuration:0.25 animations:^{
  
  pushView.frame = CGRectMake(0, -pushViewHeight, SCREEN_WIDTH, pushViewHeight);
  
 } completion:^(BOOL finished) {
  
  [UIApplication sharedApplication].statusBarHidden = NO;
  pushView.hidden = YES;
  
 }];

}

@end