如果您的用户终止了该应用程序,您将无法确保您的用户保持您的应用程序处于活动状态。但如果你真的想尝试你可以使用this https://ionicframework.com/docs/native/background-mode/.
最有效的是使用推送通知 https://devdactic.com/ionic-push-notifications-guide/。当存储新数据时,您的服务器可以向您的应用程序发送通知。
EDIT
服务器端您可以运行一个函数来发送推送通知,如下所示:
function sendGCM($message, $id) {
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array (
'registration_ids' => array (
$id
),
'data' => array (
"message" => $message
)
);
$fields = json_encode ( $fields );
$headers = array (
'Authorization: key=' . "YOUR_KEY_HERE",
'Content-Type: application/json'
);
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );
$result = curl_exec ( $ch );
echo $result;
curl_close ( $ch );
}
?>
如果您愿意,可以在 php 中每 5 分钟运行一次此函数,但在存储新数据时效果更好。
SOURCE https://stackoverflow.com/questions/37371990/how-can-i-send-a-firebase-cloud-messaging-notification-without-use-the-firebase
而且,Ionic 方面,您可以在捕获推送通知时执行一个函数来获取数据。有点像这样:
import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar, Splashscreen } from 'ionic-native';
import { HomePage } from '../pages/home/home';
import {
Push,
PushToken
} from '@ionic/cloud-angular';
@Component({
templateUrl: 'app.html'
})
export class MyApp {
rootPage = HomePage;
constructor(platform: Platform, public push: Push) {
platform.ready().then(() => {
StatusBar.styleDefault();
Splashscreen.hide();
this.push.register().then((t: PushToken) => {
return this.push.saveToken(t);
}).then((t: PushToken) => {
console.log('Token saved:', t.token);
});
this.push.rx.notification()
.subscribe((msg) => {
// CALL SERVE TO GET DATA
});
});
}
}
SOURCE https://devdactic.com/ionic-2-push-notifications/