极光推送的封装

集成步骤不说了,自己看文档吧:极光推送iOS文档

直接上代码了:

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface JPushManager : NSObject

+(JPushManager *)shareJPushManager;

// 在应用启动的时候调用
- (void)cdm_setupWithOption:(NSDictionary *)launchingOption
                 appKey:(NSString *)appKey
                channel:(NSString *)channel
       apsForProduction:(BOOL)isProduction
  advertisingIdentifier:(NSString *)advertisingId;

// 在appdelegate注册设备处调用
- (void)cdm_registerDeviceToken:(NSData *)deviceToken;

//设置角标
- (void)cdm_setBadge:(int)badge;

//获取注册ID
- (void)cdm_getRegisterIDCallBack:(void(^)(NSString *registerID))completionHandler;

//处理推送信息
- (void)cdm_handleRemoteNotification:(NSDictionary *)remoteInfo;

@end

实现:

//
//  JPushManager.m
//  JPushManager
//
//  Created by Doman on 17/3/31.
//  Copyright ? 2017年 doman. All rights reserved.
//

#import "JPushManager.h"
#import "JPUSHService.h"
// iOS10注册APNs所需头文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif

@interface JPushManager ()<JPUSHRegisterDelegate>

@end

@implementation JPushManager

+ (JPushManager *)shareJPushManager
{
    static JPushManager * JPushTool = nil;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{

        JPushTool = [[JPushManager alloc] init];
    });

    return JPushTool;
}

// 在应用启动的时候调用
- (void)cdm_setupWithOption:(NSDictionary *)launchingOption
                     appKey:(NSString *)appKey
                    channel:(NSString *)channel
           apsForProduction:(BOOL)isProduction
      advertisingIdentifier:(NSString *)advertisingId
{

    JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
        entity.types = UNAuthorizationOptionAlert | UNAuthorizationOptionBadge|UNAuthorizationOptionSound;
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        // 可以添加自定义categories
        // NSSet<UNNotificationCategory *> *categories for iOS10 or later
        // NSSet<UIUserNotificationCategory *> *categories for iOS8 and iOS9
    }

    [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];

    [JPUSHService setupWithOption:launchingOption appKey:appKey channel:channel apsForProduction:isProduction advertisingIdentifier:nil];

    return;
}

// 在appdelegate注册设备处调用
- (void)cdm_registerDeviceToken:(NSData *)deviceToken
{
    [JPUSHService registerDeviceToken:deviceToken];
    return;

}

//设置角标
- (void)cdm_setBadge:(int)badge
{
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:badge];
    [JPUSHService setBadge:badge];
}

//获取注册ID
- (void)cdm_getRegisterIDCallBack:(void(^)(NSString *registerID))completionHandler
{
    [JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
//        if(resCode == 0){
//        NSLog(@"registrationID获取成功:%@",registrationID);

//        }
//        else{
//            NSLog(@"registrationID获取失败,code:%d",resCode);
//        }

        if (resCode == 0) {

            NSLog(@"registrationID获取成功:%@",registrationID);

            completionHandler(registrationID);
        }
    }];

}

//处理推送信息
- (void)cdm_handleRemoteNotification:(NSDictionary *)remoteInfo
{
    [JPUSHService handleRemoteNotification:remoteInfo];
    [self cdm_setBadge:0];

}

#pragma mark JPUSHRegisterDelegate
// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger options))completionHandler
{
    // Required
    NSDictionary * userInfo = notification.request.content.userInfo;
    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [self cdm_handleRemoteNotification:userInfo];
    }
    completionHandler(UNNotificationPresentationOptionAlert); // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以选择设置

}

// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler
{
    // Required
    NSDictionary * userInfo = response.notification.request.content.userInfo;
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [self cdm_handleRemoteNotification:userInfo];
    }
    completionHandler(UNNotificationPresentationOptionAlert);  // 系统要求执行这个方法

}

@end

AppDelegate.h中:

#import <UIKit/UIKit.h>

static NSString *appKey = @"你的appKey";
static NSString *channel = @"APP Store";
static BOOL isProduction = YES;
@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

AppDelegate.m中:

//
//  AppDelegate.m
//  JPushManager
//
//  Created by Doman on 17/3/31.
//  Copyright ? 2017年 doman. All rights reserved.
//

#import "AppDelegate.h"
#import "JPushManager.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    [[JPushManager shareJPushManager] cdm_setupWithOption:launchOptions appKey:appKey channel:channel apsForProduction:isProduction advertisingIdentifier:nil];

    [[JPushManager shareJPushManager] cdm_getRegisterIDCallBack:^(NSString *registerID) {

        NSLog(@"----%@",registerID);
    }];

    [[JPushManager shareJPushManager] cdm_setBadge:0];
    // Override point for customization after application launch.
    return YES;
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    // Required - 注册 DeviceToken
    [[JPushManager shareJPushManager] cdm_registerDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    // Required, iOS 7 Support
    [[JPushManager shareJPushManager] cdm_handleRemoteNotification:userInfo];

    completionHandler(UIBackgroundFetchResultNewData);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

    // Required,For systems with less than or equal to iOS6
    [[JPushManager shareJPushManager] cdm_handleRemoteNotification:userInfo];

}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

Demo地址:https://github.com/domanc/JPushManager.git

时间: 2024-08-04 18:12:07

极光推送的封装的相关文章

Qt通过极光推送向app推送消息

简介 最近在做个项目,当客服端收到防盗的消息通知时向手机app推送一个消息,告知有防盗报警.这么小的功能没必要自己写个推送端,极光推送免费而且推送的成功率高,已经能满足我们的需求了. 极光推送的文档大家可以到极光推送的官网查看(http://docs.jiguang.cn/),由于我们这是是使用Qt C++开发的极光推送并没有提供c++的封装,这里我们选择rest API的方式推送,rest API的推送方式的demo以及json格式在http://docs.jiguang.cn/server/

iOS: 极光推送

之前做过环信和友盟的推送,照着官方文档集成其实挺简单的,今天公司需要,特地做了一下极光推送.不用不知道,原来极光推送集成如此简单,不得不说说了. 当然做推送钱需要做一些准备工作了,就是推送必须的p12推送证书:开发环境(开发时测试需要的推送证书).生产环境(发布到AppStore时需要的推送证书),因为xcode已经升级到了7.0以上,所以一些真机测试的配置文件证书就不需要自己手动去创建了,只要有Apple ID,真机测试时,就能自动生成,免费测试: 制作证书的过程就不啰嗦了,详细看官方文档或者

使用极光推送实现分组发送和服务端集成

推送功能在手机应用开发中越来越重要,几乎成为所有App必备的功能,由于Android本身没有消息推送机制,通常采用的是基于XMPP协议的推送,但这种开发很麻烦,因此在市场上应运而生了提供消息推送服务的诸多产品,例如:百度云.个推.极光等. 极光推送正是一个整合了Android推送.iOS推送的统一推送服务平台.下面讲解一下如何使用极光实现消息推送应用,并重点讲解一下如何实现向分组发送消息及推送服务端和自身应用集成,具体实现过程如下: 目录: 一.注册应用 二.环境搭建 三.Android开发,实

【Android应用开发】 推送原理解析 极光推送使用详解 (零基础精通推送)

作者 : octopus_truth 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/45046283 推送技术产生场景 : -- 服务器端主动性 : 客户端与服务器交互都是客户端主动的, 服务器一般不能主动与客户端进行数据交互, 因为服务器端无法得知客户端的 IP 地址 及 状态; -- 数据实时性 : 如果服务器端有紧急数据要传递给客户端, 就必须主动向客户端发送数据; -- 基本原理 : 使客户端实时获取服务器端消息,

看了极光推送技术原理的几点思考

看了极光推送技术原理的几点思考 分类: android2012-11-26 20:50 16586人阅读 评论(18) 收藏 举报 目录(?)[+] 移动互联网应用现状 因为手机平台本身.电量.网络流量的限制,移动互联网应用在设计上跟传统 PC 上的应用很大不一样,需要根据手机本身的特点,尽量的节省电量和流量,同时又要尽可能的保证数据能及时到达客户端. 为了解决数据同步的问题,在手机平台上,常用的方法有2种.一种是定时去服务器上查询数据,也叫Polling,还有一种手机跟服务器之间维护一个 TC

【android极光推送】—从客户端到后台,一文通吃

前记 推送原理浅析 平台说明 概念解释 推送的三种实现方式 客户端直接向推送服务方发送Http请求 项目服务器通过Http转发推送请求至推送服务方 项目服务端使用SDK进行功能集成 关于推送的种类概述 android客户端初步实现 集成SDK说明 集成步骤 1下载官方提供的SDK集成包 2手动导入SDK 3在极光的官网创建一个应用 4编写一个MyApplication类初始化SDK 5配置 AndroidManifestxml wampServer服务端配置 配置推送SDK 通过composer

Android客户端与PHP服务端通信(四)---极光推送示例工程分析

概述 上一节,描述了注册极光推送并使用其例子的方法,这一节准备研究一下示例工程的框架,为移植它做准备. 分析例程源码 首先分析一下例程的源码结构,建议对照着JPUSH的官方文档(http://docs.jpush.io/)分析,我就是这样做的. 注册应用后,下载的示例工程结构如下, ExampleApplication.java:该类为应用程序定制了一个Application类,因为调用JPush的SDK时,需要调用JPush提供的init()函数API,而按照官方文档的说明"init 只需要在

Ionic项目中使用极光推送-android

对于Ionic项目中使用消息推送服务,Ionic官方提供了ngCordova项目,这个里面的提供了用angularjs封装好的消息推送服务(官方文档),使用的是GitHub上的 PushPlugin 插件,也有相关的实现实例:GitHub地址 ,但是使用的是Google的GCM消息推送服务,一些网络原因,国内GCM可能不怎么好用(自己也没有试可不可以). 于是选择国内的消息推送服务,主要有:百度云推送,腾讯信鸽,极光推送,yunba 等等,其中只有极光推送官方提供了phonegap/cordov

推送原理解析 极光推送使用详解

推送原理解析 极光推送使用详解 原军锋 12016.09.22 18:10:07字数 5,705阅读 19,494 推送技术产生场景: --服务器端主动性: 客户端与服务器交互都是客户端主动的, 服务器一般不能主动与客户端进行数据交互, 因为服务器端无法得知客户端的 IP 地址 及 状态; --数据实时性: 如果服务器端有紧急数据要传递给客户端, 就必须主动向客户端发送数据; --基本原理: 使客户端实时获取服务器端消息, Pull 方式, 小周期轮询, 费电费流量; 另一个就是 Push 方式