TPNS_iOS 1.1.2

TPNS_iOS 1.1.2

Carl Jahn 维护。



TPNS_iOS 1.1.2

  • Deutsche Telekom AG

TPNS_iOS 是一个简化与电信推送通知服务 (TPNS) 进行设备注册和解注册的库。

安装

TPNS_iOS 支持多种方法将库集成到项目中。

CocoaPods 安装

CocoaPods 是一个 Objective-C 的依赖管理器,它自动简化了使用第三方库(如 AFNetworking)的过程。有关更多信息,请参阅 “入门”指南。您可以使用以下命令安装它

$ gem install cocoapods

Podfile

要使用 CocoaPods 将 TPNS_iOS 集成到您的 Xcode 项目中,请在您的 Podfile 中指定它

pod 'TPNS_iOS', :git => 'https://github.com/dtag-dbu/TPNS_iOS.git'

然后,运行以下命令

$ pod install

使用 Carthage 安装

Carthage 是一个分解依赖管理器,它构建依赖项并提供二进制框架。

您可以使用以下命令使用 Homebrew 安装 Carthage

$ brew update
$ brew install carthage

要使用 Carthage 将 TPNS_iOS 集成到您的 Xcode 项目中,请在您的 Cartfile 中指定它

git "https://github.com/dtag-dbu/TPNS_iOS.git" ~> 1.0

运行 carthage 命令构建框架,并将构建好的 TPNS_iOS.framework 拖入您的 Xcode 项目中。

使用方法

使用TPNS注册设备

要将设备注册到TPNS,首先调用相应的 UIApplication 方法

iOS 9及之前版本

    UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
    UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
    [application registerUserNotificationSettings:mySettings];
    [application registerForRemoteNotifications];

iOS 10及以上版本

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;

UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
[center requestAuthorizationWithOptions:types
                      completionHandler:^(BOOL granted, NSError * _Nullable error) {

                          if (nil == error) {
                              [application registerForRemoteNotifications];
                          }

                      }];

在应用程序收集所有必需信息后,您的 AppDelegates 的 didRegisterForRemoteNotificationsWithDeviceToken 方法将用生成的设备令牌被调用。使用此令牌在TPNS上注册设备

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

    NSArray *params = @[@{@"key" : @"SomeAdditionalID", @"value" : @4711},
                            @{@"key" : @"OtherID", @"value" : @"randomValue"}];

    DTPushNotification *tpns = [DTPushNotification sharedInstance];
    if(tpns.isRegistered) {
    	//Already registered no need to register again
    	return;
    }
    
    [tpns registerWithURL:[NSURL URLWithString:@"TPNS Endpoint"] //The different endpoints are defined in the TPNS_iOS.h file
                   appKey:@"APPKEY"
                pushToken:token
     additionalParameters:params
                  sandbox:YES
               completion:^(NSString * _Nullable deviceID, NSError * _Nullable error) {
                       if (error) {
                           //handle error
                           return;
                       }
                       //save the deviceID
                   }];
}

如果您的应用需要返回的 deviceID,可以调用相关属性

[DTPushNotification sharedInstance].deviceId

要检查您是否已注册,只需调用 registered 方法

[DTPushNotification sharedInstance].isRegistered

注销设备

要注销设备,只需调用以下方法

DTPushNotification *tpns = [DTPushNotification sharedInstance];
if(!tpns.isRegistered) {
    //Not registered yet no need to unregister
    return;
}

[tpns unregisterWithCompletion:^(NSError * _Nullable error) {
        if (error) {
              //handle error
        }
     }];

错误代码

除了标准的 HTTP 错误代码外,在 NSError+TPNS.h 文件中还定义了更多错误代码

typedef NS_ENUM(NSInteger, TPNSErrorCode)
{
    // General
    TPNSErrorCodeDeviceNotRegistered                = -1000,
    TPNSErrorCodeRegistrationIsAlreadyInProgress    = -1001,
    TPNSErrorCodeUnregisterBeforeYouRegisterAgain   = -1002,
};

测试

要测试一切是否正常工作,您可以在终端中运行以下curl命令

curl -X POST -H "Accept: application/json" -H "Accept-Encoding: gzip" -H "Content-Type: application/json" -H "Cache-Control: no-cache"  -d '[{"message" : "Test","handlingLevel" : 1,"applicationKey" : "APPKEY","applicationTypes" : ["IOS_SAND"], "deviceIds" : ["YOUR SAVED DEVICE ID"]}]' "'TPNS Endpoint'/api/pushnotifications"

JSON载荷

[{
	"message": "Test",
	"handlingLevel": 1, //1, 2 or 3, depends on your configuration
	"applicationKey": "APP Key",
	"applicationTypes": ["IOS_SAND"], // 'IOS_SAND' for Sandbox otherwise 'IOS'
	"deviceIds": ["YOUR SAVED DEVICE ID"] // deviceID or null for every device
}]

可用端点