iOS 推送以及小红点

思路:

1.设置友盟推送相关内容

// 友盟推送相关内容设置

- (void)setUMengPushMessageWithLaunchOptions:(NSDictionary *)launchOptions {

[UMessage startWithAppkey:U_MENG_KEY launchOptions:launchOptions];

// 打开日志,方便调试

[UMessage setLogEnabled:YES];

//注册通知,如果要使用category的自定义策略,可以参考demo中的代码。

[UMessage registerForRemoteNotifications];

//iOS10必须加下面这段代码

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

center.delegate=self;

UNAuthorizationOptions types10=UNAuthorizationOptionBadge|  UNAuthorizationOptionAlert|UNAuthorizationOptionSound;

[center requestAuthorizationWithOptions:types10    completionHandler:^(BOOL granted, NSError * _Nullable error) {

if (granted) {

//点击允许

//这里可以添加一些自己的逻辑

} else {

//点击不允许

//这里可以添加一些自己的逻辑

}

}];

}

2.获取devicetoken方法

//app 允许接受远程通知以后 利用该回调 回取token

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

NSString *deviceTokenString = [[[[deviceToken description]

stringByReplacingOccurrencesOfString:@"<" withString:@""]

stringByReplacingOccurrencesOfString:@">" withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@""];

if (![AppDelegate APP].deviceToken) {

[AppDelegate APP].deviceToken = deviceTokenString;

[[NSNotificationCenter defaultCenter]postNotificationName:@"LOADSHOP" object:nil];

}

[AppDelegate APP].deviceToken = deviceTokenString;

CLog(@"本地存储deviceToken = %@", deviceTokenString);

}

//app 允许接受远程通知以后token获取失败

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {

NSLog(@"DeviceToken 获取失败,原因:%@",error);

}

3.获取推送数据的方法,userInfo

//iOS10以下使用这个方法接收通知

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

[UMessage setAutoAlert:NO];

[UMessage didReceiveRemoteNotification:userInfo];

// 自定义提示框

if([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {

[self showAlertWithUserInfo:userInfo];

} else {

[self pushToPageWithUserInfo:userInfo];

}

}

//iOS10新增:处理前台收到通知的代理方法

-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{

NSDictionary * userInfo = notification.request.content.userInfo;

if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {

//应用处于前台时的远程推送接受

//关闭友盟自带的弹出框

[UMessage setAutoAlert:NO];

[UMessage didReceiveRemoteNotification:userInfo];

[self showAlertWithUserInfo:userInfo];

}else{

//应用处于前台时的本地推送接受

}

}

//iOS10新增:处理后台点击通知的代理方法

-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {

NSDictionary * userInfo = response.notification.request.content.userInfo;

if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {

//应用处于后台时的远程推送接受

//必须加这句代码

[UMessage didReceiveRemoteNotification:userInfo];

[self pushToPageWithUserInfo:userInfo];

}else{

//应用处于后台时的本地推送接受

}

}

4.自定义提示框

// 自定义提示框

- (void)showAlertWithUserInfo:(NSDictionary *)userInfo {

JYAlert *alert = [JYAlert alertWithTitle:nil andMessage:userInfo[@"text"]];

[alert addCommonButtonWithTitle:@"确定" handler:^(JYAlertItem *item) {

[self pushToPageWithUserInfo:userInfo];

}];

[alert addCancleButtonWithTitle:@"取消" handler:^(JYAlertItem *item) {

}];

[alert show];

}

5.页面跳转

// 收到推送信息的页面跳转

- (void)pushToPageWithUserInfo:(NSDictionary *)userInfo {

// 保存推送的最新一条信息

[M_USER_DEFAULTS setObject:userInfo forKey:@"pushMessage"];

if ([[userInfo objectForKey:@"pushAppMessageType"] isEqualToString:@"logistics"]) {

[M_USER_DEFAULTS setBool:YES forKey:@"tradeBadge"];

} else if ([[userInfo objectForKey:@"pushAppMessageType"] isEqualToString:@"advert"]) {

//        [M_USER_DEFAULTS setBool:YES forKey:@""];

//        [M_USER_DEFAULTS synchronize];

} else if ([[userInfo objectForKey:@"pushAppMessageType"] isEqualToString:@"order"]) {

//        [M_USER_DEFAULTS setBool:YES forKey:@""];

//        [M_USER_DEFAULTS synchronize];

} else if ([[userInfo objectForKey:@"pushAppMessageType"] isEqualToString:@"system"]){

//            [M_USER_DEFAULTS setBool:YES forKey:@""];

//            [M_USER_DEFAULTS synchronize];

}

// 发送通知

[[NSNotificationCenter defaultCenter] postNotificationName:@"pushMessage" object:[userInfo objectForKey:@"pushAppMessageType"]];

// 跳转到消息列表页面

MessageViewController *messageVc =  [[MessageViewController alloc] initWithNibName:NSStringFromClass([MessageViewController class]) bundle:nil];

UINavigationController *messageNav = [[UINavigationController alloc ]initWithRootViewController:messageVc];

[self.window.rootViewController presentViewController:messageNav animated:YES completion:^{

}];

}

6.消息页面,通知方法

(当APP在前台时,推送是不会走通知的方法,所以将接收到通知时,写在userDefault里保存,然后修改)

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushAction:) name:@"pushMessage" object:nil];

- (void)pushAction:(NSNotification *)notification {

[self getPushMessageWithUserInfo:[M_USER_DEFAULTS objectForKey:@"pushMessage"]];

if ([notification.object isEqualToString:@"logistics"]) {

[_tradeImageView setTradeMessageWithState:[M_USER_DEFAULTS boolForKey:@"tradeBadge"]];

} else {

}

}

7.接收到推送时,小红点处理

@interface UIView (RedDot)

@property (nonatomic, strong) UIView *badge;

- (void)setTradeMessageWithState:(BOOL)state;

- (void)setActivityMessageWithState:(BOOL)state;

- (void)setNoticeMessageWithState:(BOOL)state;

- (void)setMessageList;

@end

8.小红点,方法实现 

思路是:

1.用存本地的状态BOOL值来判断,如果为YES,就显示红点;NO,就不显示

2.只要在消息列表页面的BOOL值有一个为YES,个人中心的小红点就显示 

注意:防止反复创建,需要加一个判断if badge == nil

#import "UIView+RedDot.h"

#import#import "Constant.h"

static NSString * const badgeViewKey;

static NSInteger const pointWidth = 10; //小红点的宽高

@implementation UIView (RedDot)

- (void)showBadgeWithWidth:(CGFloat)width {

self.badge = [[UIView alloc] init];

self.badge.frame = CGRectMake(width, -pointWidth / 2, pointWidth, pointWidth);

self.badge.backgroundColor = [UIColor redColor];

//圆角为宽度的一半

self.badge.layer.cornerRadius = pointWidth / 2;

//确保可以有圆角

self.badge.layer.masksToBounds = YES;

[self addSubview:self.badge];

[self bringSubviewToFront:self.badge];

}

- (void)hidenBadge {

//从父视图上面移除

[self.badge removeFromSuperview];

}

#pragma mark - GetterAndSetter

- (UIView *)badge {

//通过runtime创建一个UILabel的属性

return objc_getAssociatedObject(self, &badgeViewKey);

}

- (void)setBadge:(UIView *)view {

objc_setAssociatedObject(self, &badgeViewKey, view, OBJC_ASSOCIATION_RETAIN);

}

- (void)setTradeMessageWithState:(BOOL)state {

if (state == YES) {

if (self.badge == nil) {

[self showBadgeWithWidth:55];

}

} else {

[self hidenBadge];

}

}

- (void)setActivityMessageWithState:(BOOL)state {

if (state == YES) {

if (self.badge == nil) {

[self showBadgeWithWidth:55];

}

} else {

[self hidenBadge];

}

}

- (void)setNoticeMessageWithState:(BOOL)state {

if (state == YES) {

if (self.badge == nil) {

[self showBadgeWithWidth:55];

}

} else {

[self hidenBadge];

}

}

- (void)setMessageList {

if ([M_USER_DEFAULTS boolForKey:@"tradeBadge"] || [M_USER_DEFAULTS boolForKey:@"noticeBadge"] || [M_USER_DEFAULTS boolForKey:@"activityBadge"]) {

if (self.badge == nil) {

[self showBadgeWithWidth:18];

}

} else {

[self hidenBadge];

}

}

你可能感兴趣的:(iOS 推送以及小红点)