搜尋
首頁後端開發php教程Yii1.1框架模擬PHP極光推播訊息通知

這篇文章主要講述的是用Yii框架模擬PHP極光推播訊息通知,具有一定的參考價值,有興趣的朋友可以了解一下,希望對你有幫助。

一、下載極光推送PHP SDK,解壓縮後放在/protected/components/目錄下,如下圖:

##二、完善修改下官方的demo例子,我在這裡複製一份demo,改為NotifyPush.php,如下程式碼:

<?php
require dirname(__FILE__) . &#39;/jpush-api-php-client/autoload.php&#39;;
use JPush\Client as JPush;
class NotifyPush {
    static function pushAlias($alias,$ticket=&#39;消息提醒&#39;,$alert){
        $appKey=Yii::app()->params[&#39;push&#39;][&#39;AppKey&#39;];
        $appMasterSecret=Yii::app()->params[&#39;push&#39;][&#39;AppMasterSecret&#39;];
        $production_mode=Yii::app()->params[&#39;push&#39;][&#39;production_mode&#39;];



        $client = new JPush($appKey, $appMasterSecret);


// 完整的推送示例
// 这只是使用样例,不应该直接用于实际生产环境中 !!
        try {
            $response = $client->push()
                ->setPlatform(array(&#39;ios&#39;, &#39;android&#39;))
                // 一般情况下,关于 audience 的设置只需要调用 addAlias、addTag、addTagAnd  或 addRegistrationId
                // 这四个方法中的某一个即可,这里仅作为示例,当然全部调用也可以,多项 audience 调用表示其结果的交集
                // 即是说一般情况下,下面三个方法和没有列出的 addTagAnd 一共四个,只适用一个便可满足大多数的场景需求

                ->addAlias($alias)
                ->setNotificationAlert($alert)
                ->iosNotification($ticket, array(
                    &#39;sound&#39; => &#39;sound.caf&#39;,
                    // &#39;badge&#39; => &#39;+1&#39;,
                    // &#39;content-available&#39; => true,
                    // &#39;mutable-content&#39; => true,
                    &#39;category&#39; => &#39;jiguang&#39;,
                    &#39;extras&#39; => array(
                        &#39;key&#39; => &#39;value&#39;,
                        &#39;jiguang&#39;
                    ),
                ))
                ->androidNotification($ticket, array(
                    &#39;title&#39; => $alert,
                    // &#39;build_id&#39; => 2,
                    &#39;extras&#39; => array(
                        &#39;key&#39; => &#39;value&#39;,
                        &#39;jiguang&#39;
                    ),
                ))
                ->message($alert, array(
                    &#39;title&#39; => $alert,
                    // &#39;content_type&#39; => &#39;text&#39;,
                    &#39;extras&#39; => array(
                        &#39;key&#39; => &#39;value&#39;,
                        &#39;jiguang&#39;
                    ),
                ))
                ->options(array(
                    // sendno: 表示推送序号,纯粹用来作为 API 调用标识,
                    // API 返回时被原样返回,以方便 API 调用方匹配请求与返回
                    // 这里设置为 100 仅作为示例

                    // &#39;sendno&#39; => 100,

                    // time_to_live: 表示离线消息保留时长(秒),
                    // 推送当前用户不在线时,为该用户保留多长时间的离线消息,以便其上线时再次推送。
                    // 默认 86400 (1 天),最长 10 天。设置为 0 表示不保留离线消息,只有推送当前在线的用户可以收到
                    // 这里设置为 1 仅作为示例

                    // &#39;time_to_live&#39; => 1,

                    // apns_production: 表示APNs是否生产环境,
                    // True 表示推送生产环境,False 表示要推送开发环境;如果不指定则默认为推送生产环境

                    &#39;apns_production&#39; => $production_mode,

                    // big_push_duration: 表示定速推送时长(分钟),又名缓慢推送,把原本尽可能快的推送速度,降低下来,
                    // 给定的 n 分钟内,均匀地向这次推送的目标用户推送。最大值为1400.未设置则不是定速推送
                    // 这里设置为 1 仅作为示例

                    // &#39;big_push_duration&#39; => 1
                ))
                ->send();
            print_r($response);

        } catch (\JPush\Exceptions\APIConnectionException $e) {
            // try something here
            print $e;
        } catch (\JPush\Exceptions\APIRequestException $e) {
            // try something here
            print $e;
        }

    }

    static function pushAll($ticket=&#39;消息提醒&#39;,$alert){
        $appKey=Yii::app()->params[&#39;push&#39;][&#39;AppKey&#39;];
        $appMasterSecret=Yii::app()->params[&#39;push&#39;][&#39;AppMasterSecret&#39;];
        $production_mode=Yii::app()->params[&#39;push&#39;][&#39;production_mode&#39;];



        $client = new JPush($appKey, $appMasterSecret);


// 完整的推送示例
// 这只是使用样例,不应该直接用于实际生产环境中 !!
        try {
            $response = $client->push()
                ->setPlatform(array(&#39;ios&#39;, &#39;android&#39;))
                ->setAudience(&#39;all&#39;)
                // 一般情况下,关于 audience 的设置只需要调用 addAlias、addTag、addTagAnd  或 addRegistrationId
                // 这四个方法中的某一个即可,这里仅作为示例,当然全部调用也可以,多项 audience 调用表示其结果的交集
                // 即是说一般情况下,下面三个方法和没有列出的 addTagAnd 一共四个,只适用一个便可满足大多数的场景需求
                ->setNotificationAlert($alert)
                ->iosNotification($ticket, array(
                    &#39;sound&#39; => &#39;sound.caf&#39;,
                    // &#39;badge&#39; => &#39;+1&#39;,
                    // &#39;content-available&#39; => true,
                    // &#39;mutable-content&#39; => true,
                    &#39;category&#39; => &#39;jiguang&#39;,
                    &#39;extras&#39; => array(
                        &#39;key&#39; => &#39;value&#39;,
                        &#39;jiguang&#39;
                    ),
                ))
                ->androidNotification($ticket, array(
                    &#39;title&#39; => $alert,
                    // &#39;build_id&#39; => 2,
                    &#39;extras&#39; => array(
                        &#39;key&#39; => &#39;value&#39;,
                        &#39;jiguang&#39;
                    ),
                ))
                ->message($alert, array(
                    &#39;title&#39; => $alert,
                    // &#39;content_type&#39; => &#39;text&#39;,
                    &#39;extras&#39; => array(
                        &#39;key&#39; => &#39;value&#39;,
                        &#39;jiguang&#39;
                    ),
                ))
                ->options(array(
                    // sendno: 表示推送序号,纯粹用来作为 API 调用标识,
                    // API 返回时被原样返回,以方便 API 调用方匹配请求与返回
                    // 这里设置为 100 仅作为示例

                    // &#39;sendno&#39; => 100,

                    // time_to_live: 表示离线消息保留时长(秒),
                    // 推送当前用户不在线时,为该用户保留多长时间的离线消息,以便其上线时再次推送。
                    // 默认 86400 (1 天),最长 10 天。设置为 0 表示不保留离线消息,只有推送当前在线的用户可以收到
                    // 这里设置为 1 仅作为示例

                    // &#39;time_to_live&#39; => 1,

                    // apns_production: 表示APNs是否生产环境,
                    // True 表示推送生产环境,False 表示要推送开发环境;如果不指定则默认为推送生产环境

                    &#39;apns_production&#39; => $production_mode,

                    // big_push_duration: 表示定速推送时长(分钟),又名缓慢推送,把原本尽可能快的推送速度,降低下来,
                    // 给定的 n 分钟内,均匀地向这次推送的目标用户推送。最大值为1400.未设置则不是定速推送
                    // 这里设置为 1 仅作为示例

                    // &#39;big_push_duration&#39; => 1
                ))
                ->send();
            print_r($response);

        } catch (\JPush\Exceptions\APIConnectionException $e) {
            // try something here
            print $e;
        } catch (\JPush\Exceptions\APIRequestException $e) {
            // try something here
            print $e;
        }

    }
}

三、呼叫NotifyPush.php裡的方法,實作推送,如下程式碼:

class CronNotifyPushCommand extends  CConsoleCommand{
    public $keys=array();
    public function init(){
        parent::init();
    }

    public function actionIndex(){
        echo 'start CronNotifyPushCommand '.chr(10);
        if(!Yii::app()->params['push']['push_status']){
            echo 'push status disabled';die();
        }
        $rkey='message_notify_list';
        $waitTotals=Fredis::model()->redis->lsize($rkey);
        echo 'wait totals:'.$waitTotals.chr(10);
        $waitResult=true;
        $i=0;
        while($waitResult) {$i++;
            echo $i.'/'.$waitTotals.' wait to do'.chr(10);
            $waitResult=Fredis::model()->redis->rpop($rkey);
            if(!$waitResult) {
                continue;
            }
            $db_data=unserialize($waitResult);var_dump($db_data);
            $message_content=$db_data['message_content'];
            $uid=$db_data['uid'];
            $alias=$uid;
            if($uid==0){
                NotifyPush::pushAll($message_content, $message_content);
            }else {
                NotifyPush::pushAlias($alias, $message_content, $message_content);
            }
        }

        echo 'end'.chr(10);
    }
}
 相關教學:

PHP影片教學

以上是Yii1.1框架模擬PHP極光推播訊息通知的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:博客园。如有侵權,請聯絡admin@php.cn刪除
可以在PHP會話中存儲哪些數據?可以在PHP會話中存儲哪些數據?May 02, 2025 am 12:17 AM

phpsessionscanStorestrings,數字,數組和原始物。

您如何開始PHP會話?您如何開始PHP會話?May 02, 2025 am 12:16 AM

tostartaphpsession,usesesses_start()attheScript'Sbeginning.1)placeitbeforeanyOutputtosetThesessionCookie.2)useSessionsforuserDatalikeloginstatusorshoppingcarts.3)regenerateSessiveIdStopreventFentfixationAttacks.s.4)考慮使用AttActAcks.s.s.4)

什麼是會話再生,如何提高安全性?什麼是會話再生,如何提高安全性?May 02, 2025 am 12:15 AM

會話再生是指在用戶進行敏感操作時生成新會話ID並使舊ID失效,以防會話固定攻擊。實現步驟包括:1.檢測敏感操作,2.生成新會話ID,3.銷毀舊會話ID,4.更新用戶端會話信息。

使用PHP會話時有哪些性能考慮?使用PHP會話時有哪些性能考慮?May 02, 2025 am 12:11 AM

PHP会话对应用性能有显著影响。优化方法包括:1.使用数据库存储会话数据,提升响应速度;2.减少会话数据使用,只存储必要信息;3.采用非阻塞会话处理器,提高并发能力;4.调整会话过期时间,平衡用户体验和服务器负担;5.使用持久会话,减少数据读写次数。

PHP會話與Cookie有何不同?PHP會話與Cookie有何不同?May 02, 2025 am 12:03 AM

PHPsessionsareserver-side,whilecookiesareclient-side.1)Sessionsstoredataontheserver,aremoresecure,andhandlelargerdata.2)Cookiesstoredataontheclient,arelesssecure,andlimitedinsize.Usesessionsforsensitivedataandcookiesfornon-sensitive,client-sidedata.

PHP如何識別用戶的會話?PHP如何識別用戶的會話?May 01, 2025 am 12:23 AM

phpIdentifiesauser'ssessionSessionSessionCookiesAndSessionId.1)whiwsession_start()被稱為,phpgeneratesainiquesesesessionIdStoredInacookInAcookInAcienamedInAcienamedphpsessIdontheuser'sbrowser'sbrowser.2)thisIdallowSphptpptpptpptpptpptpptpptoretoreteretrieetrieetrieetrieetrieetrieetreetrieetrieetrieetrieetremthafromtheserver。

確保PHP會議的一些最佳實踐是什麼?確保PHP會議的一些最佳實踐是什麼?May 01, 2025 am 12:22 AM

PHP會話的安全可以通過以下措施實現:1.使用session_regenerate_id()在用戶登錄或重要操作時重新生成會話ID。 2.通過HTTPS協議加密傳輸會話ID。 3.使用session_save_path()指定安全目錄存儲會話數據,並正確設置權限。

PHP會話文件默認存儲在哪裡?PHP會話文件默認存儲在哪裡?May 01, 2025 am 12:15 AM

phpsessionFilesArestoredIntheDirectorySpecifiedBysession.save_path,通常是/tmponunix-likesystemsorc:\ windows \ windows \ temponwindows.tocustomizethis:tocustomizEthis:1)useession_save_save_save_path_path()

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用