Saturday, July 13, 2019

Swift - Notify ViewController from AppDelegate On FCM / APN

Hello,

Recently I tried  my hands on iOS app after long time as I had to convert one of our hybrid app to native app on urgent basis. There I had webview where I was loading my html app. On FCM message receieved we need to send URL received in FCM to webview and load the URL.

So here in this blog I will mention the procedure for it.

First of all in AppDelegate we have didReceiveRemoteNotification method. There we will create notification for it. Here is the code.


let url = dict
serverURL = url;
let notificationName = Notification.Name("updateWebView")
NotificationCenter.default.post(name: notificationName, object: nil)

Now in the ViewController where you want to make updates, subscribe to this notification in viewDidLoad function.

override func viewDidLoad() {
  let notificationName = Notification.Name("updateWebView")
  NotificationCenter.default.addObserver(self, selector:            #selector(ViewController.updateWebView), name: notificationName, object: nil)
}

And then add function in ViewController.

@objc func updateWebView() {
  let appDelegate = UIApplication.shared.delegate as! AppDelegate
  let serverURL = appDelegate.serverURL
        
  guard let url = URL(string: serverURL!) else {
     print("Invalid URL")
     return
  }
        
  let request = URLRequest(url: url)
  webView.load(request)
}

That's it and now you will have data passed from AppDelegate to ViewController. This method you can pass any kind of data from FCM to respective ViewController.

Hope this helps you.



Sunday, May 5, 2019

Laravel Mail Queue Get Receiver User

Hello,

Recently in one my Laravel project faced an issue where in Mail we have to change content dynamically based on receiver. As we know that when we use Mailable class and mail queue, mail will not sent immediately but it will be processed with Mail Queue. Here we don't have Auth Session or any other information to get receiver user.

To solve this I used following trick.

In mailable class if you check $this variable it will give all such information like this.

from-> => "norply@xyz.com"
address-> : Array[
    0 => Array[
          "address" => "hdave10@gmail.com"
    ]
]

From here you can access email address like this

$emailOfReceiver = $this->to[0]["address"];

And using this you can find user object from Auth\User model or your own model which you used for user information in your laravel project. For my case I was using EmployeeMaster model to get additional information.

Following line I have used in one my Mailable class.

$employee = EmployeeMaster::where("emp_email",$this->to[0]["address"])->first();

Now with this I can write my logic to dynamically change the content of mail.

Hope this helps you.

Wednesday, February 13, 2019

Laravel - Connect With Multiple Database

Hello,

In this blog post I am going to mention how we can connect multiple databases with Laravel applications. First add multiple connections in config/database.php For this copy paste mysql connection and change configurations as following.

'mysql1' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE_1',''),
            'username' => env('DB_DATABASE_1_USERNAME',''),
            'password' => env('DB_DATABASE_1_PASSWORD',''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],

Now add these values in .env file

DB_DATABASE_1=YOUR_DB_NAME
DB_DATABASE_1_USERNAME =YOUR_USER_NAME
DB_DATABASE_1_PASSWORD = YOUR_PASSWORD

Now laravel give you two ways to switch database connection.

1) Set connection property in Eloquent Model

protected $connection = 'mysql1';

So here when you use this model to get data it will use the connection specified in the mysql1. You don't need to manually change it. 

2) use DB:connection 

If you are using raw queries and using DB facade so to get query the data. You have to use following logic.

DB::connection('mysql1')->table(env('DB_DATABASE_1','')).'.TABLE_NAME')


So here first you have specified connection and then used table by appending db name in front of it. 

So this is how you can manage multiple databases.