Ionic v2:FCM在后台打开应用程序

时间:2017-07-06 09:51:05

标签: android firebase notifications ionic2 firebase-cloud-messaging

我构建了Ionic 2,我必须整合推送通知。 首先,我尝试使用firebase接口发送通知: https://console.firebase.google.com/ 一切正常。

但是现在,我想通过php服务器发送通知。所以我找到了这段代码:

var x = $('#datatab').DataTable({
            "bServerSide": true,
            "ajax": {
                "beforeSend":AjaxBegin,
                "type": "POST",
                "url": '/Totto/GetZScgData',
                "contentType": 'application/json; charset=utf-8',
                'data': function(data) { return data = JSON.stringify(data); },
                'complete': AjaxComplete
            },

function AjaxBegin() {
    $('#myPleaseWait').modal('show');

}
function AjaxComplete() {
    $('#myPleaseWait').modal('hide');

}

有效。但是当我的应用程序处于后台(暂停)时,我收到了通知,但触摸它不会在前台打开我的应用程序。 我的应用程序关闭时效果很好 要恢复:

- 如果我的应用程序已关闭,我会收到通知。当我触摸通知时,我的应用程序已打开。

- 如果我的应用程序处于打开状态且处于前台,我收到了通知。

- 如果我的应用暂停,但在后台打开,我收到通知但触摸它不会打开应用。但我想。

我不明白因为它适用于firebase控制台...... 我需要做些什么才能解决这个问题?

我在app.component.ts中的回调代码:

<?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;
?>

更新:

当我触摸我的通知时,如果我的应用程序是“暂停”,则她会进入forground并再次直接返回后台。我不知道为什么,我不知道自己要做什么......

1 个答案:

答案 0 :(得分:2)

在PHP代码中,您发送的是data - 消息有效负载,而在使用Firebase控制台时,您发送的消息被视为{{ 1}}消息有效负载(请参阅Message Types)。

尝试编辑PHP代码以发送notification消息有效负载。像这样:

notification

但是,请注意 // prep the bundle $msg = array ( 'title'=> 'This is a title', 'body'=> 'This is a body' ); $fields = array ('registration_ids'=>$registrationIds, 'notification'=>$msg ); 消息有效负载仅具有可用的特定参数。请参阅HTTP Protocol Reference作为参数指南。