推送通知
推送通知
iOS版亚博Zendesk SDK使用Apple Push Notification service (apn)进行推送通知。以下步骤将指导您或您的开发团队在应用程序中为Zendesk SDK设置推送通知。亚博
步骤1 -在Apple Developer站点上创建。p12证书
对于下面的步骤,您必须登录到您的Apple Developer帐户。如果你还没有开发者账户,你可以注册在这里.
- 登录Apple Developer Member Center,然后导航到证书列表.
- 单击
+
按钮以创建新证书,然后选择Apple推送通知服务SSL (Sandbox & Production)
. - 从下拉菜单中选择你的app ID,然后点击continue。
- 遵循指令使用Keychain Access生成证书签名请求(CSR),并将其上传以生成证书。
- 证书准备好后,将其下载到您的计算机并双击它以在Keychain Access中打开它。
- 右键单击您创建的证书,然后选择
导出“Apple Push Services: {your-app-id}”
. - 如果需要,选择一个密码,并保存
.p12
文件到你的电脑。
重要的:我们不支持只使用沙箱证书。为了启用推送通知,您必须在生产环境中使用证书。
我们不支持任何其他方式来生成CSR和。p12文件。
苹果在应用程序中实现推送功能所需的文档没有由Zendesk提供,但可以在Zendesk上找到亚博苹果开发者网站.
这同样适用于Xcode配置的某些部分,除了添加推送通知功能(见下文)。
步骤2 -在Zendesk管理中心添加。p12证书亚博
现在您已经创建了您的.p12
证书,它将需要上传到Zendesk管理中心的管理员在您的Zendesk帐户。亚博如果您不是管理员,请联系您帐户上的管理员,以协助您执行此步骤。
如果你已经启用了iOS消息
- 在Zendes亚博k管理中心,单击Channels图标,然后单击消息传递.
- 点击iOS,然后单击通知选项卡。
- 拖放您的。p12证书或单击链接找到并上传您的证书。
如果你没有启用iOS消息
- 在Zendes亚博k管理中心,单击Channels图标,然后单击消息设置.
- 在“设置消息传递”页上,选择复选框到为您的帐户启用消息传递.
- 点击管理渠道.
- 点击添加通道,然后点击iOS.
- 按照提示配置通道,然后单击完成.
- 如果iOS通道尚未打开,请单击通知选项卡。
- 拖放您的。p12证书或单击链接找到并上传您的证书。
步骤3 -在项目中添加推送通知功能
选择您的项目目标。
转到签名和功能选项卡。
添加推送通知能力与+能力按钮。
步骤4—设置APS环境权限
属性时自动添加推送通知能力。检查应用程序的.entitlements文件,以确保一个APS的环境
键存在并且正在使用生产.
步骤5 -请求用户授权推送通知
使用注册推送通知UNUserNotificationCenter
在你的AppDelegate在应用程序(didFinishLaunchingWithOptions)
.
斯威夫特
进口UserNotifications
函数application(_ application: UIApplication)
didFinishLaunchingWithOptions: [UIApplication.]LaunchOptionsKey: Any]) -> Bool {
registerForPushNotifications ()
还真
}
私有函数registerForPushNotifications() {
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.delegate = self
notificationCenter。请求Authorization(options: [.alert, .sound, .badge]) { allowed, _ in
允许守卫,否则{返回}
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications ()
}
}
}
objective - c
#进口<UserNotifications/UserNotifications.h>
-(保龄球)应用程序:(UIApplication*)应用didFinishLaunchingWithOptions:(NSDictionary*)launchOptions{
[自我registerForPushNotifications];
返回是的;
}
-(无效)registerForPushNotifications{
UNUserNotificationCenter*中心=[UNUserNotificationCenter currentNotificationCenter];
中心.委托=自我;
[中心requestAuthorizationWithOptions:(UNAuthorizationOptionSound|UNAuthorizationOptionAlert|UNAuthorizationOptionBadge)
completionHandler:^(BOOL允许,NSError*_Nullable错误){
如果(允许){
设置(dispatch_get_main_queue(),^{
[[UIApplication sharedApplication]registerForRemoteNotifications];
});
}
}];
}
步骤6 -添加Zendesk SD亚博K到您的应用程序
添加Zendes亚博k SDK并更新PushNotifications
使用接收到的设备令牌初始化。
覆盖应用程序(didRegisterForRemoteNotificationsWithDeviceToken)
在你的AppDelegate
.
斯威夫特
进口Zende亚博skSDKMessaging
函数application(_ application: UIApplication, didreregisterforremotenotifationswithdevicetoken deviceToken: Data) {
PushNotifications.updatePushNotificationToken (deviceToken)
}
objective - c
#进口<亚博ZendeskSDKMessaging/亚博ZendeskSDKMessaging.h>
-(无效)应用程序:(UIApplication*)应用didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken{
[ZDKPushNotifications updatePushNotificationToken:deviceToken];
}
步骤7 -在应用程序中显示推送通知
符合UNUserNotificationCenterDelegate
.
斯威夫特
类AppDelegate: UIResponder UIApplicationDelegate UNUserNotificationCenterDelegate
objective - c
@ interfaceAppDelegate:UIResponder<UIApplicationDelegate,UNUserNotificationCenterDelegate>
实现userNotificationCenter (willPresent)
在应用程序处于前台时显示通知。
斯威夫特
UNUserNotificationCenter: UNUserNotificationCenter
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
//检查收到的推送通知是否应该由Messaging显示
let shouldbedisplay = pushnotification . shouldbedisplay (userInfo)
switch shouldbedisplay {
案例.messagingShouldDisplay:
//此推送属于消息传递,SDK能够将其显示给最终用户
if #available(iOS 14.0, *) {
completionHandler([。横幅,声音,徽章])
} else {
completionHandler([。Alert, .sound, .badge])
}
案例.messagingShouldNotDisplay:
//此推送属于消息传递,但不应该显示给最终用户
completionHandler ([])
案例.notFromMessaging:
//此推送不属于消息传递
//如果你的应用中有推送通知,把你的代码放在这里
//如果没有,就调用completionHandler
completionHandler ([])
@unknown default:中断
}
}
objective - c
-(无效)userNotificationCenter:(UNUserNotificationCenter*)中心
willPresentNotification:(UNNotification*)通知
withCompletionHandler:(无效(^)(UNNotificationPresentationOptions))completionHandler{
id用户信息=通知.请求.内容.用户信息;
//检查收到的推送通知是否应该由Messaging显示
ZDKPushResponsibility shouldBeDisplayed=[ZDKPushNotifications shouldBeDisplayed:用户信息];
开关(shouldBeDisplayed){
情况下ZDKPushResponsibilityMessagingShouldDisplay:
//此推送属于消息传递,SDK能够将其显示给最终用户
如果(@可用(iOS14.0,*)){
completionHandler(UNNotificationPresentationOptionBanner|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionBadge);
}其他的{
completionHandler(UNNotificationPresentationOptionAlert|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionBadge);
}
打破;
情况下ZDKPushResponsibilityMessagingShouldNotDisplay:
//此推送属于消息传递,但不应该显示给最终用户
completionHandler(UNNotificationPresentationOptionNone);
打破;
情况下ZDKPushResponsibilityNotFromMessaging:
//此推送不属于消息传递
//如果你的应用中有推送通知,把你的代码放在这里
//如果没有,调用completionHandler和break
completionHandler(UNNotificationPresentationOptionNone);
打破;
}
}
步骤8 -处理应用中的推送通知
实现userNotificationCenter (didReceive)
当通知被点击时处理。
斯威夫特
UNUserNotificationCenter: UNUserNotificationCenter
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
//检查收到的推送通知是否应该由Messaging处理
let shouldbedisplay = pushnotification . shouldbedisplay (userInfo)
switch shouldbedisplay {
案例.messagingShouldDisplay:
//这个推送属于消息传递,当最终用户与它交互时,SDK能够处理
PushNotifications.handleTap(userInfo) {viewController in
//处理在这里显示返回的viewController
}
案例.messagingShouldNotDisplay:
//这个推送属于消息传递,但是交互不应该由SDK处理
打破
案例.notFromMessaging:
//此推送不属于消息传递
//如果你的应用中有推送通知,把你的代码放在这里
//如果没有,就调用' break '
打破
@unknown default:中断
}
completionHandler ()
}
objective - c
-(无效)userNotificationCenter:(UNUserNotificationCenter*)中心
didReceiveNotificationResponse:(UNNotificationResponse*)响应
withCompletionHandler:(无效(^)(无效))completionHandler{
id用户信息=响应.通知.请求.内容.用户信息;
//检查收到的推送通知是否应该由Messaging处理
ZDKPushResponsibility shouldBeDisplayed=[ZDKPushNotifications shouldBeDisplayed:用户信息];
开关(shouldBeDisplayed){
情况下ZDKPushResponsibilityMessagingShouldDisplay:
//这个推送属于消息传递,当最终用户与它交互时,SDK能够处理
[ZDKPushNotifications handleTap:完成用户信息:^(ui*_Nullable viewController){
//处理在这里显示返回的viewController
}];
打破;
情况下ZDKPushResponsibilityMessagingShouldNotDisplay:
//这个推送属于消息传递,但是交互不应该由SDK处理
打破;
情况下ZDKPushResponsibilityNotFromMessaging:
//此推送不属于消息传递
//如果你的应用中有推送通知,把你的代码放在这里
//如果不是,就调用break
打破;
}
completionHandler();
}
请注意当应用程序处于后台状态时,返回的viewController将允许你显示对话。当应用处于kill状态时,它会返回nil,因为SDK还没有初始化。
如果你想改变那个行为,你将不得不实现你自己对推送显示的处理,以及处理SDK的初始化和messagingViewController
.