首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何通过NativeScript & Angular接收推送通知?

如何通过NativeScript & Angular接收推送通知?
EN

Stack Overflow用户
提问于 2019-10-22 23:10:34
回答 2查看 1.2K关注 0票数 2

我正在努力让推送通知在我的项目中发挥作用。我正在与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文件:

代码语言:javascript
运行
复制
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:

代码语言:javascript
运行
复制
{
  "nativescript": {
    "id": "org.nativescript.FireBaseTest2",
    "tns-android": {
      "version": "6.1.2"
    },
    "tns-ios": {
      "version": "6.1.0"
    }
  },
  ....

以下是我的推送通知尝试:

EN

回答 2

Stack Overflow用户

发布于 2019-10-26 21:11:18

你的代码看起来没问题……

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

票数 2
EN

Stack Overflow用户

发布于 2022-01-25 07:55:55

在Nativescript应用程序端,您需要执行以下操作才能使用FCM。

1)将来自fcm的google-services.json文件添加到路径中

"App_Resources/Android“

2)如果ns 7以上版本,请根据NS版本添加nativescript firebase插件

tns插件add @nativescript/firebase

3)在App_Resources/Android/app.gradle文件中添加以下依赖项

代码语言:javascript
运行
复制
dependencies {
 implementation 'com.google.firebase:firebase-iid'
}

4)在app.component.ts中添加firebase初始化代码

代码语言:javascript
运行
复制
    ngOnInit(): void {
       firebase.init({
          showNotifications: true,
          showNotificationsWhenInForeground: true,
    
          onPushTokenReceivedCallback: (token) => {
            console.log('[Firebase] onPushTokenReceivedCallback:', { token });
          },
    
          onMessageReceivedCallback: (message: firebase.Message) => {
            console.log('[Firebase] onMessageReceivedCallback:', { message });
          }
        })
          .then(() => {
            console.log('[Firebase] Initialized');
          })
          .catch(error => {
            console.log('[Firebase] Initialize', { error });
          });
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58507309

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档