Swift

来源:互联网 发布:照片尺寸修改软件 编辑:程序博客网 时间:2024/05/16 18:28

Swift - JPush极光推送的使用4(根据Tag标签,给同一类别用户发推送)

一、标签(tag)介绍
(1)前文讲的别名(alias)是为了对每一个用户进行标识。而标签(tag)是用来将用户分类分组,这样便于批量推送消息。
(2)可为每个用户打多个标签。(比如: vipwomengame 等等)
(3)不同应用程序、不同的用户,可以打同样的标签。
(4)每次调用至少设置一个 tag。(这个会覆盖之前的设置,不是新增。)
(5)使用空数组或列表表示取消之前的设置。

二、标签使用要求
(1)有效的标签组成:字母(区分大小写)、数字、下划线、汉字。
(2)限制:每个 tag 命名长度限制为 40 字节,最多支持设置 100tag,但总长度不得超过 1K 字节。(判断长度需采用UTF-8 编码)
(3)单个设备最多支持设置 100tagApp 全局 tag 数量无限制。

三、标签使用的样例说明
下面给某些标签下关联的所有用户发送通知消息。
1,iOS客户端界面
客户端在启动后,我们可以选择我们关注的新闻类别(每个类别对应一个标签)。点击“注册标签”按钮后,便调用 API 将该设备与选择的标签关联起来。
原文:Swift - JPush极光推送的使用4(根据Tag标签,给同一类别用户发推送)

               
2,服务端界面
我们输入标签(如果给多个标签发送,则用逗号隔开)、消息文本,点击“推送”按钮后,即可给这些标签对应的所有用户发送消息。
原文:Swift - JPush极光推送的使用4(根据Tag标签,给同一类别用户发推送)

原文:Swift - JPush极光推送的使用4(根据Tag标签,给同一类别用户发推送)


3,客户端显示效果
可以看到关联了这些标签的设备能收到消息,而其他的设备是收不到的。
原文:Swift - JPush极光推送的使用4(根据Tag标签,给同一类别用户发推送)

四、完整代码
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 {
     
    varwindow: UIWindow?
     
    funcapplication(_ application: UIApplication,
                     didFinishLaunchingWithOptions
                        launchOptions: [UIApplicationLaunchOptionsKey:Any]?) -> Bool {
       
        //通知类型(这里将声音、消息、提醒角标都给加上)
        letuserSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound],
                                                      categories:nil)
        if((UIDevice.current.systemVersionas 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)
 
        returntrue
    }
  
     
    funcapplication(_ application: UIApplication,
                     didRegisterForRemoteNotificationsWithDeviceToken deviceToken:Data) {
        //注册 DeviceToken
        JPUSHService.registerDeviceToken(deviceToken)
    }
     
    funcapplication(_ application: UIApplication,
                     didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                     fetchCompletionHandler
                        completionHandler:@escaping (UIBackgroundFetchResult) ->Void) {
        //增加IOS 7的支持
        JPUSHService.handleRemoteNotification(userInfo)
        completionHandler(UIBackgroundFetchResult.newData)
    }
     
    funcapplication(_ 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
31
32
33
34
35
36
37
38
39
40
41
42
import UIKit
 
class ViewController: UIViewController{
 
    @IBOutletweak var switch1: UISwitch!
    @IBOutletweak var switch2: UISwitch!
    @IBOutletweak var switch3: UISwitch!
    @IBOutletweak var textView: UITextView!
     
    overridefunc viewDidLoad() {
        super.viewDidLoad()
    }
     
    //按钮点击
    @IBActionfunc btnTouchUp(sender:AnyObject) {
        //获取标签
        vartags = Set<NSObject>()
        ifswitch1.on {
            tags.insert("movie")
        }
        ifswitch2.on {
            tags.insert("music")
        }
        ifswitch3.on {
            tags.insert("game")
        }
         
        //注册标签
        JPUSHService.setTags(tags,
                              callbackSelector: #selector(tagsAliasCallBack(_:tags:alias:)),
                              object:self)
    }
     
    //标签注册回调
    functagsAliasCallBack(resCode:CInt, tags:NSSet, alias:NSString) {
        textView.text ="响应结果:\(resCode)"
    }
 
    overridefunc 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
37
38
39
<?
//引入代码
require 'JPush/autoload.php';
 
use JPush\Clientas JPush;
 
if(isset($_POST["message"])){
  //初始化
  $app_key ="7b528333738ec729165798fd";
  $master_secret ="32da4e2c36957b247a2c9828";
  $client =new JPush($app_key, $master_secret);
 
  //将逗号隔开的字符串拆分成标签数组
  $tags = explode(',', $_POST["tags"]);
 
  //简单的推送样例
  $result = $client->push()
      ->setPlatform('ios','android')
      ->addTag($tags)
      ->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="tags"/><br>
      消息:<input type="text"name="message"/>
      <button type="submit">推送广播通知</button>
    </form>
  </body>
</html>

原文出自:www.hangge.com  转载请保留原文链接:http://www.hangge.com/blog/cache/detail_1272.html
原创粉丝点击