AppDelegate减负之常用三方封装 - 友盟推送篇
之前分享过集成友盟推送的方法, 需要的朋友可以查看一下链接:
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减负之常用三方封装 - 友盟推送篇的更多相关文章
- AppDelegate减负之常用三方封装 - 友盟分享 / 三方登录篇
之前完成了 AppDelegate减负之常用三方封装 - 友盟推送篇: http://www.cnblogs.com/zhouxihi/p/7113511.html 今天接着来完成 - 友盟分享和三方 ...
- java 集成友盟推送
原文:https://blog.csdn.net/Athena072213/article/details/83414743 最近应公司业务需求需要完善友盟推送,认真看了官方文档后其实很简单,只需要细 ...
- 友盟推送 .NET (C#) 服务端 SDK rest api 调用库
友盟推送 .NET SDK rest api 介绍 该版本是基于友盟推送2.3版本封装的,网上查询了下发现没有.NET版本的调用库,官方也没有封装.NET的版本,只有python.java.php版本 ...
- iOS集成友盟推送
之前有写过利用Python自己写一个推送服务器, 今天说下如果集成友盟的推送服务 在这之前我们需要做一些准备动作 #1. 注册一个App ID #2. Enable Push Notification ...
- ThinkPHP 提供Auth 权限管理、支付宝、微信支付、阿里oss、友盟推送、融云即时通讯、云通讯短信、Email、Excel、PDF 等等
多功能 THinkPHP 开源框架 项目简介:使用 THinkPHP 开发项目的过程中把一些常用的功能或者第三方 sdk 整合好,开源供亲们参考,如 Auth 权限管理.支付宝.微信支付.阿里oss. ...
- 极光推送和友盟推送,ios端和安卓端的后端调试设置
我是最后端的,这两天搞了一个app项目,前端安卓使用友盟很方便,调试比较顺利,然后ios就遇到各种问题了,证书.发送成功推送不成功,测试时用的TestMode(),ios上架之后就必须用product ...
- 使用极光/友盟推送,APP进程杀死后为什么收不到推送(转)
为什么会存在这样的 问题,刚开始的时候我也搞不清楚,之前用极光的时候杀死程序后也会收到推送,但最近重新再去集成时就完全不好使了,这我就纳闷了,虽然Google在高版本上的android上面不建议线程守 ...
- 友盟推送里面的Alias怎么用?可以理解成账号吗?
友盟推送里面的Alias怎么用?可以理解成账号吗? 我们的App有自己的账号体系的,想在每次用户登陆的时候,给用户发一个欢迎消息. 看了一下友盟推送,里面有一个概念叫做Alias(别名),但是官方文档 ...
- iOS app 集成友盟推送问题
之前做app推送主要是集成友盟SDK,在程序获取deviceToken时,老是提示如下错误: Error Domain=NSCocoaErrorDomain Code=3000 "未找到应用 ...
随机推荐
- wget-文件下载工具
阅读目录:详解wget命令-文件下载工具 安装wget 命令格式 命令功能 参数指南 启动参数 记录和输入文件参数 下载参数 目录参数 http选项参数 ftp选项参数 递归下载参数 递归下载中的包含 ...
- Day4-迭代器
for循环的数据类型: 1.集合数据类型,如list列表,tuple元组,dict字典,set集合,str字符串等: 2.generator生成器,包括生成器和带yield的generator fun ...
- Cassandra Issue with Tombstone
1. Cassandra is quicker than postgre and have lower change to lose data. Cassandra doesn't have fore ...
- jsp/html页面中的路径
Html/Jsp页面中的路径,是供浏览器使用的."/"代表的是服务器根目录,一个服务器会有多个web应用,所以请求资源时需要加应用名才能正确访问. 页面中使用的相对路径,也是由浏览 ...
- Json及Json字符串
JSON(JavaScript Object Notation)是一种独立于开发语言的用于存储和交换文本数据的格式,JSON 语法是JavaScript 语法的子集. Json 可以保存数组格式和对象 ...
- Norm 数据库操作竟然可以如此简单
github地址,https://github.com/xcr1234/norm/欢迎各位大神fork&交流! Norm Norm是一套微型的JAVA数据库ORM库,提供了简单高效的 API, ...
- reshape: from long to wide format(转)
This is to continue on the topic of using the melt/cast functions in reshape to convert between long ...
- 玩转Storage Table 的PartitionKey,RowKey设计
参阅的文章 l https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/designing-a-scalable ...
- 网络安全——一图看懂HTTPS建立过程
关于网络安全加密的介绍可以看之前文章: 1. 网络安全--数据的加密与签名,RSA介绍 2. Base64编码.MD5.SHA1-SHA512.HMAC(SHA1-SHA512) 3. When I ...
- 高性能队列Disruptor系列1--传统队列的不足
在前一篇文章Java中的阻塞队列(BlockingQueue)中介绍了Java中的阻塞队列.从性能上我们能得出一个结论:数组优于链表,CAS优于锁.那么有没有一种队列,通过数组的方式实现,而且采用无锁 ...