美文网首页
使用苹果原生APNS和后端推送工具Easy APNs Provi

使用苹果原生APNS和后端推送工具Easy APNs Provi

作者: hope20 | 来源:发表于2018-07-06 09:35 被阅读0次

最近研究苹果原生apns,极光推送在此不详细解释了,具体查看极光文档

 极光推送传送门


原生APNS,iOS 代码如下

在AppDelegate 里面加入如下代码

#import "AppDelegate.h"

#ifdef NSFoundationVersionNumber_iOS_9_x_Max

#import

#endif

#define IOS_VERSION  [UIDevice currentDevice].systemVersion.floatValue

#import

@interface AppDelegate ()

@end

@implementation AppDelegate

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

   if(IOS_VERSION>=10.0) {

       UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter];

       [centersetDelegate:self];

       UNAuthorizationOptions type = UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert;

        [centerrequestAuthorizationWithOptions:type completionHandler:^(BOOL granted, NSError * _Nullable error) {

        if(granted) {

            NSLog(@"注册成功");

        }else{

            NSLog(@"注册失败");

        }

      }];

   }elseif(IOS_VERSION>=8.0){

        UIUserNotificationTypenotificationTypes =UIUserNotificationTypeBadge|

        UIUserNotificationTypeSound |

        UIUserNotificationTypeAlert;

        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:notificationTypes categories:nil];

        [applicationregisterUserNotificationSettings:settings];

    }else{//ios8一下

        UIRemoteNotificationTypenotificationTypes =UIRemoteNotificationTypeBadge|

        UIRemoteNotificationTypeSound |

        UIRemoteNotificationTypeAlert;

        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:notificationTypes];

    }

    // 注册获得device Token

    [applicationregisterForRemoteNotifications];

    return YES;

}

// 将得到的deviceToken传给SDK

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken{

    NSString*deviceTokenStr = [[[[deviceTokendescription]

                                  stringByReplacingOccurrencesOfString:@"<" withString:@""]

                                 stringByReplacingOccurrencesOfString:@">" withString:@""]

                                stringByReplacingOccurrencesOfString:@" " withString:@""];

    NSLog(@"deviceTokenStr:\n%@",deviceTokenStr);

//向后太发送获取的token,使用工具测试,发送广播不需要

//后台指定设备token 定向推送

//[ self postToken:deviceTokenStr];

}

// 注册deviceToken失败

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error{

   NSLog(@"error -- %@",error);

}

//在前台

- (void)userNotificationCenter:(UNUserNotificationCenter*)center willPresentNotification:(UNNotification*)notification withCompletionHandler:(void(^)(UNNotificationPresentationOptions))completionHandler{

   // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以设置

    NSLog(@"前台 %@",notification.request.content.userInfo);

    completionHandler(UNNotificationPresentationOptionBadge|

                     UNNotificationPresentationOptionSound|

                     UNNotificationPresentationOptionAlert);

}

    //处理推送过来的数据

- (void)userNotificationCenter:(UNUserNotificationCenter*)center didReceiveNotificationResponse:(UNNotificationResponse*)response withCompletionHandler:(void(^)())completionHandler{

    NSLog(@"后台 %@",response.notification.request.content.userInfo);

    completionHandler();

}

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*_Nonnull)userInfo fetchCompletionHandler:(void(^_Nonnull)(UIBackgroundFetchResult))completionHandler{

  NSLog(@"didReceiveRemoteNotification:%@",userInfo);

    /*

    UIApplicationStateActive 应用程序处于前台

     UIApplicationStateBackground 应用程序在后台,用户从通知中心点击消息将程序从后台调至前台

     UIApplicationStateInactive 用用程序处于关闭状态(不在前台也不在后台),用户通过点击通知中心的消息将客户端从关闭状态调至前台

     */

    //应用程序在前台给一个提示特别消息

    if(application.applicationState==UIApplicationStateActive) {

        //应用程序在前台

    }else{

    //其他两种情况,一种在后台程序没有被杀死,另一种是在程序已经杀死。用户点击推送的消息进入app的情况处理。

}

   completionHandler(UIBackgroundFetchResultNewData);

}

向服务器传token

-(void)postToken:(NSString*)token{

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

    manager.requestSerializer.timeoutInterval = 10;             [managerPOST:@""parameters:@{}progress:^(NSProgress*_NonnulluploadProgress) {

   }success:^(NSURLSessionDataTask*_Nonnulltask,id  _NullableresponseObject) {

        NSError*error =nil;

        id obj = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves|NSJSONReadingAllowFragments|NSJSONReadingMutableContainers error:&error];

        if(error) {

           NSLog(@"%@",obj);

        }

    }failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {

   }];

}

@end

Easy APNs Provider - 推送测试工具

 mac下载地址

界面截图

按照步骤,

1配置token,

2选择测试或生产证书(.cer)格式,这里不需要p12的

3测试证书就选择gateway.sandbox.push.apple.com   生产证书就选择gateway.push.apple.com

4发送推送

相关文章

  • 使用苹果原生APNS和后端推送工具Easy APNs Provi

    最近研究苹果原生apns,极光推送在此不详细解释了,具体查看极光文档极光推送传送门 原生APNS,iOS 代码如下...

  • 开发工具

    Easy APNs Provider 推送工具 JsonExport json生成类 Network Link ...

  • APNS与VoIP

    APNS 一、简述APNS APNS全称是Apple Push Notification service(苹果推送...

  • iOS推送小结

    文章的Demo地址:iOS-Push。Demo中的推送测试可以使用类似 Easy APNs Provider的工具...

  • [iOS]APNs推送机制

    目录 APNs 简介 Apple Push Notification service (APNs),即苹果推送通知...

  • iOS APNs推送机制

    APNs简介Apple Push Notification service(APNs),即苹果推送通知服务。注:这...

  • 极光推送的使用

    苹果的APNS 用户的应用注册了APNS 消息推送功能 用户iOS设备通过SSL长连接到APNS苹果服务器,收到设...

  • iOS集成极光推送

    iOS集成极光推送 一、配置APNs 苹果APNs(英文全称:Apple Push Notification Se...

  • iOS推送证书有效性检查

    1.生成证书请求文件2.生成推送证书3.下载APNS Tool工具4.使用APNS Tool工具验证证书是否有效 ...

  • APNS苹果远程推送

    什么是APNS? 苹果推送通知服务(APNs)是推送通知的网关,iPhone ipad 对于应用程序在后台运行有诸...

网友评论

      本文标题:使用苹果原生APNS和后端推送工具Easy APNs Provi

      本文链接:https://www.haomeiwen.com/subject/mxkvuftx.html