iOS远程推送之友盟Push

  更新记录:

    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]==0) {

            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:方法。

时间: 2024-08-18 19:32:38

iOS远程推送之友盟Push的相关文章

iOS远程推送Push开发教程_IOS

远程推送通知 什么是远程推送通知 顾名思义,就是从远程服务器推送给客户端的通知(需要联网)远程推送服务,又称为APNs(Apple Push Notification Services) 为什么需要远程推送通知 传统获取数据的局限性 只要用户关闭了app,就无法跟app的服务器沟通,无法从服务器上获得最新的数据内容 远程推送通知可以解决以上问题 不管用户打开还是关闭app,只要联网了,都能接收到服务器推送的远程通知 远程推送通知使用须知 所有的苹果设备,在联网状态下,都会与苹果的服务器建立长连接

iOS远程推送原理及实现过程

原文出自[听云技术博客]:http://blog.tingyun.com/web/article/detail/571 推送通知,是现在的应用必不可少的功能.那么在 iOS 中,我们是如何实现远程推送的呢?iOS 的远程推送原理又是什么呢?在做 iOS 远程推送时,我们会遇到各种各样的问题.那么首先让我们准备一些做推送需要的东西.我们需要一个付费的苹果开发者账号(免费的不可以做远程推送),有了开发者账号,我们可以去苹果开发者网站,配置自己所需要的推送的相关证书.然后下载证书,供我们后面使用,详细

IOS远程推送,第一次点击通知,没有提示信息

问题描述 IOS远程推送,第一次点击通知,没有提示信息 当应用在前台或后台的时候,推送一切正常:但是当应用是退出状态的时候,收到推送信息,点击推送信息.这里要说明一下,打开应用的时候,应该有一个alertview弹出框的(当应用在前台或后台的时候都是这样),但是,第一次点击推送消息,应用打开,并没有alertview弹出框,但是,第二次点击通知栏通知的时候,会出现alertview弹出框. 我感觉不应该是这样的,请大神们给我解惑,到底本来就是这样,还是说我有地方没有考虑到... ps:应用是在退

ios远程推送问题

问题描述 app杀掉后,后台显示如下图,这里可以看到已登录用户的证书存在,而且我确认与上传的证书名称相同. 解决方案 如果app杀掉后按照问题描述还是收不到离线推送,请联系官网下方"在线技术支持"或屏幕右下角"联系客服",环信技术人员会协助测试推送证书是否适合环信的推送.

iOS实现远程推送原理及过程_IOS

推送通知,是现在的应用必不可少的功能.那么在 iOS 中,我们是如何实现远程推送的呢?iOS 的远程推送原理又是什么呢?在做 iOS 远程推送时,我们会遇到各种各样的问题.那么首先让我们准备一些做推送需要的东西.我们需要一个付费的苹果开发者账号(免费的不可以做远程推送),有了开发者账号,我们可以去苹果开发者网站,配置自己所需要的推送的相关证书.然后下载证书,供我们后面使用,详细的证书配置过程,我们下面再说. 首先我们要说说iOS推送通知的基本原理: 苹果的推送服务通知是由自己专门的推送服务器AP

iOS开发之(APNS)远程推送实现代码 附证书与真机调试_IOS

远程推送通知 什么是远程推送通知 顾名思义,就是从远程服务器推送给客户端的通知(需要联网)远程推送服务,又称为APNs(ApplePush Notification Services) 为什么需要远程推送通知传统获取数据的局限性只要用户关闭了app,就无法跟app的服务器沟通,无法从服务器上获得最新的数据内容远程推送通知可以解决以上问题不管用户打开还是关闭app,只要联网了,都能接收到服务器推送的远程通知远程推送通知使用须知所有的苹果设备,在联网状态下,都会与苹果的服务器建立长连接什么是长连接只

详解iOS本地推送与远程推送_IOS

一.简介 分为本地推送和远程推送2种.可以在应用没有打开甚至手机锁屏情况下给用户以提示.它们都需要注册,注册后系统会弹出提示框(如下图)提示用户是否同意,如果同意则正常使用:如果用户不同意则下次打开程序也不会弹出该提示框,需要用户到设置里面设置.一共有三种提示类型: UIUserNotificationTypeBadge:应用图标右上角的信息提示 UIUserNotificationTypeSound:播放提示音 UIUserNotificationTypeAlert:提示框 二.本地推送 1

iOS消息远程推送通知_IOS

本文实例为大家分享了iOS消息推送.iOS远程通知代码,供大家参考,具体内容如下 消息推送 /* 要开发测试消息机制的程序,必须用真机测试 推送消息的类型 UIRemoteNotificationTypeNone 不接收推送消息 UIRemoteNotificationTypeBadge 接收图标数字 UIRemoteNotificationTypeSound 接收音频 UIRemoteNotificationTypeAlert 接收消息文字 UIRemoteNotificationTypeNe

ios-iOS远程推送 提示音怎么实现的 自定义提示音和系统提示都是怎么实现的?

问题描述 iOS远程推送 提示音怎么实现的 自定义提示音和系统提示都是怎么实现的? iOS远程推送 提示音怎么实现的 自定义提示音和系统提示都是怎么实现的? 解决方案 http://m.blog.csdn.net/blog/chengyakun11/9324069 这篇是系统提示音,自定义的其实就是播放音频而已,AVFoundation有play和stop方法,极其简单 解决方案二: 你要知道,后台和未启动运行的时候使用的是远程推送,这个时候和客户端代码没有一点用处,服务端那边发的消息是有个字段