MANUAL & FAQ

文章详情

使用Ubiix PBX在本地iOS APP中实施推送通知
来源: | 作者:ubiixcn | 发布时间: 2214天前 | 664 次浏览 | 分享到:

本指南将逐步说明如何基于Ubiix VoIP SDK创建本机iOS应用程序,该SDK可以接收Ubiix PBX发送的VoIP推送通知。

1. VoIP通知

官方文档可以在这里找到。其中的一些优点包括:
如果收到VoIP推送时应用程序未运行,应用程序会自动重新启动设备仅在发生VoIP推送时唤醒(以节省电池)VoIP会直接推送到您的应用进行处理,并且无延迟地发送如果收到VoIP推送时应用程序未运行,应用程序会自动重新启动

2.先决条件设置
Apple为我们提供了一个名为PushKit的框架,以支持使用此VoIP推送功能。但是,我们需要配置一些额外的设置才能使其工作。 

3.创建一个App ID
如果您没有应用程序(并因此没有应用程序ID),则需要创建一个。首先,登录您的Apple开发者帐户并访问证书,标识符和配置文件:
接下来,转到标识符 - >应用程序ID,然后单击+按钮。
这里填写的两件重要的事情是:App ID Description和所谓的Bundle (这很可能是com.yourdomain.yourappname):
虽然在上面的截图中没有看到,但我使用UBIIXVoipTest作为Bundle ID。这在下一步中将很重要。

4.生成VoIP推送证书

要生成VoIP推送证书,首先您需要登录:https//developer.apple.com/account/ios/certificate单击左侧“ 证书”部分中的“ 全部”按钮和+按钮:
在下一页中,您需要选择VoIP服务证书:
在此之后,您需要选择您创建此VoIP证书的App ID

接下来,您将看到有关如何创建所谓的CSR(证书签名请求)文件的说明:
一旦你创建了这个文件,你就可以在下一个屏幕上选择它来上传。如果一切顺利,您将获得必须下载的证书:
下载证书后,将其打开,钥匙串访问应用程序将打开。现在您将在“ 我的证书”部分下看到证书

5.将VoIP推送支持添加到UBIIX SIPSample项目中
下载示例项目,解压缩并打开PUSH SIPSample项目。在设置产品名称时要特别小心,因为根据它自动设置分类标识符。我们需要将它设置为与前面步骤中设置的Bundle标识符相同。

6.设置适当的功能
要在应用程序中使用VoIP推送,我们需要打开我们的应用程序的背景模式,并检查几个复选框:

7.确保您选择了以下选项
启用推送通知音频,Airplay和后台提取远程通知

8.添加代码

打开AppDelegate.m并在其顶部添加导入PushKit语句。

接下来,在应用程序函数的didFinishLaunchingWithOptions部分中,确保您注册了以下通知:

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

{
 UIUserNotificationSettings * userNotifiSetting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];[[UIApplication sharedApplication] registerUserNotificationSettings:userNotifiSetting];

 ...

}

我们需要实现它的委托回调函数didRegisterUserNotificationSettings:

- (void)应用程序:(UIApplication *)应用程序didRegisterUserNotificationSettings :( UIUserNotificationSettings *)notificationSettings

{

PKPushRegistry * pushRegistry = [[PKPushRegistry alloc] initWithQueue:nil];

pushRegistry.delegate = self;

pushRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];

}

在此回调中,我们注册了VoIP通知,因为我们知道用户已同意接收通知(因为此函数已被调用)。我们通过声明voipRegistry 对象来启用VoIP通知  。

此时,您将在pushRegistry.delegate = self上得到一个错误; 行说“不能分配类型' AppDelegate ' 的值键入' PKPushRegistryDelegate!'”。

pushRegistry的委托类型是  PKPushRegistryDelegate ,它有三种方法,其中两种是必需的。(didUpdatePushCredentials 和  didReceiveIncomingPushWithPayload)。我们必须定义一个所谓的AppDelegate类的扩展。我们通过在AppDelegate.m文件中的所有当前代码之后添加以下代码来完成此操作:

@interface AppDelegate:UIResponder UbiixEventDelegate,UIAlertViewDelegate,LineViewControllerDelegate,CallManagerDelegate>

- (void)pushRegistry:(PKPushRegistry *)注册表didUpdatePushCredentials:(PKPushCredentials *)凭证forType:(PKPushType)类型

{

NSString * token = [NSString stringWithFormat:@“%@”,credentials.token];

[[NSNotificationCenter defaultCenter] postNotificationName:@“TOKEN”object:token];

NSLog(@“%@”,令牌);

_enablePUSH = YES;

}

- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(PKPushType)type

{

NSLog(@“Payload:%@”,payload.dictionaryPayload);

[[NSNotificationCenter defaultCenter] postNotificationName:@“PAYLOAD”object:payload.dictionaryPayload];

UIUserNotificationType theType = [UIApplication sharedApplication] .currentUserNotificationSettings.types;

if(theType == UIUserNotificationTypeNone){

UIUserNotificationSettings * userNotifySetting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert

类别:无];

[[UIApplication sharedApplication] registerUserNotificationSettings:userNotifySetting];

}

如果(!_ callManager.enableCallKit)

{

//如果未启用Call Kit,请显示本地通知

UILocalNotification * backgroudMsg = [[UILocalNotification alloc] init];

backgroudMsg.alertBody = @“你接到一个新的电话”;

backgroudMsg.soundName = @“ringtone.mp3”;

backgroudMsg.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;

[[UIApplication sharedApplication] presentLocalNotificationNow:backgroudMsg];

}

_enablePUSH = YES;

[loginViewController receivedPushIncomingCall];

};

添加这个扩展后,你会注意到前面提到的错误消失了。

在第一个函数中,我们只输出设备令牌。当我们通过发送VoIP推送通知来测试我们的应用程序时,我们将在下一部分中需要此令牌。

在第二个中,我们对收到的VoIP推送通知“采取行动”。在这个具体的例子中,如果应用程序在后台显示本地通知,或者如果我们在应用程序中显示警报。当令牌失效时使用第三个函数(didInvalidatePushTokenForType)。

我们需要通过向REGISTER消息添加SIP头“ ubiix-push ”来通知Ubiix PBX此客户端已启用推送 。

NSString * bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];

NSString * pushMessage = [[NSString alloc] initWithFormat:@“device-os = ios; device-uid =%@; allow-call-push = true; allow-message-push = true; app-id =%@”, _textToken.text,bundleIdentifier];

[ubiixSDK addSipMessageHeader:-1 methodName:@“REGISTER”msgType:1 headerName:@“ubiix-push”headerValue:pushMessage];

ret = [buiixSDK registerServer:90 retryTimes:0];

如果您想禁用推送,则需要使用取消注册消息来通知Ubiix PBX

// unRegisterServer并禁用推送消息

NSString * bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];

NSString * pushMessage = [[NSString alloc] initWithFormat:@“device-os = ios; device-uid =%@; allow-call-push = false; allow-message-push = false; app-id =%@”, _textToken.text,bundleIdentifier];

[ubiixSDK clearAddedSipMessageHeaders]; [ubiixSDK addSipMessageHeader:-1 methodName:@“REGISTER”msgType:1 headerName:@“ubiix-push”headerValue:pushMessage]; [ubiixSDK unRegisterServer];

当应用程序收到推送APP正在运行时,它应该自动注册到服务器:

- (void)doAutoRegister

{

if([_textUsername.text length]> 1 &&

[_textPassword.text length]> 1 && [_textSIPtotal.text length]> 1 && [_textSIPubiix.text length]> 1 && [_textToken.text length]> 1){ [self onLine];

}

}

9.准备证书文件

我们下载并添加到KeyChain的VoIP证书文件必须转换为不同的文件格式,以便我们能够将其与上面列出的工具和服务一起使用。
首先,您需要在Mac上打开KeyChain应用程序并导出(右键单击并选择导出)证书:

您将获得YOUR_CERT.p12 (例如ubiixgo_voip_push.p12)文件。现在导出证书密钥文件

您将获得一个YOUR_CERT_key.p12(例如ubiixgo_voip_push_key.p12)文件。
现在,导航到您已将该文件导出到的文件夹并执行以下命令:

openssl pkcs12 -clcerts -nokeys -out ubiixgo_voip_push.pem -in ubiixgo_voip_push.p12 
openssl pkcs12 -nocerts -out ubiixgo_voip_push_key.pem -in ubiixgo_voip_push_key.p12 
openssl rsa -in ubiixgo_voip_push_key.pem -out ubiixgo_voip_push_key_nopws.pem

这将生成我们将在PBX服务器中使用的ubiixgo_voip_push.pem和ubiixgo_voip_push_key_nopws.pem文件。

10.休斯顿

休斯敦将允许我们从终端窗口发送推送通知以供测试。

我们下载并添加到KeyChain的VoIP证书文件必须转换为不同的文件格式,以便我们能够将其与上面列出的工具和服务一起使用。
尽管文档说可以使用gem install houston安装它,但使用此命令安装它很可能最终(在某些StackOverflow搜索之后):

sudo gem install -n / usr / local / bin houston

这样你就可以将它安装到你有充分权利的本地bin目录中。
休斯顿安装了另一个工具,可以帮助我们发送如下通知:
使用终端导航到您拥有证书的文件夹:

cat ubiixgo_voip_push.pem ubiixgo_voip_push_key-nopwd.pem> ubiixgo2in1.pem

VoIP Push APP复制设备令牌并按如下所示执行命令:

apn push“”-c ubiixgo2in1.pem -m“测试VoIP通知!”请注意,要将ubiixgo_voip_push.pem ubiixgo_voip_push_key-nopwd.pem更改为您在上述步骤中命名该文件的任何内容,您应该在终端中获得以下输出:

1个推送通知已成功发送

而且,如果手机处于前台,您应该在手机上看到推送消息。

11. Ubiix PBX

现在登录Ubiix PBX管理控制台,选择菜单“ 设置 ”>“ 移动推送 ”。

点击“ 添加新应用程序 ”按钮,你会看到下面的屏幕:

请设置以下项目:

启用 - 选中它启用推送并取消选中禁用推送

Apple和Google都提供生产推送服务器和用于发送推送通知的开发推送服务器。开发生产服务器通常在开发阶段使用。一旦您的应用程序发布,您可以将此设置更改为生产服务器。

应用程序ID - 您在步骤3中创建的ID。请注意,此ID区分大小写。

Apple证书文件和私钥文件。您在步骤9中生成的证书文件。请记住,私钥文件必须没有密码。

点击“ 应用 ”按钮并在PBX中启用推送服务。