我正在努力让推送通知在我的项目中发挥作用。我正在使用 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"
}
},
....
这是我的推送通知尝试:
最佳答案
你的代码看起来不错......
只需在 Firebase 初始化时从应用控制台日志中复制 token ,然后在将消息发送到目标设备时在 Firebase 控制台中使用它即可。
关于angular - 如何使用 NativeScript 和 Angular 接收推送通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58507309/