当我的应用程序在 Ionic 中关闭时,我如何发送通知?
How i can send notifications when my app is closed in Ionic?
我正在使用 Ionic 进行移动开发,实际上我正在使用 LocalNotifications
如果有新问题,我每 5 分钟检查一次我的服务器:
this.checkQuestions();
this.IntervalId = setInterval(() => {
this.checkQuestions();
}, 300000);
我的函数 checkQuestions()
使用我服务器的数据创建 for()
:
for (var i = 0; i < res.data.notificar.questions.length; i++) {
this.localNotifications.schedule({
id: i,
priority: 2,
text: 'Produto: ' + res.data.notificar.questions[i].produto.substring(0, 20) + '...',
title: 'Nova pergunta, conta: ' + res.data.notificar.questions[i].conta,
smallIcon: 'res://notification',
foreground: true,
});
}
问题是当我的应用程序关闭时,此逻辑不会 运行 并且我的客户不会收到通知。当我的应用程序关闭时,Ionic 中有一些替代方法可以发送通知吗?
我需要每 5 分钟检查一次服务器,即使应用已关闭。
如果您的用户终止了该应用程序,您将无法确保您的用户保持您的应用程序处于活动状态。但如果你真的想尝试,你可以使用 this。
最有效的是使用Push Notifications。您的服务器可以在存储新数据时向您的应用程序发送通知。
编辑
服务器端你可以 运行 一个发送推送通知的函数,像这样:
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 );
}
?>
运行 如果需要,此功能每 5 分钟在 php 中运行一次,但在存储新数据时效果更好。
并且,在 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
});
});
}
}
我正在使用 Ionic 进行移动开发,实际上我正在使用 LocalNotifications
如果有新问题,我每 5 分钟检查一次我的服务器:
this.checkQuestions();
this.IntervalId = setInterval(() => {
this.checkQuestions();
}, 300000);
我的函数 checkQuestions()
使用我服务器的数据创建 for()
:
for (var i = 0; i < res.data.notificar.questions.length; i++) {
this.localNotifications.schedule({
id: i,
priority: 2,
text: 'Produto: ' + res.data.notificar.questions[i].produto.substring(0, 20) + '...',
title: 'Nova pergunta, conta: ' + res.data.notificar.questions[i].conta,
smallIcon: 'res://notification',
foreground: true,
});
}
问题是当我的应用程序关闭时,此逻辑不会 运行 并且我的客户不会收到通知。当我的应用程序关闭时,Ionic 中有一些替代方法可以发送通知吗?
我需要每 5 分钟检查一次服务器,即使应用已关闭。
如果您的用户终止了该应用程序,您将无法确保您的用户保持您的应用程序处于活动状态。但如果你真的想尝试,你可以使用 this。
最有效的是使用Push Notifications。您的服务器可以在存储新数据时向您的应用程序发送通知。
编辑
服务器端你可以 运行 一个发送推送通知的函数,像这样:
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 );
}
?>
运行 如果需要,此功能每 5 分钟在 php 中运行一次,但在存储新数据时效果更好。
并且,在 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
});
});
}
}