【问题标题】:How i can send notifications when my app is closed in Ionic?当我的应用在 Ionic 中关闭时如何发送通知?
【发布时间】:2019-03-20 14:55:49
【问题描述】:

我正在使用 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 分钟检查一次服务器。

【问题讨论】:

  • 你可以使用推送通知吗?
  • 是的,但如果有新问题,我什至需要在服务器中检查 5 分钟,推送通知可以做到这一点吗?有什么例子吗?我只是找到了使用静态数据的推送通知示例。
  • facebook 使用这些来通知您。它们在移动设备和桌面设备之间无缝连接。查了一下,还不错。 developers.google.com/web/fundamentals/push-notifications
  • 帮不上忙,因为我需要一种离子溶液...

标签: ionic-framework localnotification


【解决方案1】:

如果您的用户终止了应用程序,您无法确保您的用户保持您的应用程序处于活动状态。但如果你真的想尝试,你可以使用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 );
}

?>

如果您愿意,可以在 php 中每 5 分钟运行一次此函数,但在存储新数据时会更好。

SOURCE

而且,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

【讨论】:

  • 我正在尝试推送通知,但我没有找到任何关于如何在 firebase 中每 5 分钟运行一次的示例。这将通过我的后端和 Firebase 的集成来解决?请问有什么例子吗?
猜你喜欢
  • 1970-01-01
  • 2017-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多