zoukankan      html  css  js  c++  java
  • Swift

    一、别名(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 JPushClient 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
  • 相关阅读:
    test
    男神zyh的青睐
    HH的项链
    PAT刷题经验
    LaTeX常用数学符号
    Battle Over Cities Hard Version
    Cut
    文本生成器
    Explorer Space
    2021.04.21
  • 原文地址:https://www.cnblogs.com/wangbin/p/7567082.html
Copyright © 2011-2022 走看看