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

Ok so I've implemented the code and it works fine for Android I'm receiving the push notification perfectly however it's not delivering to iOS. I've made all possible code modification with no luck to reach a perfect solution.

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);
    }   

}
This Firebase API is installed for Opencart 2.x to allow Administrators to send Push Notification from their backend. Push Notification for Android works fine but not for iOS. And also using Firebase console I'm able to send notifications for both Android and iOS.

Newbie

Posts

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

Users browsing this forum: No registered users and 3 guests