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

最近研究苹果原生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 Provider 测试)