简体   繁体   中英

Ionic v2 : FCM open application in background

I build an Ionic 2 and I have to integrate push notification. First, I try to send my notification with firebase interface : https://console.firebase.google.com/ All works well.

But now, I want to send notification with a php server. So I found this code :

<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'my-key' );
$registrationIds = array( $_GET['id'] );
// prep the bundle
$msg = array
(
'message'=> 'here is a message. message',
'title'=> 'This is a title. title',
'subtitle'=> 'This is a subtitle. subtitle',
'tickerText'=> 'Ticker text here...Ticker text here...Ticker text here',
'vibrate'=>1,
'sound'=>1,
'largeIcon'=>'large_icon',
'smallIcon'=>'small_icon',
'content-available'=>1
);
$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://fcm.googleapis.com/fcm/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 );
echo $result;
?>

It works. But when my app is in background (onpause), I received a notification but touch it don't open my app in foreground. It works well when my app is closed To resume :

-If my App is close, I received notification. When I touch the notification my app is open.

-If my App is open and in foreground, I received notification.

-If my app is on pause, open but in background, I received notification but touch it doesn't open the app . But I want to.

I don't understand because it works with firebase console... What I have to do to resolve that ?

My call back code in app.component.ts :

this.push.rx.notification()
          .subscribe((msg) => {
            console.log('I received awesome push: ' + msg);
          });

UPDATE :

When I touch my notification, if my app is "onpause", she goes into forground and back directly into background again. I don't know why and I don't know what I have to do...

In the PHP code, you're sending a data - only message payload, while when using the Firebase Console, the message(s) you sent are considered as a notification message payload (see Message Types ).

Try editing your PHP code to send a notification message payload instead. Like so:

 // prep the bundle
$msg = array
(
    'title'=> 'This is a title',
    'body'=> 'This is a body'
);
$fields = array
('registration_ids'=>$registrationIds,
    'notification'=>$msg
);

However, do note that notification message payload only have specific parameters available. See the HTTP Protocol Reference as a guide for the parameters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM