I'm using Laravel as my backend and Android as my front end. In my mobile app, I need to send notifications when clicking on a card view to alert the other user. These users will be associted to each other in database tables. So do I have to work with laravel or android for this part? PS: I am using MySQL, any help will be appreciated.
1条回答 默认 最新
- douping1825 2019-03-08 12:08关注
you can use a great package https://github.com/davibennun/laravel-push-notification;
or by php For Android
<?php public static function send_notification_android($device_id, $android_ids, $message, $title) { $result = array(); $payload = array(); $url = 'https://fcm.googleapis.com/fcm/send'; $api_key = 'AAAA3KgUuXA:APA91bEfEs8UHGIzxVPPAxnNAAwaOIfHw82LTJ2PzNjh4P-kDJGUx5KAtqHmcNixyslW087G3dWfNVwMrfmvj9tHKmEoC5OB3NW6CZT_H4oGFv_QvTZWN9aEr2IfZ3QOixfTaF0VTac9'; $result['title'] = $title; $result['body'] = $message; $fields = array( 'to' => $android_ids, 'data' => $result ); $headers = array( 'Authorization: key=' . $api_key, 'Authorization: key=' . $api_key, 'Content-Type: application/json' ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); $result = curl_exec($ch); if ($result === FALSE) { die('Curl failed: ' . curl_error($ch)); } curl_close($ch); if (!empty($result)) { return false; } else { return true; } } IOS <?php public function send_push_notification($deviceToken, $badges, $msg, $data) { $key = public_path() . '/pushcert12.pem'; $url = "ssl://gateway.push.apple.com:2195"; // $url = 'ssl://gateway.sandbox.push.apple.com:2195'; // for development $ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'local_cert', $key); stream_context_set_option($ctx, 'ssl', 'passphrase', 'ThreatAlert'); $fp = stream_socket_client($url, $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx); $message = $msg; $body['aps'] = array( 'alert' => $message, 'badge' => intval($badges) , 'sound' => 'default', 'data' => '' ); $payload = json_encode($body); $deviceToken = str_replace(' ', '', $deviceToken); try { $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload . chr(3) . pack('n', 4) . pack('N', $deviceToken) . chr(4) . pack('n', 4) . pack('N', time() + 86400) . chr(5) . pack('n', 1) . chr(10); $result = fwrite($fp, $msg, strlen($msg)); if (!$result) { $res = 0; } else { $res = 1; } $retmsg = $res; } catch(Exception $eee) { $this->log('Message Push - ' . $eee); } fclose($fp); $retmsg = '<br />' . date("Y-m-d H:i:s") . ' Connection closed to APNS' . PHP_EOL; }
解决 无用评论 打赏 举报
悬赏问题
- ¥15 2024-五一综合模拟赛
- ¥15 下图接收小电路,谁知道原理
- ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
- ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
- ¥15 手机接入宽带网线,如何释放宽带全部速度
- ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
- ¥15 ETLCloud 处理json多层级问题
- ¥15 matlab中使用gurobi时报错
- ¥15 这个主板怎么能扩出一两个sata口
- ¥15 不是,这到底错哪儿了😭