question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PushNotifications stop working after installing this plugin

See original GitHub issue

I am currently using ionic capacitor PushNotifications for Firebase cloud messaging. By the help of this I can get tokens. Now I want to send notifications to all app users so I decided to use topics.

For this, I installed community/FCM plugin.

I also made suggested changes in the MainActivity.java file, no error during app build.

But after installation this FCM plugin, PushNotifications(token) stop working.

Even below code starts always retuning false.

if (isPushNotificationsAvailable) {
      this.initPushNotifications();
    }

Here is my MainActivity.java file:-

package io.ionic.starter;

import com.getcapacitor.community.fcm.FCMPlugin;

import android.os.Bundle;

import com.getcapacitor.BridgeActivity;

import com.getcapacitor.Plugin;

import java.util.ArrayList;

public class MainActivity extends BridgeActivity {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Initializes the Bridge
    this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
      // Additional plugins you've installed go here
      // Ex: add(TotallyAwesomePlugin.class);
      add(FCMPlugin.class);
    }});
  }
}

Here my fcm.service.ts File:-

import { Component, Injectable } from '@angular/core';
import { Capacitor } from '@capacitor/core';
import { Router } from '@angular/router';
import { Storage } from '@ionic/storage';
import {
  ActionPerformed,
  PushNotificationSchema,
  PushNotifications,
  Token
} from '@capacitor/push-notifications';
// import { FCM } from '@capacitor-community/fcm';

const isPushNotificationsAvailable = Capacitor.isPluginAvailable('PushNotifications');

@Injectable({
  providedIn: 'root'
})

export class FcmService {

  public topicName = 'project';

  constructor(
    private router: Router,
    private storage: Storage
  ) { }

  initPush() {
    //This Plugin is not available on web

    if (isPushNotificationsAvailable) {
      this.initPushNotifications();
    }
  }
  // Request permission to use push notifications
  // iOS will prompt user and return if they granted permission or not
  // Android will just grant without prompting
  private initPushNotifications() {

    //console.log('Initializing HomePage');

    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);
      //Store Devive Token in session variable
      this.storage.set("device_token", token.value);
      // FCM.getToken()
      //   .then((r) => alert(`Token ${r.token}`))
      //   .catch((err) => console.log(err));
      // now you can subscribe to a specific topic
      // FCM.subscribeTo({ topic: this.topicName })
      //   .then((r) => alert(`subscribed to topic`))
      //   .catch((err) => console.log(err));      
    });

    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));
        const data = notification.notification.data;
        //console.log('Action performed: ' + JSON.stringify(notification.notification));
        if (data.detailsId) {
          this.router.navigate(['/bid-project', data.detailsId]);
        }
      },
    );
  }
  //Reset all Badge count
  // resetBadgeCount() {
  //   PushNotifications.removeAllDeliveredNotifications();
  // }
  // move to fcm demo
  // subscribeTo() {
  //   PushNotifications.register()
  //     .then((_) => {
  //       FCM.subscribeTo({ topic: this.topicName })
  //         .then((r) => alert(`subscribed to topic ${this.topicName}`))
  //         .catch((err) => console.log(err));
  //     })
  //     .catch((err) => alert(JSON.stringify(err)));
  // }

  // unsubscribeFrom() {
  //   FCM.unsubscribeFrom({ topic: this.topicName })
  //     .then((r) => alert(`unsubscribed from topic ${this.topicName}`))
  //     .catch((err) => console.log(err));

  // }
}


After removing community plugin token generation started working as before. Please tell me what wrong is with my code.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
stewonescommented, Oct 5, 2021

should be fixed on latest plugin version v2.0.2

1reaction
rbokajrcommented, Sep 29, 2021

@stewones - appreciate the input, work, and expedient response very much… this will work for us for now, I suppose we’ll track it until it is merged in… hopefully they can handle that sooner than later!

Read more comments on GitHub >

github_iconTop Results From Across the Web

PushNotifications stop working after installing this plugin ...
I am currently using ionic capacitor PushNotifications for Firebase cloud messaging. By the help of this I can get tokens.
Read more >
PushNotifications stop working after installing Community ...
I am currently using ionic capacitor PushNotifications for Firebase cloud messaging. By the help of this I can get tokens.
Read more >
bug: Plugins stop working after re-compilation #2849 - GitHub
I specifically notice this starts getting buggy when I install new npm packages or clear the cache of XCode or Android Studio. Expected...
Read more >
Push notification not working - WordPress.org
I am always getting the following message when I visit the page for wpmobile.app's plugin settings in wordpress admin portal: “Push notifications are...
Read more >
WordPress Troubleshooting - OneSignal Documentation
Notifications Not Being Sent. No Recipients Error. If you are JNews Theme, go to JNews > Install Plugins > Desktop Push Notifications ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found