1

I'm not receiving push notification when app is closed. But it works when app is in background/foreground.

Here's part of my code inside app.component.ts

  initPushNotification()
  {
    // to initialize push notifications
    const options: PushOptions = {
       android: {
          icon: 'small-icon',
          forceShow: true
       },
       ios: {
          alert: 'true',
          badge: true,
          sound: 'false'
       },
       windows: {}
    };
    const pushObject: PushObject = this.push.init(options);
    pushObject.on('notification').subscribe((notification: any) => {
      //Notification Display Section
      alert(JSON.stringify(notification));
    });
    pushObject.on('registration').subscribe((registration: any) => {
      alert(registration.registrationId);
    });
    pushObject.on('error').subscribe(error => {
      alert('Error with Push plugin' + error);
    });
  }

Here's my php code for sending push notification

<?php
    public function androidNotification($title, $message, $registrationIds, $addData) {
        // API access key from Google API's Console
        define( 'API_ACCESS_KEY', 'accesskeyhere' );
        // prep the bundle
        $msg = array
        (
            'message' => $message,
            'title' => $title,
            'addData' => $addData,
            'vibrate' => 1,
            'sound' => 1,
            'largeIcon' => 'large_icon',
            'smallIcon' => 'small_icon'
        );
        $fields = array
        (
            'registration_ids' => $registrationIds,
            'data' => $msg
        );

        $headers = array
        (
            'Authorization: key=' . API_ACCESS_KEY,
            'Content-Type: application/json'
        );
        $ch = curl_init();
        curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
        curl_setopt( $ch,CURLOPT_POST, true );
        curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
        curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
        curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
        curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
        $result = curl_exec($ch );
        curl_close( $ch );
    }

What is the problem here? I'm using ionic platform.

I'm searching for forums, but no luck. I didn't found any solution.

Codeblooded Saiyan
  • 1,457
  • 4
  • 28
  • 54

1 Answers1

0

You may refer with this thread. Try changing your priority. It is stated in this page that setting priority to 2 had it working.

abielita
  • 13,147
  • 2
  • 17
  • 59