问题描述
我正在尝试实施FCM,以使用Firebase Cloud Messaging向应用程序用户发送通知。但是,调试该应用程序时,我无法在手机上收到任何通知。
我正在尝试使用邮递员https://fcm.googleapis.com/fcm/send显式发送请求。这是以下请求:
{
"registration_ids": [
"my_random_device_ids"
],"notification": {
"title": "New request","body": "Your action required to accept.","content_available": false,"priority": "high","sound": "default","click_action": "FCM_PLUGIN_ACTIVITY"
},"data": {
"key": "value"
}
}
this.fcm.getToken().then(
(token) => {
console.log(token);
},(err) => {
console.log("FCM",err);
}
);
this.fcm.onNotification().subscribe(data => {
console.log(data);
});
this.fcm.onTokenRefresh().subscribe(async token => {
var deviceType = this.platform.is('android') ? 'Android' : 'iOS'
var deviceDetails = { deviceid: this.deviceUUID,devicetoken: token,deviceType: deviceType };
await this.authService.insertUpdateDevicetoken(deviceDetails);
});
无论声明this.fcm.onNotification()
,我都会在后台收到通知。
另外,当我声明它时,它不会记录任何内容,这意味着它无法正常工作。有办法解决吗?
解决方法
我找到了解决方案。与此类似,但是使用主包而不是@ionic-native/fcm
包装器库。
工作代码如下,以在基于angular的离子项目中实现fcm。
在cmd [您的项目目录]
ionic cordova plugin add cordova-plugin-fcm-with-dependecy-updated@7.3.1
现在,请确保在您的项目中的plugins文件夹中存在一个名为cordova-plugin-fcm-with-dependecy-updated
的文件夹,并且其中有一个子文件夹ionic
,并且在此子文件夹中还有另一个子文件夹ngx
。
现在,您可以在代码中使用它,我在app.component.ts中使用它
import { FCM } from 'cordova-plugin-fcm-with-dependecy-updated/ionic/ngx';
// Initialise in constructor
.
.
.
.
this.fcm.getToken().then(function(token) {
console.log(token);
}).catch(err) {
console.log(err,'Unable to get token');
});
this.fcm.onNotification().subscribe((data: any) => {
console.log(data);
});
this.fcm.onTokenRefresh(). subscribe (async token => {
console.log(token);
});
现在,它可以正常工作了。您可以尝试在收到的令牌上向发送通知。