阿里大鱼手机短信发送、极光手机推送

现在很多公司的业务处理、事务处理、消息展示都会通过一些方式去通知用户,比如发送短信、手机推送等等。

所以很多公司都整合了SMS短信发送、Jpush手机推送等等,以前手机推送都是使用原生的推送,如IOS的APNS、Android的GCM推送,现在都整合到了一起,极光推送平台已经都整合了很多设备、短信、语音等推送。

以下贴出相应的code:

1.阿里大鱼短信:

// APP登录发行手机验证码
public function getCodeAction ()
{
	$request = $this->getRequest();
	$phone = $request->get('phone');
	$phone = str_replace(' ', '', $phone);
	if ($phone && preg_match('/^(13[0-9]|14[0-9]|15[0-9]|18[0-9])\d{8}$/i', $phone)) {

		//短信验证码异常log生成路径
		$smsCaptchaLogFile = StringHelper::generateLogFileAndReturn($this->getCfgParameter('sms_captcha_error_log_path'), 'error_');
		try {
			// 阿里短信SDK引入
			$captchaSdkPath = $this->getCfgParameter('sms_aliyun_captcha_sdk_path');
			include($captchaSdkPath . 'TopSdk.php');

			// 本地生成的短信验证码
			$phoneValidCode = StringHelper::generateTelCode();
			// 取得短信模板ID
			$stencilId = $this->getCfgParameter('sms_aliyun_template_id');
			// 发送短信封装
			$c = new \TopClient;
			$req = new \AlibabaAliqinFcSmsNumSendRequest;
			$req->setSmsType("normal");
			$req->setSmsFreeSignName("官网的短信模板签名");
			$req->setSmsParam("{\"code\":\"$phoneValidCode\"}");
			$req->setRecNum($phone);
			$req->setSmsTemplateCode($stencilId);
			$result=$c->e xecute($req);//这边因为网站安全问题不能写执行关键字,所以加了空格
			if($result->children()->result->err_code==0 && empty($result->children()->code) ){
				// 验证码发送成功
				// TODO 业务处理
			}else{
			    // 短信发送失败,log记录
				$logContent = "{$phone}短信验证码异常:" . $result->children()->sub_msg;
				LoggerHelper::addLog($smsCaptchaLogFile, $logContent, Logger::ERROR);
				// 短信发送失败的code和message
				$this->returnArray['code'] = 0;
				$this->returnArray['message'] = $result->children()->sub_msg;
			}
		} catch (\Exception $e) {
	        // 代码异常情况
			$logContent = "{$phone}短信验证码异常:" . $e->getMessage();
			LoggerHelper::addLog($smsCaptchaLogFile, $logContent, Logger::ERROR);
		}
	}
}

2.极光Jpush推送:

// 推送IOS、Android
// @param array $registrationIdArray
// @param string $message
// @return string/boolean
public function push ($registrationIdArray, $message, $contentType = 'messageType')
{
	// 极光推送SDK引入
	$appKey = $this->container->getParameter('sms_jpush_sdk_key');
	$appSecret = $this->container->getParameter('sms_jpush_sdk_secret');
	$jpushSdkPath = $this->container->getParameter('sms_jpush_sdk_path');
	include($jpushSdkPath . 'autoload.php');

	//极光异常log生成路径
	$smsJpushLogFile = StringHelper::generateLogFileAndReturn($this->container->getParameter('sms_jpush_error_log_path'), 'info_');
	$smsJpushExceptionLogFile = StringHelper::generateLogFileAndReturn($this->container->getParameter('sms_jpush_error_log_path'), 'error_');
	// 初始化推送Client
	$jpushClient = new \JPush\Client($appKey, $appSecret, $smsJpushLogFile);

	try {
		$title = '';
		if (is_array($message)) {
			$title = $message['title'];
			$message = $message['message'];
		} else {
			$title = $message;
		}
        //$registrationIdArray = ['11112222222222aaaaa', '2222222rtestetsetstset'];//测试时使用
		$response = $jpushClient->push()
			->setPlatform('all')
			// 一般情况下,关于 audience 的设置只需要调用 addAlias、addTag、addTagAnd  或 addRegistrationId
			// 这四个方法中的某一个即可,这里仅作为示例,当然全部调用也可以,多项 audience 调用表示其结果的交集
			// 即是说一般情况下,下面三个方法和没有列出的 addTagAnd 一共四个,只适用一个便可满足大多数的场景需求
			->addRegistrationId($registrationIdArray);
		if ($contentType == 'loginType') {
			$response->message($message, array(
				'title' => $title,
				'content_type' => $contentType,// loginType/messageType
//                    'extras' => array(
//                        'key' => 'value',
//                        'content'=>'jiguang'
//                    ),
			));
		} else {
			$response->setNotificationAlert($message)
				->iosNotification($message, ['sound' => '_enable_Sound'])
				->androidNotification($message, ['sound' => '_enable_Sound']);
		}
		$response->options(array(
				// True 表示推送生产环境,False 表示要推送开发环境;如果不指定则默认为推送生产环境
				'apns_production' => False,
			))
			->send();
		$logContent = "极光推送正常,极光返回信息: serialize response: " . serialize($response);
		LoggerHelper::addLog($smsJpushLogFile, $logContent, Logger::INFO);
		return $response;
	} catch (\JPush\Exceptions\APIConnectionException $e) {
		$logContent = "极光推送异常:" . $e->getMessage() . ', serialize registrationIdArray: ' . serialize($registrationIdArray);
		LoggerHelper::addLog($smsJpushExceptionLogFile, $logContent, Logger::ERROR);
	} catch (\JPush\Exceptions\APIRequestException $e) {
		$logContent = "极光推送异常:" . $e->getMessage() . ', serialize registrationIdArray: ' . serialize($registrationIdArray);
		LoggerHelper::addLog($smsJpushExceptionLogFile, $logContent, Logger::ERROR);
	}
	return false;
}

※短信发送一定要做双重校验(算法校验和手机号加密),不然会被盗刷刷到爆为止,我们公司之前就被刷了1万多条,那个心疼,心急如焚的说啊。

个推推送:http://www.getui.com/cn/index.html

极光推送:https://www.jiguang.cn/

信鸽推送:https://cloud.tencent.com/product/XGPush?fromSource=gwzcw.185959.185959.185959

GoEasy推送:http://www.cnblogs.com/jishaochengduo/articles/5552645.html

Leave a comment