No push notification on Android excepted from ionic.io

Hi,

I’ve been testing my app on an iPad and a Huawei smartphone (Android).

I use the plugin Push from @ionic/cloud-angular to send notifications when a new article is published on our website.

For some reason, when I use the https://apps.ionic.io Push form, both of my devices receive the notifications. But when I use the Ionic API with a PHP script on my website, only the iPad receive the notification. I have no idea why, maybe something is wrong in my PHP script ?


            $apiurl = 'https://api.ionic.io/push/notifications';
            
            $last_post = $wpdb->get_results("SELECT * FROM wp_posts WHERE post_status = 'publish' ORDER BY post_date DESC")[0];
            $post_title = $last_post->post_title;
            $post_type = $last_post->post_type;
            $post_id = $last_post->ID;
            
            $data = array(
              'tokens' => $tokens,
              'profile' => 'development',
              'notification' => array(
                'title' => $post_title,
                'message' => $post_title,
                "payload" => array(
                  "type" => $post_type,
                  "id" => $post_id
                ),
                'ios' => array(
                  'sound' => 'default',
                  "content_available" => 1
                ),
                'android' => array(
                  'sound' => 'default',
                  'image' => 'www/img/icon.png',
                  "content_available" => 1
                )
              )
            );
            
            
            $method = "POST";
            
            $content = send_ionic_api( $apiurl, $method, $data );

  function send_ionic_api( $apiurl, $method, $data ){
    
      $ch = curl_init();
  
      curl_setopt($ch,CURLOPT_URL, $apiurl); 
      curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-Type: application/json', 'Authorization: Bearer MY_API_KEY')); 
      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); 
      curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $data ));
      curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); 
      
      $content = curl_exec($ch);
      
      curl_close($ch);
      
      return $content;
          
  }

I’ve checked :

  • the tokens (they’re valid),
  • the notifications (they’re marked as “sent”, no error, for both devices)
  • the phone (notifications are activated and there is an Internet connexion)

I don’t know what to do next.

Thanks in advance for your help.

Can you show us the final “data” that is generated?
What does the call return? Do you get a response?

Thanks for your reply Sujan.

The endpoint https://api.ionic.io/push/notifications returned :

{
  "data": {
    "config": {
      "profile": "development",
      "tokens": [
        "082f8d308e6e51aad65c9acd70788e7f1dXXXXXXXXXXXXXXXX",
        "eBB1PckyrVs:APA91bF6ZkHfibM_WP6u5lLu_KmJ7fopFTSeQhxvNPxMg4UT43Ij4YqUXXXXXXXXXXXXXXXXXXXXXXXXXX"
      ],
      "notification": {
        "ios": {
          "sound": "default",
          "content_available": 1
        },
        "title": "Utilisateur",
        "payload": {
          "id": 933,
          "type": "actualite"
        },
        "message": "Utilisateur",
        "android": {
          "image": "www/img/icon.png",
          "sound": "default",
          "content_available": 1
        }
      }
    },
    "state": "enqueued",
    "created": "2017-06-16T09:23:19.794492+00:00",
    "status": "open",
    "app_id": "XXXXXXXX",
    "uuid": "e78f106f-8495-424f-8ecf-XXXXXXXXXX"
  },
  "meta": {
    "version": "2.0.0-beta.0",
    "request_id": "eb1d4d38-46f3-4e8f-ce51-XXXXXXXXXX",
    "status": 201
  }
}

The endpoint https://api.ionic.io/push/notifications/:notification_id/messages returned :

  "meta": {
    "request_id": "1eaa940a-6c04-45bb-c511-XXXXXXXXXX",
    "status": 200,
    "version": "2.0.0-beta.0"
  },
  "data": [
    {
      "user_id": null,
      "status": "sent",
      "notification": "e78f106f-8495-424f-8ecf-XXXXXXXXXX",
      "token": {
        "type": "ios",
        "id": "9f2a0153bda27ef74e54afaXXXXXXXXXX",
        "app_id": "XXXXXXX",
        "token": "082f8d308e6e51aad65c9acd70788e7f1dXXXXXXXXXXXXXXXX",
        "invalidated": null,
        "valid": true,
        "created": "2017-06-09T09:46:42.786880+00:00"
      },
      "created": "2017-06-16T09:23:20.205472+00:00",
      "uuid": "4cf33cd0-49c3-4c5d-9d2e-XXXXXXXXXXX",
      "error": null
    },
    {
      "user_id": null,
      "status": "sent",
      "notification": "e78f106f-8495-424f-8ecf-XXXXXXXXXX",
      "token": {
        "type": "android",
        "id": "87628f3a2a7d02dcedfd2dXXXXXXXXXX",
        "app_id": "XXXXXXX",
        "token": "eBB1PckyrVs:APA91bF6ZkHfibM_WP6u5lLu_KmJ7fopFTSeQhxvNPxMg4UT43Ij4YqUXXXXXXXXXXXXXXXXXXXXXXXXXX",
        "invalidated": null,
        "valid": true,
        "created": "2017-06-15T08:48:28.740351+00:00"
      },
      "created": "2017-06-16T09:23:20.106749+00:00",
      "uuid": "159dd8e0-741e-4d6d-ae53-XXXXXXXXXX",
      "error": null
    }
  ]
}

Nah, the one your PHP scripts is sending to the API.

My script sends this :

{
  "tokens": [
    "082f8d308e6e51aad65c9acd70788e7f1d78a02c61cXXXXXXXXXXXXXXX",
    "eBB1PckyrVs:APA91bF6ZkHfibM_WP6u5lLu_KmJ7fopFTSeQhxvNPxMg4UT43Ij4YqUDlzZj_tcQzbmCtoAS-VkrtialwXR2bop763ctB535P7qxJ2wxAy-6QPwXXXXXXXXXXXXXXX"
  ],
  "profile": "development",
  "notification": {
    "title": "Utilisateur",
    "message": "Utilisateur",
    "payload": {
      "type": "actualite",
      "id": 933
    },
    "ios": {
      "sound": "default",
      "content_available": 1
    },
    "android": {
      "sound": "default",
      "image": "www/img/icon.png",
      "content_available": 1
    }
  }
}

Ok.

The API responses from /notifications and /messages match what you sent in?

Yes, they do. And the notification received on my iPad shows what I expected.

Ok sorry, no idea.

Maybe (include the link to this topic):
http://ionicframework.com/support#support
(Scroll down to “Business and Customer Support”)

I wrote to the ionic support. Thank you !