简体   繁体   English

带电容器的离子推送通知,注册并获取令牌但未收到通知 Android

[英]Ionic push notifications with Capacitor, registering and getting token but not receiving notifications Android

I have an Ionic 5 app with Capacitor 3 and I'm trying to receive notifications using Firebase Cloud messaging, on an Android device.我有一个带电容器 3 的 Ionic 5 应用程序,我正在尝试在 Android 设备上使用 Firebase 云消息传递接收通知。 I followed the configurations,(I downloaded the google JSON file and put my app id name correctly ) and I'm getting correctly the device token.我遵循了配置,(我下载了谷歌 JSON 文件并正确输入了我的应用程序 ID 名称)并且我得到了正确的设备令牌。 Once my app is open I get the token successfully without any error and then I send a notification sharing my token to the Firebase test message, the notification never arrived, and also I never get an error of push notification in my logger.一旦我的应用程序打开,我就成功获得了令牌,没有任何错误,然后我向 Firebase 测试消息发送了一个共享我的令牌的通知,通知从未到达,而且我的记录器中也从未收到推送通知的错误。 This is the code that I use for push notification.这是我用于推送通知的代码。

export class PushNotificationsService {

  constructor(private readonly http: HttpClient, private notificationState: NotificationsStore) { }

   public initPush() {
  if (Capacitor.getPlatform() !== 'web') {
      this.registerPush();
     }
  }

  private registerPush() {
    PushNotifications.requestPermissions().then(async result => {
      if (result.receive === 'granted') {
        // Register with Apple / Google to receive push via APNS/FCM
        console.log('granted');
       await PushNotifications.register();
      } else {
        // Show some error
        console.log('errorr');
      }
    });

    // On success, we should be able to receive notifications
    PushNotifications.addListener('registration',
      (token: Token) => { (I get through this step and access the token successfully)
        console.log('Push registration success, token: ' + token.value);
        this.notification state.setToken(token.value);
      }
    );

    // I never get this step error
    PushNotifications.addListener('registrationError',
      (error: any) => {
        console.log('Error on registration: ' + JSON.stringify(error));
      }
    );

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

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

I also have capacitor.config.json like this:我也有这样的电容器.config.json:

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

Also, I checked that my device and app have permission for notifications and are enabled both.此外,我检查了我的设备和应用程序是否具有通知权限并且都启用了。 I tried and test this issue with my app open and closed and open only in the background and the notification never arrives.我尝试通过打开和关闭我的应用程序并仅在后台打开并且通知永远不会到达来测试此问题。 What it could be?它可能是什么? Any clue?有什么线索吗? Thank you谢谢

Android Phone Please Create the channel Like this, Test or Add screenshot for console Error Android 电话请像这样创建频道,测试或添加控制台错误截图

    if (Capacitor.getPlatform() === 'android') {
   PushNotifications.createChannel({
    id: 'fcm_default_channel',
    name: 'app name',
    description: 'Show the notification if the app is open on your device',
    importance: 5,
    visibility: 1,
    lights: true,
    vibration: true,
  });
}

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

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