首页 > 解决方案 > 在 Android 上实现推送通知时出错

问题描述

我正在创建一个离子角度应用程序。我尝试将推送通知与电容器插件一起使用。但是我遇到了这样的错误。谁能帮我?

**"ERROR Error: Uncaught (in promise): Error: "PushNotifications" plugin is not implemented on android
Error: "PushNotifications" plugin is not implemented on android"**

标签: androidionic-frameworkpush-notificationcapacitor

解决方案


我解决了“错误错误:未捕获(承诺):错误:“PushNotifications”插件未在 android 上实现错误:“PushNotifications”插件未在 android 上实现”错误请按照以下步骤操作

在 android/app 文件夹中添加 google-service.json 文件

在 .ts 文件中添加此代码

import {
  ActionPerformed,
  PushNotificationSchema,
  PushNotifications,
  Token,
} from '@capacitor/push-notifications';
import { Platform } from '@ionic/angular';

constructor(public platform: Platform) {
    this.platform.ready().then(() => {
      this.pushAdded();
    })
  }




pushAdded() {
    // Request permission to use push notifications
    // iOS will prompt user and return if they granted permission or not
    // Android will just grant without prompting
    PushNotifications.requestPermissions().then(result => {
      if (result.receive === 'granted') {
        // Register with Apple / Google to receive push via APNS/FCM
        PushNotifications.register();
      } else {
        // Show some error
      }
    });

    PushNotifications.addListener('registration', (token: Token) => {
      alert('Push registration success, token: ' + token.value);
    });

    PushNotifications.addListener('registrationError', (error: any) => {
      alert('Error on registration: ' + JSON.stringify(error));
    });

    PushNotifications.addListener(
      'pushNotificationReceived',
      (notification: PushNotificationSchema) => {
        alert('Push received: ' + JSON.stringify(notification));
      },
    );

    PushNotifications.addListener(
      'pushNotificationActionPerformed',
      (notification: ActionPerformed) => {
        alert('Push action performed: ' + JSON.stringify(notification));
      },
    );
  }

在此处输入图像描述


推荐阅读