public function testReceivers() { $receiver = $this->getMockForAbstractClass(AbstractReceiver::class, [], '', false); $this->notification->addReceiver($receiver); self::assertCount(2, $this->notification->getReceivers()); $this->notification->clearReceivers(); self::assertCount(0, $this->notification->getReceivers()); }
/** * Get configured service message. * * @param array $tokens * @param \Jgut\Tify\Notification $notification * * @throws \ZendService\Google\Exception\InvalidArgumentException * @throws \ZendService\Google\Exception\RuntimeException * * @return \Jgut\Tify\Adapter\Gcm\GcmMessage */ public function buildPushMessage(array $tokens, Notification $notification) { $message = $notification->getMessage(); $pushMessage = new GcmMessage(); $pushMessage->setRegistrationIds($tokens)->setCollapseKey($notification->getParameter('collapse_key'))->setDelayWhileIdle($notification->getParameter('delay_while_idle'))->setTimeToLive($notification->getParameter('time_to_live'))->setRestrictedPackageName($notification->getParameter('restricted_package_name'))->setDryRun($notification->getParameter('dry_run'))->setData($message->getPayloadData()); if ($this->shouldHaveNotification($message)) { $pushMessage->setNotificationPayload($message->getParameters()); } return $pushMessage; }
/** * Get service formatted push messages. * * @param \Jgut\Tify\Notification $notification * * @throws \ZendService\Google\Exception\InvalidArgumentException * @throws \ZendService\Google\Exception\RuntimeException * * @return \Generator */ protected function getPushMessages(Notification $notification) { foreach (array_chunk($notification->getReceivers(), 100) as $receivers) { $tokens = array_map(function ($receiver) { return $receiver instanceof GcmReceiver ? $receiver->getToken() : null; }, $receivers); (yield $this->builder->buildPushMessage(array_filter($tokens), $notification)); } }
/** * Get service formatted push messages. * * @param \Jgut\Tify\Notification $notification * * @throws \ZendService\Apple\Exception\RuntimeException * * @return \Generator */ protected function getPushMessages(Notification $notification) { foreach ($notification->getReceivers() as $receiver) { if ($receiver instanceof ApnsReceiver) { (yield $this->builder->buildPushMessage($receiver, $notification)); } } }
/** * Get service message from origin. * * @param \Jgut\Tify\Receiver\ApnsReceiver $receiver * @param \Jgut\Tify\Notification $notification * * @throws \ZendService\Apple\Exception\RuntimeException * * @return \ZendService\Apple\Apns\Message */ public function buildPushMessage(ApnsReceiver $receiver, Notification $notification) { $message = $notification->getMessage(); $messageId = sha1(sprintf('%s%s%s%s', $receiver->getToken(), $message->getParameter('title'), $message->getParameter('body'), time())); $badge = $notification->getParameter('badge') === null ? null : (int) $notification->getParameter('badge'); $pushMessage = (new ServiceMessage())->setId($messageId)->setToken($receiver->getToken())->setBadge($badge)->setSound($notification->getParameter('sound'))->setCategory($notification->getParameter('category'))->setCustom($message->getPayloadData()); if ($notification->getParameter('content-available') !== null) { $pushMessage->setContentAvailable((int) $notification->getParameter('content-available')); } if (is_array($notification->getParameter('url-args'))) { $pushMessage->setUrlArgs($notification->getParameter('url-args')); } if ($notification->getParameter('expire') !== null) { $pushMessage->setExpire($notification->getParameter('expire')); } if ($this->shouldHaveAlert($message)) { $pushMessage->setAlert(new ServiceMessageAlert($message->getParameter('body'), $message->getParameter('action-loc-key'), $message->getParameter('loc-key'), $message->getParameter('loc-args'), $message->getParameter('launch-image'), $message->getParameter('title'), $message->getParameter('title-loc-key'), $message->getParameter('title-loc-args'))); } return $pushMessage; }