之前分享过集成友盟推送的方法, 需要的朋友可以查看一下链接:

http://www.cnblogs.com/zhouxihi/p/6533058.html

一般开发中我们比较多使用的三方有友盟推送, 友盟分享, 友盟登录, 微信支付, 支付宝支付, 融云等等...等等...

光集成一个友盟推送就要好几十行代码, 如果多集成几个AppDelegate就会变得臃肿不堪, 也降低了可读性

为了解决这个问题, 目前想到以Category的方式给AppDelegate添加新的类别去完成这些三方集成

先以友盟推送为例

具体方法为先创建一个类别AppDelegate+UMengPush.h

给类别添加一个userInfo属性用来临时存放接收到的推送消息,

@property (nonatomic, strong) NSDictionary *userInfo;

以及一个配置友盟的方法

/**
配置友盟推送 @param appKey 友盟appkey
@param launchOptions App launchOptions
*/
- (void)configureUMessageWithAppKey:(NSString *)appKey launchOptions:(NSDictionary *)launchOptions;

因为类别增加的属性不能直接赋值和取值, 还要再专门增加getter / setter方法

/**
给类别属性赋值 @param userInfo 推送消息字典
*/
- (void)zx_setUserInfo:(NSDictionary *)userInfo; /**
获取类别属性值 @return 暂存的推送消息
*/
- (NSDictionary *)zx_getUserInfo;

实现文件直接给大家看吧, 注释的很清楚

//
// AppDelegate+UMengPush.m
// UMengPushDemo
//
// Created by Jackey on 2017/7/3.
// Copyright © 2017年 com.zhouxi. All rights reserved.
// #import "AppDelegate+UMengPush.h"
#import "UMessage.h" #import <objc/runtime.h> static char UserInfoKey; @implementation AppDelegate (UMengPush) #pragma mark - Configure UMessage SDK - (void)configureUMessageWithAppKey:(NSString *)appKey launchOptions:(NSDictionary *)launchOptions { // 设置AppKey & LaunchOptions
[UMessage startWithAppkey:appKey launchOptions:launchOptions]; // 注册
[UMessage registerForRemoteNotifications]; // 开启Log
[UMessage setLogEnabled:YES]; // 检查是否为iOS 10以上版本
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0) { // 如果检查到时iOS 10以上版本则必须执行以下操作
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
UNAuthorizationOptions types10 = \
UNAuthorizationOptionBadge | UNAuthorizationOptionAlert | UNAuthorizationOptionSound; [center requestAuthorizationWithOptions:types10 completionHandler:^(BOOL granted, NSError * _Nullable error) { if (granted) { // 点击允许
// 这里可以添加一些自己的逻辑
} else { // 点击不允许
// 这里可以添加一些自己的逻辑
}
}]; }
} #pragma mark - UMessage Delegate Methods - (void)application:(UIApplication *)application
didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo { // 关闭友盟自带的弹出框
[UMessage setAutoAlert:NO]; [UMessage didReceiveRemoteNotification:userInfo]; [self zx_setUserInfo:userInfo]; // 定制自己的弹出框
if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示"
message:userInfo[@"aps"][@"alert"]
delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alertView show];
}
} // iOS 10新增: 处理前台收到通知的代理方法
- (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]; }else{ //应用处于前台时的本地推送接受
} //当应用处于前台时提示设置,需要哪个可以设置哪一个
completionHandler(UNNotificationPresentationOptionSound |
UNNotificationPresentationOptionBadge |
UNNotificationPresentationOptionAlert);
} //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]; }else{ //应用处于后台时的本地推送接受
}
} - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { [UMessage sendClickReportForRemoteNotification:[self zx_getUserInfo]];
} - (void)zx_setUserInfo:(NSDictionary *)userInfo { objc_setAssociatedObject(self, &UserInfoKey, userInfo, OBJC_ASSOCIATION_COPY_NONATOMIC);
} - (NSDictionary *)zx_getUserInfo { if (objc_getAssociatedObject(self, &UserInfoKey)) { return objc_getAssociatedObject(self, &UserInfoKey);
} else { return nil;
}
} @end

这样当们有项目需要继承友盟推送的时候, 只要配置好key, 在AppDelegate中只要简单一句话就完成了

#import "AppDelegate.h"
#import "AppDelegate+UMengPush.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch. // 配置UMessage
[self configureUMessageWithAppKey:UMessageAppKey launchOptions:launchOptions]; return YES;
}

附上Demo: https://github.com/zhouxihi/ThirdPartDemo

后面会再陆续完成友盟分享, 友盟登录, 支付宝/微信支付等的内容, 欢迎大家指出不足

如果对大家有帮助, 还请不吝帮忙star

AppDelegate减负之常用三方封装 - 友盟推送篇的更多相关文章

  1. AppDelegate减负之常用三方封装 - 友盟分享 / 三方登录篇

    之前完成了 AppDelegate减负之常用三方封装 - 友盟推送篇: http://www.cnblogs.com/zhouxihi/p/7113511.html 今天接着来完成 - 友盟分享和三方 ...

  2. java 集成友盟推送

    原文:https://blog.csdn.net/Athena072213/article/details/83414743 最近应公司业务需求需要完善友盟推送,认真看了官方文档后其实很简单,只需要细 ...

  3. 友盟推送 .NET (C#) 服务端 SDK rest api 调用库

    友盟推送 .NET SDK rest api 介绍 该版本是基于友盟推送2.3版本封装的,网上查询了下发现没有.NET版本的调用库,官方也没有封装.NET的版本,只有python.java.php版本 ...

  4. iOS集成友盟推送

    之前有写过利用Python自己写一个推送服务器, 今天说下如果集成友盟的推送服务 在这之前我们需要做一些准备动作 #1. 注册一个App ID #2. Enable Push Notification ...

  5. ThinkPHP 提供Auth 权限管理、支付宝、微信支付、阿里oss、友盟推送、融云即时通讯、云通讯短信、Email、Excel、PDF 等等

    多功能 THinkPHP 开源框架 项目简介:使用 THinkPHP 开发项目的过程中把一些常用的功能或者第三方 sdk 整合好,开源供亲们参考,如 Auth 权限管理.支付宝.微信支付.阿里oss. ...

  6. 极光推送和友盟推送,ios端和安卓端的后端调试设置

    我是最后端的,这两天搞了一个app项目,前端安卓使用友盟很方便,调试比较顺利,然后ios就遇到各种问题了,证书.发送成功推送不成功,测试时用的TestMode(),ios上架之后就必须用product ...

  7. 使用极光/友盟推送,APP进程杀死后为什么收不到推送(转)

    为什么会存在这样的 问题,刚开始的时候我也搞不清楚,之前用极光的时候杀死程序后也会收到推送,但最近重新再去集成时就完全不好使了,这我就纳闷了,虽然Google在高版本上的android上面不建议线程守 ...

  8. 友盟推送里面的Alias怎么用?可以理解成账号吗?

    友盟推送里面的Alias怎么用?可以理解成账号吗? 我们的App有自己的账号体系的,想在每次用户登陆的时候,给用户发一个欢迎消息. 看了一下友盟推送,里面有一个概念叫做Alias(别名),但是官方文档 ...

  9. iOS app 集成友盟推送问题

    之前做app推送主要是集成友盟SDK,在程序获取deviceToken时,老是提示如下错误: Error Domain=NSCocoaErrorDomain Code=3000 "未找到应用 ...

随机推荐

  1. Enum in Java

    1. Enum Class public enum ContainerPropertyConstants { RETAILER("retailer"), LINED("i ...

  2. HourRank 19

    https://www.hackerrank.com/contests/hourrank-19/challenges 第一题略. 第二题是nim博弈,问删掉一个区间的石子,使得先手败的方案有几种,明显 ...

  3. Git详细教程(3)---结合gitHub使用

    1.GitHub的基本使用 GitHub就是一个网站,本身是基于Git,可以完成版本控制,可以托管代码. 英文版的. 在使用GitHub之前,首先需要注册一个账号. 登录,就可以完成相关的一些操作. ...

  4. redis3.2.6 集群安装

    下载   [root@localhost ~]# cd /usr/local/src/ [root@localhost src]# wget http://download.redis.io/rele ...

  5. 关于STM32空闲中断

    有一次做一个东西,为了尽量不占用CPU的处理数据时间,所以就使用DMA接收串口的数据,但是呢问题来了.,,,,,怎么样才能确定接收到了一条完整的数据了,,我们都知道只要打开DMA 那家伙就不停的把接收 ...

  6. hive报错 Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. MetaException(message:For direct MetaStore DB connections,

    学习hive 使用mysql作为元数据  hive创建数据库和切换数据库都是可以的 但是创建表就是出问题 百度之后发现 是编码问题 特别记录一下~~~ 1.报错前如图: 2.在mysql数据库中执行如 ...

  7. cpp(第六章)

    1. #include <iostream> #include <limits> int main() { ; ) { std::cout<<"enter ...

  8. python之路第一篇

    一.python环境的搭建 1.window下环境的搭建 (1).在 https://www.python.org/downloads/ 下载自己系统所需要的python版本 (2).安装python ...

  9. 请教 C# 异步 async await 问题

    各位园友,之前对C#异步只是肤浅了解,请教一个具体问题. 需求: 前台会发送一个Array,这个数组都是 id的集合,比较大.分两步,首先保存这些id,然后去调用异步方法. 可以正常返回json,也可 ...

  10. Bash shell执行命令的优先级

    1.别名2.关键字:if.function.while .until等3.函数4.内置命令5.可执行程序或脚本 别关函内可 =-=-=-=-=Powered by Blogilo