繁体   English   中英

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

[英]Capacitor - Not receiving push notifications in Ionic + Angular app

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

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

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

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

电容器.config.json

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

应用程序.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.服务.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 服务文件?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM