Ionic fcm 插件,onNotification 函数不工作
Ionic fcm plugin, onNotification function not working
我正在尝试实施 FCM 以向使用 Firebase 云消息传递的应用程序用户发送通知。不过,我在调试应用程序时无法在 phone 上收到任何通知。
我正尝试在 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
In cmd[your project directory]
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);
});
现在,它可以正常工作了。您可以尝试向您收到的令牌发送通知。
我正在尝试实施 FCM 以向使用 Firebase 云消息传递的应用程序用户发送通知。不过,我在调试应用程序时无法在 phone 上收到任何通知。
我正尝试在 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.
上构建的离子项目中实现 fcmIn cmd[your project directory]
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);
});
现在,它可以正常工作了。您可以尝试向您收到的令牌发送通知。