是否可以在ionic 4项目中使用推送通知?

时间:2019-07-10 16:57:44

标签: ionic-framework push-notification apple-push-notifications ionic4 android-push-notification

我们目前有一个已编码的Ionic和Firebase项目。在此项目中,我们要使用推送通知。但是我们的麻烦是: 我们正在寻找一个推送通知插件,例如WhatsApp应用程序。例如,当我们向某人发送消息时,我们希望该通知发送给我们发短信的人,而不是所有人。但是我们找不到一种免费的方法来做到这一点。你有什么建议吗?谢谢。

3 个答案:

答案 0 :(得分:1)

Firebase云消息传递:使用cordova-plugin和ionic-native:Ref. Url

  import { FCM } from '@ionic-native/fcm/ngx';

constructor(private fcm: FCM) {}

this.fcm.getToken().then(token => {

  //you can store device token in firebase, later you can target push notification from firebase console this token id
  backend.registerToken(token);
});

this.fcm.onNotification().subscribe(data => {
  if(data.wasTapped){  / * true , means the user tapped the notification from the notification tray and that’s how he opened the app. */
    console.log("Received in background");
  } else {// false , means that the app was in the foreground (meaning the user was inside the app at that moment)
    console.log("Received in foreground");
  };
});

this.fcm.onTokenRefresh().subscribe(token => {

  //update device token
  backend.registerToken(token);
});

答案 1 :(得分:0)

首先,您必须先创建推送通知发送服务器才能发送特定用户

这是发送PHP检查https://www.androidhive.info

的方式
  1. 创建一个名为config.php的文件,并添加以下代码。在这里,我们定义了将Firebase Web API密钥发送到Firebase端点的请求。

config.php

import sys
import pprint
pprint.pprint(search.path)
from opentrons import robot, containers, instruments
  1. 创建一个名为push.php的文件,该类准备并将需要发送到firebase的推送通知json。

push.php

<?php

// Firebase API Key
define('FIREBASE_API_KEY', 'AIzaSyCZy2efY1j8A3XmTm79OjJFcVyUfcqN9XX');
  1. 创建一个名为firebase.php的文件,并添加以下代码。此类包含必要的功能,可通过发出CURL请求将消息发送到firebase api。

    <?php
    
    /**
     * @author Ravi Tamada
     * @link URL Tutorial link
     */
    class Push {
    
        // push message title
        private $title;
        private $message;
        private $image;
        // push message payload
        private $data;
        // flag indicating whether to show the push
        // notification or not
        // this flag will be useful when perform some opertation
        // in background when push is recevied
        private $is_background;
    
        function __construct() {
    
        }
    
        public function setTitle($title) {
            $this->title = $title;
        }
    
        public function setMessage($message) {
            $this->message = $message;
        }
    
        public function setImage($imageUrl) {
            $this->image = $imageUrl;
        }
    
        public function setPayload($data) {
            $this->data = $data;
        }
    
        public function setIsBackground($is_background) {
            $this->is_background = $is_background;
        }
    
        public function getPush() {
            $res = array();
            $res['data']['title'] = $this->title;
            $res['data']['is_background'] = $this->is_background;
            $res['data']['message'] = $this->message;
            $res['data']['image'] = $this->image;
            $res['data']['payload'] = $this->data;
            $res['data']['timestamp'] = date('Y-m-d G:i:s');
            return $res;
        }
    
    }
    
  2. 最后创建一个index.php并添加以下代码。在这里,我们正在创建一个简单的UI,该UI接受输入并将通知发送到android设备。

index.php

/**
 * @author Ravi Tamada
 * @link URL Tutorial link
 */
class Firebase {

    // sending push message to single user by firebase reg id
    public function send($to, $message) {
        $fields = array(
            'to' => $to,
            'data' => $message,
        );
        return $this->sendPushNotification($fields);
    }

    // Sending message to a topic by topic name
    public function sendToTopic($to, $message) {
        $fields = array(
            'to' => '/topics/' . $to,
            'data' => $message,
        );
        return $this->sendPushNotification($fields);
    }

    // sending push message to multiple users by firebase registration ids
    public function sendMultiple($registration_ids, $message) {
        $fields = array(
            'to' => $registration_ids,
            'data' => $message,
        );

        return $this->sendPushNotification($fields);
    }

    // function makes curl request to firebase servers
    private function sendPushNotification($fields) {

        require_once __DIR__ . '/config.php';

        // Set POST variables
        $url = 'https://fcm.googleapis.com/fcm/send';

        $headers = array(
            'Authorization: key=' . FIREBASE_API_KEY,
            'Content-Type: application/json'
        );
        // Open connection
        $ch = curl_init();

        // Set the url, number of POST vars, POST data
        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // Disabling SSL Certificate support temporarly
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

        // Execute post
        $result = curl_exec($ch);
        if ($result === FALSE) {
            die('Curl failed: ' . curl_error($ch));
        }

        // Close connection
        curl_close($ch);

        return $result;
    }
}
?>

信用https://www.androidhive.info

答案 2 :(得分:0)

我不建议您使用FCM插件。它没有方法可以在您的应用中管理您的通知(清除全部或清除一些特殊通知。

最好使用phonegap-push-plugin或一个信号