Explorar el Código

add push message

stanley-king hace 7 años
padre
commit
188b1351c1

+ 14 - 5
data/api/notification/AndroidNotification.php

@@ -40,9 +40,12 @@ abstract class AndroidNotification extends UmengNotification {
 
 		if (in_array($key, $this->DATA_KEYS)) {
 			$this->data[$key] = $value;
-		} else if (in_array($key, $this->PAYLOAD_KEYS)) {
+		}
+		else if (in_array($key, $this->PAYLOAD_KEYS))
+		{
 			$this->data["payload"][$key] = $value;
-			if ($key == "display_type" && $value == "message") {
+			if ($key == "display_type" && $value == "message")
+			{
 				$this->data["payload"]["body"]["ticker"] = "";
 				$this->data["payload"]["body"]["title"] = "";
 				$this->data["payload"]["body"]["text"] = "";
@@ -51,14 +54,20 @@ abstract class AndroidNotification extends UmengNotification {
 					$this->data["payload"]["body"]["custom"] = NULL;
 				}
 			}
-		} else if (in_array($key, $this->BODY_KEYS)) {
+		}
+		else if (in_array($key, $this->BODY_KEYS))
+		{
 			$this->data["payload"]["body"][$key] = $value;
 			if ($key == "after_open" && $value == "go_custom" && !array_key_exists("custom", $this->data["payload"]["body"])) {
 				$this->data["payload"]["body"]["custom"] = NULL;
 			}
-		} else if (in_array($key, $this->POLICY_KEYS)) {
+		}
+		else if (in_array($key, $this->POLICY_KEYS))
+		{
 			$this->data["policy"][$key] = $value;
-		} else {
+		}
+		else
+        {
 			if ($key == "payload" || $key == "body" || $key == "policy" || $key == "extra") {
 				throw new Exception("You don't need to set value for ${key} , just set values for the sub keys in it.");
 			} else {

+ 35 - 1
data/logic/queue.logic.php

@@ -24,7 +24,7 @@ require_once (BASE_ROOT_PATH . '/helper/fcode/operator.php');
 require_once (BASE_ROOT_PATH . '/helper/user_session/fcode.php');
 require_once (BASE_ROOT_PATH . '/helper/stat_helper.php');
 require_once (BASE_ROOT_PATH . '/helper/message/publisher.php');
-
+require_once (BASE_ROOT_PATH . '/helper/search/tcp_client.php');
 
 class queueLogic
 {
@@ -420,6 +420,13 @@ class queueLogic
         return callback(true);
     }
 
+    public function sendPushMessage($param)
+    {
+        $push = new push_sender();
+        $push->send_message($param);
+        return callback(true);
+    }
+
     /**
      * 发短信
      * @param $param
@@ -662,7 +669,34 @@ class queueLogic
     {
         $publisher = new message\publisher();
         $publisher->add_special($param['user_id'],$param['specials']);
+
+        $specials = $param['specials'];
+        $pub_count = 0;
+        $pri_count = 0;
+        foreach ($specials as $special)
+        {
+            if($special['type'] == 0) {
+                $pub_count++;
+            }
+            else {
+                $pri_count++;
+            }
+        }
+
+        $result = search\relation_client::instance()->fetch_follow(array('user_id' => $param['user_id']));
+        if($result['code'] == 200)
+        {
+            $follows = $result['data']['follows'];
+            $pushor = new push_message();
+            if($pub_count > 0) {
+                $pushor->publish_special($follows,true,$pub_count);
+            }
+            if($pri_count > 0) {
+                $pushor->publish_special($follows,false,$pri_count);
+            }
+        }
     }
+
     public function onDelSpecial($param)
     {
         $publisher = new message\publisher();

+ 18 - 0
helper/push_helper.php

@@ -184,4 +184,22 @@ class push_helper
 
         return $param;
     }
+}
+class push_message
+{
+    static public function publish_special($member_ids,$ispub,$count)
+    {
+        if($ispub) {
+            $visible_type = 'pub_visible';
+        } else {
+            $visible_type = 'pri_visible';
+        }
+        foreach ($member_ids as $member_id)
+        {
+            $info['member_id'] = $member_id;
+            $info['custom'] = ['type' => 'add_special','visible_type' => $visible_type,'count' => $count];
+
+            QueueClient::push('sendPushMessage',$info);
+        }
+    }
 }

+ 61 - 12
helper/push_sender.php

@@ -26,20 +26,16 @@ class push_sender
     private function sendAndroidCustomizedcast(array $pushinfo)
     {
         $customizedcast = new AndroidCustomizedcast();
+
         $customizedcast->setAppMasterSecret($this->androd_appmastersecret);
         $customizedcast->setPredefinedKeyValue("appkey", $this->androd_appkey);
         $customizedcast->setPredefinedKeyValue("timestamp", $this->timestamp);
-        // Set your alias here, and use comma to split them if there are multiple alias.
-        // And if you have many alias, you can also upload a file containing these alias, then
-        // use file_id to send customized notification.
         $customizedcast->setPredefinedKeyValue("alias", $pushinfo['member_id']);
-        // Set your alias_type here
         $customizedcast->setPredefinedKeyValue("alias_type", $this->android_alias_type);
         $customizedcast->setPredefinedKeyValue("ticker", $pushinfo['text']);
         $customizedcast->setPredefinedKeyValue("title", $pushinfo['text']);
         $customizedcast->setPredefinedKeyValue("text", $pushinfo['text']);
         $customizedcast->setPredefinedKeyValue("after_open", "go_app");
-        //Set extra fields
         $customizedcast->setExtraField("go_type", $pushinfo['go_type']);
 
         return $customizedcast->send();
@@ -52,24 +48,48 @@ class push_sender
         $customizedcast->setAppMasterSecret($this->ios_appmastersecret);
         $customizedcast->setPredefinedKeyValue("appkey", $this->ios_appkey);
         $customizedcast->setPredefinedKeyValue("timestamp", $this->timestamp);
-
-        // Set your alias here, and use comma to split them if there are multiple alias.
-        // And if you have many alias, you can also upload a file containing these alias, then
-        // use file_id to send customized notification.
         $customizedcast->setPredefinedKeyValue("alias", $pushinfo['member_id']);
-        // Set your alias_type here
         $customizedcast->setPredefinedKeyValue("alias_type", $this->ios_alias_type);
         $customizedcast->setPredefinedKeyValue("alert", $pushinfo['text']);
         $customizedcast->setPredefinedKeyValue("badge", 0);
         $customizedcast->setPredefinedKeyValue("sound", "chime");
-        // Set 'production_mode' to 'true' if your app is under production mode
         $customizedcast->setPredefinedKeyValue("production_mode", "true");
-        //Set extra fields
+
         $customizedcast->setCustomizedField("go_type", $pushinfo['go_type']);
 
         return $customizedcast->send();
     }
 
+    private function sendAndroidCustomizedcastMessage(array $pushinfo)
+    {
+        $customizedcast = new AndroidCustomizedcast();
+        $customizedcast->setAppMasterSecret($this->androd_appmastersecret);
+        $customizedcast->setPredefinedKeyValue("appkey", $this->androd_appkey);
+        $customizedcast->setPredefinedKeyValue("timestamp", $this->timestamp);
+        $customizedcast->setPredefinedKeyValue("alias", $pushinfo['member_id']);
+        $customizedcast->setPredefinedKeyValue("alias_type", $this->android_alias_type);
+        $customizedcast->setPredefinedKeyValue("display_type", 'message');
+        $customizedcast->setPredefinedKeyValue("production_mode", "true");
+        $customizedcast->setPredefinedKeyValue("custom", json_encode($pushinfo['custom']));
+        return $customizedcast->send();
+    }
+
+    private function sendIOSCustomizedcastMessage(array $pushinfo)
+    {
+        $customizedcast = new IOSCustomizedcast();
+        $customizedcast->setAppMasterSecret($this->ios_appmastersecret);
+        $customizedcast->setPredefinedKeyValue("appkey", $this->ios_appkey);
+        $customizedcast->setPredefinedKeyValue("timestamp", $this->timestamp);
+        $customizedcast->setPredefinedKeyValue("alias", $pushinfo['member_id']);
+        $customizedcast->setPredefinedKeyValue("alias_type", $this->ios_alias_type);
+        $customizedcast->setPredefinedKeyValue("display_type", 'message');
+        $customizedcast->setPredefinedKeyValue("alert", $pushinfo['text']);
+        $customizedcast->setPredefinedKeyValue("production_mode", "true");
+        $customizedcast->setPredefinedKeyValue("custom", json_encode($pushinfo['custom']));
+
+        return $customizedcast->send();
+    }
+
     public function send(array $info)
     {
         try
@@ -98,4 +118,33 @@ class push_sender
 
         return false;
     }
+
+    public function send_message(array $info)
+    {
+        try
+        {
+            $ret = $this->sendAndroidCustomizedcastMessage($info);
+            $data = json_decode($ret,true);
+            if(strtoupper($data['ret']) == 'SUCCESS') {
+                return true;
+            }
+        }
+        catch (Exception $ex) {
+            Log::record("push_app sendAndroid error",Log::ERR);
+        }
+
+        try
+        {
+            $ret = $this->sendIOSCustomizedcastMessage($info);
+            $data = json_decode($ret,true);
+            if(strtoupper($data['ret']) == 'SUCCESS') {
+                return true;
+            }
+        }
+        catch (Exception $ex) {
+            Log::record("push_app sendAndroid error",Log::ERR);
+        }
+
+        return false;
+    }
 }

+ 11 - 0
test/TestPush.php

@@ -10,6 +10,7 @@
 define('BASE_ROOT_PATH',str_replace('/test','',dirname(__FILE__)));
 require_once(BASE_ROOT_PATH . '/fooder.php');
 require_once(BASE_ROOT_PATH . '/helper/memsg/message_sender.php');
+require_once(BASE_ROOT_PATH . '/helper/push_sender.php');
 
 class TestPush extends PHPUnit_Framework_TestCase
 {
@@ -41,4 +42,14 @@ class TestPush extends PHPUnit_Framework_TestCase
         $ch = 65;
         $x = sprintf("%c",$ch);
     }
+    public function testMessage()
+    {
+        $pushor = new push_sender();
+
+        $info['member_id'] = 40287;
+        $info['text'] = 'hello message';
+        $info['custom'] = ['type' => 'add_special','special_id' => 300];
+        $pushor->send_message($info);
+
+    }
 }