ionic-team / capacitor

Build cross-platform Native Progressive Web Apps for iOS, Android, and the Web ⚡️
https://capacitorjs.com
MIT License
11.46k stars 977 forks source link

bug: PushNotification in background dont work #2722

Closed ga27 closed 4 years ago

ga27 commented 4 years ago

Bug Report

Capacitor Version

npx cap doctor output:

Latest Dependencies:

@capacitor/cli: 1.5.2

@capacitor/core: 1.5.2

@capacitor/android: 1.5.2

@capacitor/ios: 1.5.2

Installed Dependencies:

@capacitor/ios not installed

@capacitor/cli 1.5.1

@capacitor/android 2.0.0-beta.1

@capacitor/core 1.5.1

Affected Platform(s)

Current Behavior

I'm using Push notification from capacitor core in a ionic 5 test app but it don't return the push notification received in background to "notification received" function, i've tried the "notification received action performed" but it dont return the title and the body, and thats what i need. I'm saving it to native storage cause i want to show all the notifications received later.

Expected Behavior

I want to build a notification center page with the notifications received when app is open and closed (or is in background).

Sample Code or Sample Application Repo

home.page.html


<ion-header>
  <ion-toolbar>
    <ion-title>
      Ionic Blank
    </ion-title>
  </ion-toolbar>
</ion-header>

<ion-content padding>
<ion-list>
  <ion-item *ngFor="let notif of notifications">
    {{notif}} 
  </ion-item>
</ion-list>
</ion-content>

home.page.ts

import { Component } from '@angular/core';
import { Plugins, PushNotification, PushNotificationActionPerformed, PushNotificationToken } from '@capacitor/core';
const { PushNotifications } = Plugins;
import { NativeStorage } from '@ionic-native/native-storage/ngx';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage {
constructor( private nat:NativeStorage) 
  notifications: any = [];

  ngOnInit() {
    PushNotifications.register();
    PushNotifications.addListener('pushNotificationReceived', (notification: PushNotification) => {
      this.nat.setItem('not', {property: notification.title})
       this.nat.getItem('not').then(
          data => this.notifications.push([JSON.stringify(data.property).replace(this.var, '')].toString().replace(',', ': ')),
          );   
    });
  }
}

Reproduction Steps

Other Technical Details

npm --version output: 6.13.7

node --version output:12.16.0

pod --version output (iOS issues only):

Other Information

jcesarmobile commented 4 years ago

Data notifications fire pushNotificationReceived if the app is in background, but not if fully closed, it’s a known issue https://github.com/ionic-team/capacitor/issues/2401

Notification notifications don’t fire pushNotificationReceived, only the actionPerformed when you tap them, it’s how notification notifications work, they are handled by the OS, not by capacitor.

ionitron-bot[bot] commented 1 year ago

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.