angular - 如何使用 NativeScript 和 Angular 接收推送通知?

标签 angular firebase nativescript

我正在努力让推送通知在我的项目中发挥作用。我正在使用 NativeScript 和 Angular 8 开发一个新项目。

我已使用 Firebase 创建了一个免费帐户:

https://console.firebase.google.com/

我将我的应用 ID 链接到 Firebase 项目并添加了 Android。我下载了文件“google-services.json”并将其放入 app/App_Resources/Android/google-services.json

我找到了一个用于使用 Firebase 的 NativeScript 插件:

https://github.com/EddyVerbruggen/nativescript-plugin-firebase

我按照 Firebase 控制台和插件 github 页面上的步骤进行操作,但我的手机上没有收到任何通知。

如何让我的应用接收推送通知?

这是我的 app.components.ts 文件:

import { Component, OnInit } from "@angular/core";
const firebase = require("nativescript-plugin-firebase");
//import * as firebase from "firebase";

@Component({
    selector: "ns-app",
    templateUrl: "./app.component.html"
})
export class AppComponent implements OnInit {

    ngOnInit() {
      firebase.init({

        showNotifications: true,
        showNotificationsWhenInForeground: true,

        // Optionally pass in properties for database, authentication and cloud messaging,
        // see their respective docs.

        onPushTokenReceivedCallback: (token) => {
          console.log('[Firebase] onPushTokenReceivedCallback:', { token });
        },

        onMessageReceivedCallback: (message) => {
          console.log('[Firebase] onMessageReceivedCallback:', { message });
        }

      }).then(
        () => { console.log("firebase.init done"); },
        error => {
          console.log(`firebase.init error: ${error}`);
        }
      );
    }

}

package.json:

{
  "nativescript": {
    "id": "org.nativescript.FireBaseTest2",
    "tns-android": {
      "version": "6.1.2"
    },
    "tns-ios": {
      "version": "6.1.0"
    }
  },
  ....

这是我的推送通知尝试:

enter image description here

最佳答案

你的代码看起来不错......

只需在 Firebase 初始化时从应用控制台日志中复制 token ,然后在将消息发送到目标设备时在 Firebase 控制台中使用它即可。

关于angular - 如何使用 NativeScript 和 Angular 接收推送通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58507309/

相关文章:

node.js - Azure 中的 Angular 应用程序显示原始 js 文本

javascript - 如何将一个对象的现有属性以 Angular 复制到另一个对象?

swift - 如何在 Firebase 中检索数据的名称

node.js - 为每个登录用户初始化管理 Firebase 应用程序

Angular CDK : attach overlay to a clicked element

node.js - 错误: Cannot find module './internal/streams/stream'

javascript - 在 firebase 中给定 id 之后获取更多数据

javascript - XHR 中的 Grant_Type 放在哪里

ios - Sidekick Cloud 构建 - iOS - 云消息传递

angular - NativeScript:路由器导出不起作用