一、別名(alias)介紹
(4)如果要刪除已有的別名,只要將別名設置為空字符串即可。
(5)系統不限定一個別名只能指定一個用戶。如果一個別名被指定到了多個用戶,當給指定這個別名發消息時,服務器端API會同時給這多個用戶發送消息。
二、別名使用要求 (1)有效的別名組成:字母(區分大小寫)、數字、下划線、漢字。
(2)限制:alias 命名長度限制為 40 字節。(判斷長度需采用 UTF-8 編碼)
三、別名使用的樣例說明
下面是一個給指定用戶發送消息的樣例。
(2)ViewController.swift(別名注冊相關)
2,服務端代碼(index.php)
http://www.hangge.com/blog/cache/detail_1268.html
(1)我們可以給每一個安裝了應用程序的用戶,取不同別名來標識(比如可以使用用戶賬號的
userid 來作為別名)。
(2)以后給某個特定用戶推送消息時,就可以用此別名來指定。
(3)每個用戶只能指定一個別名。所以同一個設備,新設置的別名會覆蓋舊的。(2)以后給某個特定用戶推送消息時,就可以用此別名來指定。
(4)如果要刪除已有的別名,只要將別名設置為空字符串即可。
(5)系統不限定一個別名只能指定一個用戶。如果一個別名被指定到了多個用戶,當給指定這個別名發消息時,服務器端API會同時給這多個用戶發送消息。
(2)限制:alias 命名長度限制為 40 字節。(判斷長度需采用 UTF-8 編碼)
三、別名使用的樣例說明
下面是一個給指定用戶發送消息的樣例。
1,iOS客戶端界面
客戶端在啟動后,我們可以在輸入框中填寫
alias 別名,點擊“注冊別名”按鈕后,便調用 API 將該設備與這個別名關聯起來。
注意:這么做只是為了演示而已,實際項目中應該是 App 在后台就自動去注冊別名(比如在登錄成功后),而不是由用戶去干預。

2,服務端界面
注意:這么做只是為了演示而已,實際項目中應該是 App 在后台就自動去注冊別名(比如在登錄成功后),而不是由用戶去干預。


2,服務端界面
3,客戶端顯示效果
四、完整代碼
1,客戶端代碼
(1)
AppDelegate.swift(這個同之前文章里的一樣,沒有改變。本文代碼已升級至 Swfit3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
import
UIKit
@UIApplicationMain
class
AppDelegate
:
UIResponder
,
UIApplicationDelegate
{
var
window:
UIWindow
?
func
application(_ application:
UIApplication
,
didFinishLaunchingWithOptions
launchOptions: [
UIApplicationLaunchOptionsKey
:
Any
]?) ->
Bool
{
//通知類型(這里將聲音、消息、提醒角標都給加上)
let
userSettings =
UIUserNotificationSettings
(types: [.alert, .badge, .sound],
categories:
nil
)
if
((
UIDevice
.current.systemVersion
as
NSString
).floatValue >= 8.0) {
//可以添加自定義categories
JPUSHService
.register(forRemoteNotificationTypes: userSettings.types.rawValue,
categories:
nil
)
}
else
{
//categories 必須為nil
JPUSHService
.register(forRemoteNotificationTypes: userSettings.types.rawValue,
categories:
nil
)
}
// 啟動JPushSDK
JPUSHService
.setup(withOption:
nil
, appKey:
"7b96331738ea713195698fd"
,
channel:
"Publish Channel"
, apsForProduction:
false
)
return
true
}
func
application(_ application:
UIApplication
,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken:
Data
) {
//注冊 DeviceToken
JPUSHService
.registerDeviceToken(deviceToken)
}
func
application(_ application:
UIApplication
,
didReceiveRemoteNotification userInfo: [
AnyHashable
:
Any
],
fetchCompletionHandler
completionHandler:
@escaping
(
UIBackgroundFetchResult
) ->
Void
) {
//增加IOS 7的支持
JPUSHService
.handleRemoteNotification(userInfo)
completionHandler(
UIBackgroundFetchResult
.newData)
}
func
application(_ application:
UIApplication
,
didFailToRegisterForRemoteNotificationsWithError error:
Error
) {
//可選
NSLog
(
"did Fail To Register For Remote Notifications With Error: \(error)"
)
}
//..........
}
|
(2)ViewController.swift(別名注冊相關)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
import
UIKit
class
ViewController
:
UIViewController
{
@IBOutlet
weak
var
textField:
UITextField
!
@IBOutlet
weak
var
textView:
UITextView
!
override
func
viewDidLoad() {
super
.viewDidLoad()
}
//按鈕點擊
@IBAction
func
btnTouchUp(_ sender:
AnyObject
) {
//獲取別名
let
alias = textField.text
//注冊別名
JPUSHService
.setAlias(alias,
callbackSelector: #selector(tagsAliasCallBack(resCode:tags:alias:)),
object:
self
)
}
//別名注冊回調
func
tagsAliasCallBack(resCode:
CInt
, tags:
NSSet
, alias:
NSString
) {
textView.text =
"響應結果:\(resCode)"
}
override
func
didReceiveMemoryWarning() {
super
.didReceiveMemoryWarning()
}
}
|
2,服務端代碼(index.php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
<?
//引入代碼
require
'JPush/autoload.php'
;
use
JPush\Client
as
JPush;
if
(isset(
$_POST
[
"message"
])){
//初始化
$app_key
=
"7b528338438ec719495768fd"
;
$master_secret
=
"32da4a2c06d27b26d12c5628"
;
$client
=
new
JPush(
$app_key
,
$master_secret
);
//簡單的推送樣例
$result
=
$client
->push()
->setPlatform(
'ios'
,
'android'
)
->addAlias(
$_POST
[
"alias"
])
->setNotificationAlert(
$_POST
[
"message"
])
->options(
array
(
"apns_production"
=> true
//true表示發送到生產環境(默認值),false為開發環境
))
->send();
echo
'Result='
. json_encode(
$result
);
}
?>
<html>
<head>
</head>
<body>
<form action=
"index.php"
method=
"post"
>
別名:<input type=
"text"
name=
"alias"
/><br>
消息:<input type=
"text"
name=
"message"
/>
<button type=
"submit"
>推送廣播通知</button>
</form>
</body>
</html>
|