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

wkyowqbh  于 2022-12-08  发布在  Ionic
关注(0)|答案(1)|浏览(191)

我已经开始移动电容器。在阅读https://capacitorjs.com/docs/cordova/known-incompatible-plugins后,我发现电容器不支持一些 cordova 插件。
I'm usingcordova-plugin-fcm-with-dependecy-updatedfor android andcordova-plugin-fcmfor 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.
本机方法不会引发任何错误,我也能够获得注册令牌,但我在注册设备上没有收到推送通知。
我还尝试了https://www.npmjs.com/package/capacitor-fcm,但fcm.getToken()返回空值。
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));
            }
        );
    }
}

我是否遗漏了什么,或者需要添加额外的配置才能接收推送通知?

5lhxktic

5lhxktic1#

你在你的firebase项目中添加了SHA certificate fingerprints并更新了谷歌服务文件?

相关问题