Post by c05338 » Sun Jun 18, 2017 8:14 pm

I've developed a code integrated for opencart where the admin is able to send Push Notification to the mobile from the backend via Firebase. So basically I'm trying to send Push Notification through Firebase for both Android and iOS. The code is working well for Android however it's not for iOS as I'm not receiving the push notification. I've created certs in Apple Development Portal and tested the Push Notification from Firebase itself it works like a charm receiving notifications in iOS and Android. However if I try to send it from Opencart it works for Android only.

The Controller code as defined below:

Code: Select all

	protected function sendNotification($notification_id) {
		
		$this->load->model('tool/image');

		$notification_info = $this->db->query("SELECT * FROM ". DB_PREFIX ."mobiapp_notification mn LEFT JOIN ". DB_PREFIX ."mobiapp_notification_description mnd ON (mn.notification_id = mnd.notification_id) WHERE mnd.language_id = '". $this->config->get('config_language_id') ."' AND mn.notification_id = '". $notification_id ."'")->row;
		

		$message = array(					
			'notification_id' => $notification_info['notification_id'],
			'title'		=> $notification_info['title'],
			'link'		=> $notification_info['link'],
			'content'	=> $notification_info['content'],
			'subTitle'	=> strip_tags(html_entity_decode($notification_info['content'])),
			'bannerImage' => $notification_info['image'],
			);

		// Set POST variables
		$gurl = 'https://fcm.googleapis.com/fcm/send';

		$fields = array(
			'to'			=> "/topics/global",
			'time_to_live'	=> 30,
			'notification' => array (
                    'data' => $message,
                    'priority' 		=> 'high',
                    'sound'  => 'default',
            ),
			//'data'			=> $message,
			
			
			'delay_while_idle'	=> true
		);

		$headers = array(
			'Authorization: key=' . $this->config->get('mobiapp_gcm_key'),
			'Content-Type: application/json'
		);

		// Open connection
		$ch = curl_init();

		// Set the url, number of POST vars, POST data
		curl_setopt($ch, CURLOPT_URL, $gurl);

		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

		// Disabling SSL Certificate support temporarly
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

		curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

		// Execute post
		$result = curl_exec($ch);
		if ($result === FALSE) {
			die('Curl failed: ' . curl_error($ch));
		}
		
		// Close connection
		curl_close($ch);

		$this->load->model('mobiapp/notification');

		if (isset(json_decode($result)->message_id)) {
			$message_id = json_decode($result)->message_id;
			$this->model_mobiapp_notification->sendNotification($message_id, $fields, $headers, $error = '');
		} elseif (isset(json_decode($result)->error)) {
			$error = json_decode($result)->error;
			$this->model_mobiapp_notification->sendNotification($message_id = '', $fields, $headers, $error);
		}	
		
	}
And here is the Model Code"

Code: Select all

    public function sendNotification($message_id, $fields, $headers, $error) {
    	$this->db->query("INSERT INTO " . DB_PREFIX . "mobiapp_send SET message_id = '" . $message_id . "', fields = '" . $this->db->escape(serialize($fields)) . "', headers = '" . $this->db->escape(serialize($headers)) . "', error = '" . $error . "'");
    }
I'm not sure if I missed anything so please can you advice and help me how I can resolve this issue I've been stuck for 2 weeks in this task.

Newbie

Posts

Joined
Tue Jan 10, 2012 10:09 pm
Who is online

Users browsing this forum: No registered users and 6 guests