示例#1
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getChannel()
 {
     return $this->hasOne(PushChannel::className(), ['channel_id' => 'subscription_channel_id']);
 }
示例#2
0
 /**
  * @param $service_channel_id
  * @param $pushMessage Message
  * @return bool
  */
 public function sendToChannel($service_channel_id, $pushMessage)
 {
     $channel = PushChannel::findOne(["service_channel_id" => $service_channel_id]);
     // Get Related ANDROID devices
     // ============================
     $android_devices = PushSubscription::find()->joinWith("user")->where(["subscription_channel_id" => $channel->channel_id, "subscription_status" => PushSubscription::STATUS_ACTIVE])->andWhere(["push_device_type" => PushUser::TYPE_ANDROID, "push_device_status" => PushUser::STATUS_ACTIVE]);
     $android_tokens = [];
     $i = 0;
     $j = 1;
     foreach ($android_devices as $android_device) {
         if ($j % 1000 != 0) {
             $android_tokens[$i][] = $android_device->user->push_device_token;
         } else {
             $i++;
             $android_tokens[$i][] = $android_device->user->push_device_token;
         }
         $j++;
     }
     // Get Related IOS devices
     // ========================
     $ios_devices = PushSubscription::find()->joinWith("user")->where(["subscription_channel_id" => $channel->channel_id, "subscription_status" => PushSubscription::STATUS_ACTIVE])->andWhere(["push_device_type" => PushUser::TYPE_ANDROID, "push_device_status" => PushUser::STATUS_ACTIVE]);
     $ios_tokens = [];
     $i = 0;
     $j = 1;
     foreach ($ios_devices as $ios_device) {
         if ($j % 1000 != 0) {
             $ios_tokens[$i][] = $ios_device->user->push_device_token;
         } else {
             $i++;
             $ios_tokens[$i][] = $ios_device->user->push_device_token;
         }
         $j++;
     }
     // Send notification to the related devices
     // ========================================
     foreach ($android_tokens as $android_tokens_pack) {
         $pushMessage->android->setTo($android_tokens_pack);
         $this->getClient()->sendAndroid($pushMessage);
     }
     foreach ($ios_tokens as $ios_tokens_pack) {
         $pushMessage->ios->setTo($ios_tokens_pack);
         $this->getClient()->sendIOS($pushMessage);
     }
 }