首页 技术 正文
技术 2022年11月13日
0 收藏 972 点赞 5,079 浏览 5537 个字

  更新记录:

    1、2015年10月23日上午10:10分更新,优化了该类,去除了不必要的方法。

——————————————————————————————————————————————————

  入职后的一个任务,就是做远程推送,听老大说用的是友盟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"@interface ZGUmessagePush : NSObject+ (instancetype)shared;/** *设备注册友盟推送 */+ (void)registerUMessageWithOptions:(NSDictionary *)launchOptions;/** *注册设备deviceToken */+ (void)registerDeviceWithToken:(NSData *)data;/** *程序未运行的时候,推送消息的处理 * @param userInfo:推送过来的数据 */+ (void)handleNotRunAppRemoteUserInfo:(NSDictionary *)userInfo;/** *程序运行的时候,推送消息的处理 *@param userInfo:推送过来的数据 */+ (void)handleRunAppRemoteUserInfo:(NSDictionary *)userInfo;/** *默认的绑定用户账号 */+ (void)bandingDefaultCount;/** *解绑用户账号 */+ (void)unBandingDefaultCount;/** 绑定账号 @param account:要绑定的用户名 */+ (void)bandingTheCount:(NSString *)account;/** *解绑用户账号 */+ (void)unBandingTheCount;/** *添加标签 */+ (void)addTags:(NSArray *)tagArray;@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 UFQUmessagePush *sharedPush = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedPush = [[UFQUmessagePush alloc] init]; }); return sharedPush;}//#warning 需要修改为自己的APPKey+ (void)registerUMessageWithOptions:(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"; action1.title=@"Accept"; action1.activationMode = UIUserNotificationActivationModeForeground;//当点击的时候启动程序 UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init]; //第二按钮 action2.identifier = @"action2_identifier"; action2.title=@"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}+ (void)registerDeviceWithToken:(NSData *)data { [UMessage registerDeviceToken:data];#if DEBUG NSString *deveiceToken = [NSString stringWithFormat:@"%@",data]; deveiceToken = [deveiceToken stringByReplacingOccurrencesOfString:@" " withString:@""]; NSLog(@"deveice-token:%@",deveiceToken);#endif}+ (void)handleNotRunAppRemoteUserInfo:(NSDictionary *)userInfo { [UMessage setAutoAlert:NO]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"程序未运行的逻辑处理" message:[userInfo objectForKey:@"name"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil]; [alert show];}+ (void)handleRunAppRemoteUserInfo:(NSDictionary *)userInfo { [UMessage setAutoAlert:NO]; if ([UIApplication sharedApplication].applicationState==UIApplicationStateActive) { //程序在前台时逻辑处理 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"程序在前台的逻辑处理" message:[userInfo objectForKey:@"name"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil]; [alert show]; }else { //程序不在前台时的逻辑处理 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"程序不在前台的逻辑处理" message:[userInfo objectForKey:@"name"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil]; [alert show]; }}+ (void)bandingDefaultCount { [UMessage addAlias:[[NSUserDefaults standardUserDefaults] objectForKey:@"username"] type:UFenQiType response:^(id responseObject, NSError *error) { if (error) { NSLog(@"Fail to banding..."); } }];}+ (void)unBandingTheCount { [UMessage removeAlias:[[NSUserDefaults standardUserDefaults] objectForKey:@"username"] type:UFenQiType response:^(id responseObject, NSError *error) { if (error) { NSLog(@"Fail to banding..."); } }];}+ (void)addTags:(NSArray *)tagArray { if ([tagArray isKindOfClass:[NSArray class]]) { if ([tagArray count]==) { NSLog(@"没有添加任何tag..."); return; }else { [UMessage addTag:tagArray response:^(id responseObject, NSInteger remain, NSError *error) { if (error) { NSLog(@"Add tag fail..."); } }]; } }}@end

  

注意事项:

  1、如果是开发环境的话,需要添加deviceToken到友盟推送后台。

  2、程序通过推送开启的调用handleNotRunAppRemoteUserInfo:方法,程序本身已经开启,只是处于前台或者后台的的调用handleRunAppRemoteUserInfo:方法。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,490
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,905
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,738
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,491
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,129
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,292