ionic 2 push notification not received when app is closed or in background

时间:2018-02-01 18:35:47

标签: ionic2 phonegap-pushplugin

When the app is open : Push notification is Okay

When the app is in background : Push notification vibrate and sound but not showing on notification bar

When the app is closed : notifications doesn't received

Here is my push notification initialization

  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 is my backend sender:

// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'accesskeyhere' );
// device ids
$registrationIds = array('mydeviceidhere');
// prep the bundle
$msg = array
(
    'message' => "This is a Test Notification",
    'title' => "This is a Test Title",
    'addData' => "This is Test Additional Data",
    '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 am I doing wrong? or there is something lacking in my code?

How to wake up GCM Service when app is closed/in background using ionic 2? Is anybody here has same issue before and had it fixed? please share your answers. Thankyou