入职后的一个任务,就是做远程推送,听老大说用的是友盟Push.所以就看了一下友盟push,具体的集成以及证书的生成请参照这里。具体的就不再多说了,主要是自己重新封装了一下UMessage,具体的内容如下:
// // ZGUmessagePush.h // NotePad // // Created by zhanggui on 15/10/19. // Copyright © 2015年 xiaoguizi. All rights reserved. // #import <Foundation/Foundation.h> #import <CoreLocation/CoreLocation.h> #import "UMessage.h" typedef void(^ResponsData)(id responseObject,NSError *error); @interface ZGUmessagePush : NSObject @property (nonatomic,strong)NSDictionary *receiveDic; + (instancetype)shared; /** *添加友盟推送 */ + (void) startAddUmessageWithOptions:(NSDictionary *)launchOptions; /** *注册设备 */ + (void)registerDeviceWithToken:(NSData *)data; /** *接收信息 */ +(void)didReceiveRemoteNotification:(NSDictionary *)receiveInfoMation; /** *关闭接收消息的通知 */ + (void)closeUmessageNotification; /** *使用友盟提供的默认提示显示 */ + (void)setCustomShow:(BOOL)isShow; /** *自定义处理显示消息,receiveDic是推送过来的内容 * @param receiveDic 指代推送过来的信息 */ +(void)handleReceiveData:(NSDictionary *)receiveDic; /** *是否允许SDK自动清空角标(默认为开启) * @param boo yes清空,no不自动清空 */ + (void)setApplicationBadegeClear:(BOOL)boo; /** *设置是否允许SDK当应用在前台运行时收到Push时弹出Alert框(默认开启) * @param boo yes为允许前台运行时弹出alert,no为不允许 */ + (void)setShowAutoAlert:(BOOL)boo; /** *设置地理位置信息 * @param location:地理信息 */ + (void)setLocation:(CLLocation *)location; /** *添加标签 * @param tagArray 标签数组,用于盛放要添加的标签 */ + (void)addTags:(NSArray *)tagArray; /** 绑定一个别名至设备(含账户,和平台类型) @warning 添加Alias的先决条件是已经成功获取到device_token,否则失败(kUMessageErrorDependsErr) @param alias 账户,例如email @param type 平台类型,参见UMessage文件头部的`kUMessageAliasType...`,例如:kUMessageAliasTypeSina @param data 反馈数据,error为获取失败时的信息,responseObject为成功返回的数据 */ + (void)addAlias:(NSString *)alias type:(NSString *)type response:(ResponsData)data; /** 删除一个设备的别名绑定 @warning 删除Alias的先决条件是已经成功获取到device_token,否则失败(kUMessageErrorDependsErr) @param alias 账户,例如email @param type 平台类型,参见本文件头部的`kUMessageAliasType...`,例如:kUMessageAliasTypeSina @param response block返回数据,error为获取失败时的信息,responseObject为成功返回的数据 */ + (void)deleteAlias:(NSString *)alias type:(NSString *)type response:(ResponsData)data; @end
以上是.h文件。
// // ZGUmessagePush.m // NotePad // // Created by zhanggui on 15/10/19. // Copyright © 2015年 xiaoguizi. All rights reserved. // #import "ZGUmessagePush.h" #import <UIKit/UIKit.h> #import "LoginViewController.h" #import "LeftTableViewController.h" #define _IPHONE80_ 80000 #define APPKEY @"5620da47e0f55a062b003b57" #define UMSYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending) @implementation ZGUmessagePush +(instancetype)shared { static ZGUmessagePush *sharedPush = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedPush = [[ZGUmessagePush alloc] init]; }); return sharedPush; } + (void)startAddUmessageWithOptions:(NSDictionary *)launchOptions { [UMessage startWithAppkey:APPKEY launchOptions:launchOptions]; #if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE80_ if(UMSYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) { //register remoteNotification types (iOS 8.0及其以上版本) UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc] init]; action1.identifier = @"action1_identifier"; [email protected]"Accept"; action1.activationMode = UIUserNotificationActivationModeForeground;//当点击的时候启动程序 UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init]; //第二按钮 action2.identifier = @"action2_identifier"; [email protected]"Reject"; action2.activationMode = UIUserNotificationActivationModeBackground;//当点击的时候不启动程序,在后台处理 action2.authenticationRequired = YES;//需要解锁才能处理,如果action.activationMode = UIUserNotificationActivationModeForeground;则这个属性被忽略; action2.destructive = YES; UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init]; categorys.identifier = @"category1";//这组动作的唯一标示 [categorys setActions:@[action1,action2] forContext:(UIUserNotificationActionContextDefault)]; UIUserNotificationSettings *userSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:[NSSet setWithObject:categorys]]; [UMessage registerRemoteNotificationAndUserNotificationSettings:userSettings]; } else{ //register remoteNotification types (iOS 8.0以下) [UMessage registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound |UIRemoteNotificationTypeAlert]; } #else //register remoteNotification types (iOS 8.0以下) [UMessage registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound |UIRemoteNotificationTypeAlert]; #endif #if DEBUG [UMessage setLogEnabled:YES]; #endif [UMessage setLogEnabled:NO]; } + (void)didReceiveRemoteNotification:(NSDictionary *)receiveInfoMation { [UMessage didReceiveRemoteNotification:receiveInfoMation]; } + (void)registerDeviceWithToken:(NSData *)data { NSString *deveiceToken = [NSString stringWithFormat:@"%@",data]; deveiceToken = [deveiceToken stringByReplacingOccurrencesOfString:@" " withString:@""]; NSLog(@"deveice-token:%@",deveiceToken); [UMessage registerDeviceToken:data]; } + (void)closeUmessageNotification { [UMessage unregisterForRemoteNotifications]; } + (void)setCustomShow:(BOOL)isShow { [UMessage setAutoAlert:isShow]; } //返回的推送信息:aps和d,p是UMessage带过来的,gender是自己添加的 //{ // aps = { // alert = "\U8868\U793a\U5185\U5bb9\U90a3\U4e2a"; // badge = 1; // sound = default; // }; // d = us88294144523914949311; // gender = girl; // p = 0; //} + (void)handleReceiveData:(NSDictionary *)receiveDic { [UMessage setAutoAlert:NO]; //不自动弹出UMessage默认的提示框 if ([UIApplication sharedApplication].applicationIconBadgeNumber!=0) { [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; //设置徽章显示为0,即不显示 } //不在前台才去处理 if ([UIApplication sharedApplication].applicationState!=UIApplicationStateActive) { NSLog(@"UMessage:开始处理程序逻辑处理"); UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"标题" message:[receiveDic objectForKey:@"name"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil]; [alert show]; }else { NSLog(@"UMessage:程序正在前台运行,默认不做任何处理"); } } + (void)addTags:(NSArray *)tagArray { if ([tagArray isKindOfClass:[NSArray class]]) { if ([tagArray count]==0) { NSLog(@"没有添加任何tag"); return; }else { [UMessage addTag:tagArray response:^(id responseObject, NSInteger remain, NSError *error) { if (error) { NSLog(@"添加tag失败"); } }]; } } } + (void)addAlias:(NSString *)alias type:(NSString *)type response :(ResponsData)data { [UMessage addAlias:alias type:type response:data]; } + (void)deleteAlias:(NSString *)alias type:(NSString *)type response:(ResponsData)data { [UMessage removeAlias:alias type:type response:data]; } + (void)setApplicationBadegeClear:(BOOL)boo { [UMessage setBadgeClear:boo]; } + (void)setShowAutoAlert:(BOOL)boo { [UMessage setAutoAlert:boo]; } + (void)setLocation:(CLLocation *)location { [UMessage setLocation:location]; } @end
注意事项:
1、如果是开发环境的话,需要添加deviceToken到友盟推送后台。
2、根据传过来的字段进行不同页面之间的跳转都是在handleReceiveData:这个方法中进行操作的。
附:
类下载地址:http://pan.baidu.com/s/1o6kZbrc
时间: 2024-10-25 17:34:29