[SOLVED] Not receiving notifications sent from server on iOS


#1

Hello,

I’m having troubles to receive the notifications sent from my own server on iOS (not Ionic 2 app panel). Android is working perfect but iOS receive notifications only sent with Ionic Push Notifications (the Ionic app panel).

I’m saving the device token to my database but I receive “InvalidRegistration” error. Maybe that’s the cause.

I have all the certificates on the Apple Developer Panel and configured on the Ionic App Settings.

Any have any idea what can be? I think the most probably is my certificates are wrong configured, but I think if that was the cause, I wouldn’t receive notifications from Ionic Panel.

Thanks.

EDIT: Solution -> [SOLVED] Not receiving notifications sent from server on iOS


#2

What API exactly are you calling and sending there?
What exactly is the error you are getting?


#3

It’s a personal server, made with Wordpress, with API endpoint created by me. Like this:

saveToken(token): any {
    var link = 'http://urlhere.com/wp-json/wp/v2/save_devices_into_ddbb';
    var data = new FormData();
    data.append('registration_id', token);
    this.http.post(link, data)
      .subscribe(data2 => {
      }, error => {
        console.log("Error!");
      });
  }

And the post is working, token is saving on the database (it’s saving all devices iOS and Android), but the server response throws that error in console only when post an iOS token (I think that can be the cause):

“{“multicast_id”:9134525152966159203,“success”:1,“failure”:2,“canonical_ids”:0,“results”:[{“error”:“InvalidRegistration”},{“error”:“InvalidRegistration”},{“message_id”:“0:1493113965871838%8d5593448d559344”}]}”


#4

So I ask again, what API are you calling with the iOS token trying to send a push?


#5

I’m using the phonegap-plugin-push by Cordova to send notifications.


#6

If you say so, but normally the Cordova Plugin is used to receive pushes. Can you paste the code you use to send pushes please?


#7

Ok, I’m sending notifications with Firebase. I’m receiving only the pushes sended with Ionic panel, and custom notifications on Android, but I’m not receiving my custom notifications on iOS, so this means that certifications are well configured (I think).

This is my code:

             $notifTitle = $notification_post->notification_title; //Notification title
             $notifType = $notification_post->notification_type;
             $notifText = $notification_post->notification_text; // Notification text
            $notifProduct = ($notifType  == "product") ? $notification_post->notification_product : ""; //ID product

            if($notifTitle != null && $notifTitle != "" && $notifType != null) {

                if ( ! defined('API_ACCESS_KEY') ) {
                    define( "API_ACCESS_KEY", "API_ACCES_KEY_HERE" );
                }

                if ( ! defined('GOOGLE_FCM_URL') ) {
                    define( "GOOGLE_FCM_URL", "https://fcm.googleapis.com/fcm/send" );
                }

                $devices_data = $wpdb->get_results( "SELECT * FROM wp_devices_notifications where status = 1" );
                
                foreach ($devices_data as $data) {
                     array_push($registrationIds, $data->registration_id);
                 }

                $bodyArray = array(
                    'type'     => $notifType,
                    'text'     => ($notifText != null && $notifText != "") ? $notifText : "",
                    'product'     =>  ($notifProduct != null && $notifProduct != "")  ? $notifProduct : "",
                    );

                $body = json_encode($bodyArray);

                $fields = array(
                    'registration_ids'     => $registrationIds,
                    'priority'            => "high",
                    'notification'      => array( "title" => $notifTitle, "body" => $body, 'sound'    => 'default' ),
                    'data'                 => array('message' => $notifProduct)
                );

                $headers = array(
                    'Authorization: key=' . API_ACCESS_KEY,
                    'Content-Type: application/json'
                );

                $ch = curl_init();
                curl_setopt( $ch, CURLOPT_URL, GOOGLE_FCM_URL );
                curl_setopt( $ch, CURLOPT_POST, true );
                curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
                curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
                curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
                curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
                curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );

                $result = curl_exec( $ch );
                if ( $result === FALSE ) {
                   $result = curl_error( $ch );
                }

                curl_close( $ch );

                // echo $result;
                return $result;
            }
            else
            {
                    return new WP_Error( 'push_notification - functions-api.php', 'Error -  some error in notifTitle or in notifType ', array( 'status' => 500 ) );
            }

PD: Sorry for my late response, my laptop has been broken one week :frowning:

Thanks!


#8

I solved my problem, the error was I was sending notifications to wrong API (FCM). Now I’m sending notifications to Ionic Api, passing my API Key, generated in the Ionic App Dashboard. Here the code:

/* Notification  */
function push_notification($notification_id) {

	if ( ! defined('API_ACCESS_KEY') ) {
		define( "API_ACCESS_KEY", "Firebase SENDER ID" );
	}

	if ( ! defined('GOOGLE_FCM_URL') ) {
		define( "GOOGLE_FCM_URL", "https://api.ionic.io/push/notifications" );
	}

	global $wpdb;
	$message = "New push";
	$title = "Hey, listen!";
	$registrationIds = array();

	$devices_data = $wpdb->get_results( "SELECT * FROM wp_devices_notifications where status = 1" );

	foreach ($devices_data as $data) {
		array_push($registrationIds, $data->registration_id);
	}


    $fields = array(
		'tokens' 	=> $registrationIds,
		'profile' 	=>'<tag here>' (you can find id in Ionic Dashboard -> Settings -> Certificates),
        'notification'      => array( "title" => $title, "message" => $message )
    );

    var_dump($fields);
    $headers = array(
        'Authorization: Bearer <here App Token, generated in Ionic Dashboard -> App -> Settings -> API Keys>,
        'Content-Type: application/json'
    );

    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_URL, GOOGLE_FCM_URL );
    curl_setopt( $ch, CURLOPT_POST, true );
    curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
    curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
    curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );

    $result = curl_exec( $ch );
    if ( $result === FALSE ) {
       $result = curl_error( $ch );
    }

    curl_close( $ch );

    return $result;
}

I hope this helps.

Greetings!