Yêu cầu thg 11 20, 2019 9:41 SA 88 0 1
  • 88 0 1
0

[ask] webpush notification Minishlink\WebPush

Chia sẻ
  • 88 0 1

Mình đang có dự án cần triển khai webpush nên mình đã sử dụng Minishlink\WebPush

Nhưng mình đang gặp vấn đề như thế này

Khi truyền Subscription vào thư viện và gửi theo tuần tự thì khi gửi số lượng lớn thì báo lỗi thời gian phản hồi và file báo code 500. Đây là đoạn code mình lặp 1 mảng tầm 600 phần tử.

Mình muốn hỏi là kinh nghiệm nên chia ra gửi 1 lần bao nhiều Subscription là ổn?

        $webPush->setDefaultOptions([
			'TTL' => 86400, // if undelivered after 1 day, expire the notification
		]);

		$subscriptions = $this->getReceiverSubscriptions();

		foreach ($subscriptions AS $subscription)
		{
			$authData = json_decode($subscription['data'], true);

			$payload['subscriber_id'] = $subscription['endpoint_id'];

			try
			{
				$webPush->setAutomaticPadding($this->getEndpointPadding($subscription['endpoint']));

				$subObj = Subscription::create([
					'endpoint' => $subscription['endpoint'],
					'publicKey' => $authData['key'],
					'authToken' => $authData['token'],
					'contentEncoding' => $authData['encoding']
				]);

				$webPush->sendNotification(
					$subObj,
					json_encode($payload)
				);
			}
			catch (\Exception $e)
			{
				// generally indicates that the payload is too big which at ~3000 bytes shouldn't happen for a typical alert...
				continue;
			}
		}

		foreach ($webPush->flush() as $report) {
			$results[] = $report->jsonSerialize();
		}
Avatar Hải Hà @HaiHaChan
thg 11 20, 2019 10:56 SA

@jonyu học bạn @Stevendie cách đặt câu hỏi đi bạn ơi

1 CÂU TRẢ LỜI


Đã trả lời thg 11 20, 2019 10:49 SA
Đã được chấp nhận
+1

tạo 1 biến $array, trong vòng foreach chỉ cần đẩy giá trị

$array[] = [
    'endpoint' => $subscription['endpoint'],
    'publicKey' => $authData['key'],
    'authToken' => $authData['token'],
    'contentEncoding' => $authData['encoding']
]

vào $array. ngoài foreach thì bạn tiến hành lưu Subscription::create($array)

Chia sẻ
thg 11 21, 2019 6:09 SA

@bachnguyen2907 Bạn giỏi quá, cho mình xin info để trao đổi được ko ạ 😍

Avatar Nguyễn Văn Bách @bachnguyen2907
thg 11 21, 2019 9:22 SA

@chinn liên hệ thư ký của mình nhé @chu.xuan.thang

thg 11 21, 2019 9:24 SA

@bachnguyen2907 Xin info cả hai được ko ạ

Avatar Trúc Mai @Stevendie
thg 11 22, 2019 2:21 SA

Cảm ơn bạn. Nay mình mới thử được.

Size Time Old Time New
100 4 3.7
200 8 7.4
400 14 14.4
800 504 Gateway Time-out 504 Gateway Time-out

Code này mình để trên site nên khi gửi thì site cũng đứng luôn 🤣

** Giải pháp mình là phải gửi lần lượt từng đoạn cắt subscriptions ra làm đoạn size: 200 sau đó gửi.**

Code sau khi thay ( ở cuối ):

foreach ($subscriptions AS $subscription)
		{
			$authData = json_decode($subscription['data'], true);

			$payload['subscriber_id'] = $subscription['endpoint_id'];

			$array = [
			    'endpoint' => $subscription['endpoint'],
			    'publicKey' => $authData['key'],
			    'authToken' => $authData['token'],
			    'contentEncoding' => $authData['encoding']
			];

			try
			{
				$webPush->setAutomaticPadding($this->getEndpointPadding($subscription['endpoint']));

				$subObj = Subscription::create($array);

				// $subObj = Subscription::create([
				// 	'endpoint' => $subscription['endpoint'],
				// 	'publicKey' => $authData['key'],
				// 	'authToken' => $authData['token'],
				// 	'contentEncoding' => $authData['encoding']
				// ]);

				$webPush->sendNotification(
					$subObj,
					json_encode($payload)
				);
			}
Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí