Swift - JPush極光推送的使用3(根據Alias別名,給某個指定用戶發推送)(轉)


一、別名(alias)介紹
(1)我們可以給每一個安裝了應用程序的用戶,取不同別名來標識(比如可以使用用戶賬號的 userid 來作為別名)。
(2)以后給某個特定用戶推送消息時,就可以用此別名來指定。
(3)每個用戶只能指定一個別名。所以同一個設備,新設置的別名會覆蓋舊的。
(4)如果要刪除已有的別名,只要將別名設置為空字符串即可。
(5)系統不限定一個別名只能指定一個用戶。如果一個別名被指定到了多個用戶,當給指定這個別名發消息時,服務器端API會同時給這多個用戶發送消息。

二、別名使用要求 (1)有效的別名組成:字母(區分大小寫)、數字、下划線、漢字。
(2)限制:alias 命名長度限制為 40 字節。(判斷長度需采用 UTF-8 編碼)

三、別名使用的樣例說明
下面是一個給指定用戶發送消息的樣例。
1,iOS客戶端界面
客戶端在啟動后,我們可以在輸入框中填寫 alias 別名,點擊“注冊別名”按鈕后,便調用 API 將該設備與這個別名關聯起來。
注意:這么做只是為了演示而已,實際項目中應該是 App 在后台就自動去注冊別名(比如在登錄成功后),而不是由用戶去干預。
       原文:Swift - JPush極光推送的使用3(根據Alias別名,給某個指定用戶發推送)        原文:Swift - JPush極光推送的使用3(根據Alias別名,給某個指定用戶發推送)
2,服務端界面
我們輸入別名、消息文本,點擊“推送”按鈕后,即可給指定的用戶發送消息。
原文:Swift - JPush極光推送的使用3(根據Alias別名,給某個指定用戶發推送)

原文:Swift - JPush極光推送的使用3(根據Alias別名,給某個指定用戶發推送)

3,客戶端顯示效果
可以看到使用該別名的設備能收到消息,而其他的設備是收不到的。
原文:Swift - JPush極光推送的使用3(根據Alias別名,給某個指定用戶發推送)

四、完整代碼
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>
http://www.hangge.com/blog/cache/detail_1268.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM