电容器 - 在 Ionic + Angular 应用程序中未收到推送通知

问题描述

我已经开始研究电容器。阅读https://capacitorjs.com/docs/cordova/known-incompatible-plugins后,我发现电容不支持某些cordova插件

我在我的应用中使用 cordova-plugin-fcm-with-dependecy-updated for android 和 cordova-plugin-fcm for iOS 在我的应用程序中推送通知但电容器不支持这些插件,所以我使用了 https://capacitorjs.com/docs/apis/push-notifications#addlistener 中指导的电容原生方法

本机方法不会引发任何错误,我也可以获得注册令牌,但我没有在注册设备上收到推送通知

我也试过 https://www.npmjs.com/package/capacitor-fcm,但 fcm.getToken() 返回 null。

capacitor.config.json

"plugins": {
         "PushNotifications": {
           "presentationoptions": [
             "badge","sound","alert"
           ]
         }
       }

app.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

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));
            }
        );
    }
}

是否有遗漏或需要添加额外配置来接收推送通知

解决方法

您在 Firebase 项目中添加了 SHA certificate fingerprints 并更新了 google 服务文件?