Sunday, 30 December 2018

Zend Framework 2 Session Management - Add Update Delete Session

Zend Framework 2 Session Management - Add Update Delete Session


Question: What is Zend Session?
A Session is a way to store information (in variables) to be used across multiple pages. It is mainly used to check for login information on browser and track the user activies. Zend Session is Zend libery for Session which helps to manage the session data.


Question: What is difference between Session Manager and Session Container?
Zend\Session\Container instances provide the primary API for manipulating session data. Containers are used to segregate all session data. Zend\Session\SessionManager, is a class that is responsible for all aspects of session management. It is used to initializes and configuration, storage and save handling.


Question: What is difference between Session Manager and Session Container and Session Config in One line each?
Container: The provides the core interface for managing session data.
SessionConfig: Handles setting for session config options.
SessionManager: Handles session management such as session start/exists/write/regenerate id/time-to-live/and destroy and session validation.


Question: Can you store session data in database?
Yes, we can.
For this we need to set the configuration like below:
use Zend\Db\TableGateway\TableGateway;
use Zend\Session\SaveHandler\DbTableGateway;
use Zend\Session\SaveHandler\DbTableGatewayOptions;
use Zend\Session\SessionManager;

$tableGateway = new TableGateway('session', $adapter); //this is table where session will store, you need to create the table and its model also.
$saveHandler  = new DbTableGateway($tableGateway, new DbTableGatewayOptions());
$manager      = new SessionManager();
$manager->setSaveHandler($saveHandler);


Question: How to set the Defualt Session Mangaer for Session Container?
use Zend\Session\Container;
use Zend\Session\SessionManager;

$manager = new SessionManager();
Container::setDefaultManager($manager);



Question: How to create session Object?
use Zend\Session\Container;
$sessionObj = new Container('sessinName');



Question: How to set Session value?
$sessionObj->offsetSet('email', 'email@domain.com');



Question: How to get Session value?
$sessionObj->offsetGet('email');



Question: How to check Session value exist OR Not?
$sessionObj->offsetExists('email');



Question: How to unset the session value?
$sessionObj->offsetUnset('email');



Sunday, 23 December 2018

FCM push notification example

FCM push notification example

Question: What is FCM Messages?
Full form of FCM is Firebase Cloud Messaging.
FCM is a cross-platform messaging solution that lets you reliably deliver messages to android and IOS devices at no cost.
You can notify a client app that new message has been arrived.



Question: How does FCM works?
App server interacts with FCM (HTTP or XMPP protocol) and a client app. You can compose and send messages.
FCM built on Firebase Cloud Messaging.


Question: What is FCM Server?
FCM servers take messages from an app server and send to cient app.
FCM connection servers provided by Google.


Question: What is App Server?
App Server sends data to a client app (via the chosen FCM connection server) using XMPP or HTTP protocol.



Question: What are role of App Server?
  • Communicate with your client
  • Send properly formatted requests to the FCM connection server
  • Securely store the Server key and client registration tokens



Question:How to send notification to android/IOS device with FCM server?
$jsonData='{
  "to": "dhkC5_jTnck:APA91bF7L_KN7GFOPMiVsykjMGqydlftItfapC4Va63hRBuhFCrRKMdUonqhO1qjKT5_OYPMRAZFMA2UDVA1spAw9SpCsLE7B05vGtNttvuykIdxL7jKWWvJkA51T98O8ZZkubhXwQyRx",
  "notification": {
    "body": "New Gr sent you a message",
    "title": "domain.com ",
    "icon": "main_ve_logo",
    "click_action": "com.domain.fcm"
  },
  "data": {
    "type": "message",    
    "avatar": "http://img.domain.com/upload/default.jpg",
    "message_id": 1
  }
}';
$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => "https://fcm.googleapis.com/fcm/send",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => json_encode($fields),
    CURLOPT_HTTPHEADER => array(
      "authorization: key=FCM_SERVER_KEY",
      "cache-control: no-cache",
      "content-type: application/json"
    ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);