This repository was archived by the owner on Jun 22, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 8
Perfect translation #16
Open
CainLuo
wants to merge
14
commits into
vapor:main
Choose a base branch
from
CainLuo:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
48ecbcb
Update overview.md
8baba6d
Update overview.md
cd65310
Upgrade Chinese Translate
6abdaf2
Add leaf docs
be1bec1
Finish security translate
1134ab4
Finish redis translate
6ba1a66
Update mkdocs.yml
911d8cd
Finish security translate
aded373
Finish translate
16ef66e
Update middleware.md
0e6ffbb
Update async.md
7d9f329
Update routing.md
44bc235
Update mkdocs.yml
8a6ac2c
Update markdown path
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
# APNS | ||
|
||
Vapor的苹果推送通知服务(APNS)API使认证和发送推送通知到苹果设备变得容易。它建立在[APNSwift](https://github.com/kylebrowning/APNSwift)的基础上。 | ||
|
||
## 开始使用 | ||
|
||
让我们来看看你如何开始使用APNS。 | ||
|
||
### Package | ||
|
||
使用APNS的第一步是将软件包添加到你的依赖项中。 | ||
|
||
```swift | ||
// swift-tools-version:5.2 | ||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "my-app", | ||
dependencies: [ | ||
// 其他的依赖性... | ||
.package(url: "https://github.com/vapor/apns.git", from: "2.0.0"), | ||
], | ||
targets: [ | ||
.target(name: "App", dependencies: [ | ||
// 其他的依赖性... | ||
.product(name: "APNS", package: "apns") | ||
]), | ||
// Other targets... | ||
] | ||
) | ||
``` | ||
|
||
如果您在Xcode中直接编辑清单,它将会自动接收更改并在保存文件时获取新的依赖关系。否则,从终端运行`swift package resolve`来获取新的依赖关系。 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 我们建议使用 “你” 来代替 “您” 即可,可以参考此术语约定: https://github.com/vapor/docs-cn/wiki |
||
### 配置 | ||
|
||
APNS模块为`Application`添加了一个新的属性`apns`。为了发送推送通知,你需要用你的证书设置`configuration`属性。 | ||
|
||
```swift | ||
import APNS | ||
|
||
// 使用JWT认证配置APNS。 | ||
app.apns.configuration = try .init( | ||
authenticationMethod: .jwt( | ||
key: .private(filePath: <#path to .p8#>), | ||
keyIdentifier: "<#key identifier#>", | ||
teamIdentifier: "<#team identifier#>" | ||
), | ||
topic: "<#topic#>", | ||
environment: .sandbox | ||
) | ||
``` | ||
|
||
在占位符中填入你的凭证。上面的例子显示了[基于JWT的认证](https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/establishing_a_token-based_connection_to_apns),使用你从苹果的开发者门户获得的`.p8`密钥。对于带有证书的[基于TLS的认证](https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/establishing_a_certificate-based_connection_to_apns),使用`.tls`认证方法。 | ||
|
||
```swift | ||
authenticationMethod: .tls( | ||
privateKeyPath: <#path to private key#>, | ||
pemPath: <#path to pem file#>, | ||
pemPassword: <#optional pem password#> | ||
) | ||
``` | ||
|
||
### 发送 | ||
|
||
一旦配置了APNS,你可以使用`apns.send`方法在`Application`或`Request`上发送推送通知。 | ||
|
||
```swift | ||
// 发送一个推送通知。 | ||
try app.apns.send( | ||
.init(title: "Hello", subtitle: "This is a test from vapor/apns"), | ||
to: "98AAD4A2398DDC58595F02FA307DF9A15C18B6111D1B806949549085A8E6A55D" | ||
).wait() | ||
|
||
// 或 | ||
try await app.apns.send( | ||
.init(title: "Hello", subtitle: "This is a test from vapor/apns"), | ||
to: "98AAD4A2398DDC58595F02FA307DF9A15C18B6111D1B806949549085A8E6A55D" | ||
) | ||
``` | ||
|
||
只要你在路由处理程序中,就使用`req.apns`。 | ||
|
||
```swift | ||
// 发送一个推送通知。 | ||
app.get("test-push") { req -> EventLoopFuture<HTTPStatus> in | ||
req.apns.send(..., to: ...) | ||
.map { .ok } | ||
} | ||
|
||
// 或 | ||
app.get("test-push") { req async throws -> HTTPStatus in | ||
try await req.apns.send(..., to: ...) | ||
return .ok | ||
} | ||
``` | ||
|
||
第一个参数接受推送通知警报,第二个参数是目标设备令牌。 | ||
|
||
## 警报 | ||
|
||
`APNSwiftAlert`是要发送的推送通知警报的实际元数据。关于每个属性的具体细节[这里](https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/PayloadKeyReference.html)。它们遵循苹果文档中列出的一对一的命名方案 | ||
|
||
```swift | ||
let alert = APNSwiftAlert( | ||
title: "Hey There", | ||
subtitle: "Full moon sighting", | ||
body: "There was a full moon last night did you see it" | ||
) | ||
``` | ||
|
||
这种类型可以直接传递给`send`方法,它将被自动包裹在`APNSwiftPayload`中。 | ||
|
||
### Payload | ||
|
||
`APNSwiftPayload`是推送通知的元数据。诸如警报、徽章数量等内容。关于每个属性的具体细节都提供了[这里](https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/PayloadKeyReference.html)。它们遵循苹果文档中列出的一对一的命名方案 | ||
|
||
```swift | ||
let alert = ... | ||
let aps = APNSwiftPayload(alert: alert, badge: 1, sound: .normal("cow.wav")) | ||
``` | ||
|
||
这可以传递给`send`方法。 | ||
|
||
### 自定义通知数据 | ||
|
||
苹果公司为工程师提供了在每个通知中添加自定义有效载荷数据的能力。为了方便这一点,我们有`APNSwiftNotification`。 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里如果去掉 “公司”,你觉得如何? |
||
|
||
```swift | ||
struct AcmeNotification: APNSwiftNotification { | ||
let acme2: [String] | ||
let aps: APNSwiftPayload | ||
|
||
init(acme2: [String], aps: APNSwiftPayload) { | ||
self.acme2 = acme2 | ||
self.aps = aps | ||
} | ||
} | ||
|
||
let aps: APNSwiftPayload = ... | ||
let notification = AcmeNotification(acme2: ["bang", "whiz"], aps: aps) | ||
``` | ||
|
||
这个自定义的通知类型可以被传递给`send`方法。 | ||
|
||
## 更多信息 | ||
|
||
关于可用方法的更多信息,请参阅[APNSwift的README](https://github.com/kylebrowning/APNSwift)。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
可能我有一些苛求了,但是我还是希望在专有名词或拼写与汉字之间能够添加空格,以下诸多不再重述,感谢你!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
可以参考术语翻译约定: https://github.com/vapor/docs-cn/wiki