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 "未找到应用 ...
随机推荐
- Spark操作HBase问题:java.io.IOException: Non-increasing Bloom keys
1 问题描述 在使用Spark BulkLoad数据到HBase时遇到以下问题: 17/05/19 14:47:26 WARN scheduler.TaskSetManager: Lost task ...
- Handler线程间通信
package com.hixin.appexplorer; import java.util.List; import android.app.Activity; import android.ap ...
- 【Spark2.0源码学习】-6.Client启动
Client作为Endpoint的具体实例,下面我们介绍一下Client启动以及OnStart指令后的额外工作 一.脚本概览 下面是一个举例: /opt/jdk1..0_79/bin/jav ...
- Git简略教程
Git使用教程 厂里大部分后端应用的版本控制工具为SVN,前端代码则更习惯于Git,好久不用Git有些生疏,复习一下,效率就是生命. 1.拉取远程分支到本地 git clone + 代码地址 + 分支 ...
- PHP cURL的详细使用手册
PHP cURL的详细使用手册 PHP cURL可以帮助我们简单有效地去抓取网页内容,帮助我们方便的实现抓取功能.本文主要介绍了PHP cURL的使用方法. AD:2013云计算架构师峰会课程资料下载 ...
- Java集合之Properties
Java集合之Properties
- python中文字符串编码问题
接口测试的时候,发现接口返回内容是uncodie类型但是包含中文.在使用print进行打印时输出提示错误: UnicodeEncodeError: 'ascii' codec can't encode ...
- Vivado简单调试技能
Vivado简单调试技能 1.关于VIO核的使用 首先配置VIO核: 配置输入输出口的数量5,5 配置输入口的位宽 配置输出口位宽和初始值. 例化与使用: vio_0 U1 ( .clk(clk_27 ...
- 用java来实现验证码功能(本帖为转载贴),作为个人学习收藏用
一.关于为何使用验证的解释 在目前的网页的登录.注册中经常会见到各种验证码.其目的便是为了:防止暴力破解 .因为只要CPU性能较强,便可以在慢慢尝试密码的过程中来破解用户账号,因而导致的结果是用户信 ...
- 实现UDP高效接收/响应
环境Linux g++6.3.0 问题一:一个ip地址如何接收高并发请求 问题二:如何高并发响应消息 发送请求端只能通过ip地址+端口号向服务器发送请求码,所以服务器只能用一个UDP去绑定此ip以及端 ...