[英]Capacitor - Not receiving push notifications in Ionic + Angular app
I've started moving on capacitor.我开始研究电容器。 After readinghttps://capacitorjs.com/docs/cordova/known-incompatible-plugins I found that capacitor doesn't support some cordova plugins.阅读https://capacitorjs.com/docs/cordova/known-incompatible-plugins后,我发现电容器不支持某些 cordova 插件。
I'm using cordova-plugin-fcm-with-dependecy-updated for android and cordova-plugin-fcm for iOS in my app for push notifications but capacitor doesn't support these plugins so I used capacitor native approach guided in https://capacitorjs.com/docs/apis/push-notifications#addlistener .我在我的应用程序中将cordova-plugin-fcm-with-dependecy-updated用于 android 和cordova-plugin-fcm用于 iOS 用于推送通知,但电容器不支持这些插件,因此我使用了https 中指导的电容器本机方法:/ /capacitorjs.com/docs/apis/push-notifications#addlistener 。
The native approach is not throwing any error and I am able to get registered token as well but I am not receiving push notifications on registered device.本机方法不会引发任何错误,我也可以获得注册令牌,但我没有在注册设备上收到推送通知。
I also tried https://www.npmjs.com/package/capacitor-fcm but fcm.getToken()
is returning null.我也试过https://www.npmjs.com/package/capacitor-fcm但fcm.getToken()
返回 null。
capacitor.config.json电容器.config.json
"plugins": {
"PushNotifications": {
"presentationOptions": [
"badge",
"sound",
"alert"
]
}
}
app.ts应用程序.ts
@Component({
selector: 'app-root',
templateUrl: 'app.component.html'
})
export class AppComponent implements OnInit {
constructor(
private platform: Platform,
private splashScreen: SplashScreen,
private fcmService: FcmService
) {
this.initializeApp();
}
ngOnInit() {}
initializeApp() {
this.platform.ready().then(() => {
setTimeout(() => {
this.splashScreen.hide();
}, 300);
this.fcmService.initPush();
});
}
}
fcm.service.ts fcm.服务.ts
import { Injectable } from '@angular/core';
import {
Plugins,
PushNotification,
PushNotificationToken,
PushNotificationActionPerformed,
Capacitor
} from '@capacitor/core';
import { Router } from '@angular/router';
const { PushNotifications, Modals } = Plugins;
import { FCM } from '@capacitor-community/fcm';
const fcm = new FCM();
const { FCMPlugin } = Plugins;
@Injectable({
providedIn: 'root'
})
export class FcmService {
constructor(
private router: Router) { }
initPush() {
alert('Capacitor platform' + Capacitor.platform);
if (Capacitor.platform !== 'web') {
this.registerPush();
}
}
private registerPush() {
PushNotifications.requestPermission().then((permission) => {
if (permission.granted) {
// Register with Apple / Google to receive push via APNS/FCM
PushNotifications.register();
} else {
alert('No permission for push granted');
}
});
PushNotifications.addListener(
'registration',
(token: PushNotificationToken) => {
alert('APN token: ' + JSON.stringify(token));
fcm.getToken().then((r) => {
alert(`FCM Token: ${r.token}`); //---- showing null.
}).catch((err) => {
alert(`FCM Token ERROR: ${JSON.stringify(err)}`);
});
}
);
PushNotifications.addListener('registrationError', (error: any) => {
alert('Registration Error: ' + JSON.stringify(error));
});
PushNotifications.addListener(
'pushNotificationReceived',
async (notification: PushNotification) => {
Modals.alert({
title: notification.title,
message: notification.body
})
}
);
PushNotifications.addListener(
'pushNotificationActionPerformed',
async (notification: PushNotificationActionPerformed) => {
alert('Action performed: ' + JSON.stringify(notification.notification));
}
);
}
}
Is there anything I am missing or I need to add extra configurations to receive push notifications?有什么我遗漏的或者我需要添加额外的配置来接收推送通知吗?
You added SHA certificate fingerprints
in your firebase project and update google service file?您在 firebase 项目中添加了SHA certificate fingerprints
并更新了 google 服务文件?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.