Deprecated Push Notification

Hey guys I’m trying to add Push Notifications on my project but I can’t get the Device Token. I also noticed that when I changed the AppDelegate.swift file, XCode is giving me a deprecation warning. Could that be related?

The warnings are:

‘InstanceID’ is deprecated: FIRInstanceID is deprecated, please use FIRInstallations for installation identifier handling and FIRMessaging for FCM registration token handling.

‘instanceID(handler:)’ is deprecated: Use Installations.installationID(completion:) to get the app instance identifier instead. Use Messaging.token(completion:) to get FCM registration token instead.

Any help?

Thanks

i suppose you already made a service file (for example pushService.ts) in which you are using
const { PushNotifications } = Plugins;

The, you can get the Device token like this:

PushNotifications.addListener(
  'registration',
  (token: PushNotificationToken) => {
    this.userToken = JSON.stringify(token.value);
  }
);

you can find a complete example for integrating Capacitor Push Notification here:
https://devdactic.com/push-notifications-ionic-capacitor/

Hi,

still no answer how the correct code should look like. Capacitor Doc is still using the deprecated functions.
Any ideas?

The docs were updated long ago, if you are seeing that code you might have then cached.

Or you might be checking v2 docs, those docs still use the old code, but tell you to use an older version of firebase where it wasn’t deprecated

If you are in Capacitor 3 follow the Capacitor 3 guide, if you are in Capacitor 2 and want to use the new code you can check the Capacitor 3 guide but change only the Firebase code and not the Capacitor code.

Hello @iconio, I used to face the problem of using push notifications of Capacitor version 3. But I found the way out with these docs from a great teacher of the ionic community Mr. Simmon.
There is a tutorials link for your: https://devdactic.com/push-notifications-ionic-capacitor/

I was afraid of the reason why you are not getting any tokens. In my guess maybe because you didn’t make a requestPermission().

In my case, the docs of the link above do not work for me so much, cause I make “requestPermission()” still don’t get tokens.

So I rely on my logic. I must ask about checkPermission() first, and then asking requesPermission() if App wasn’t permission plugins yet.

in my code will gonna be like this:

 <ion-button @click="askToken()"> Generate Push Notification token </ion-button>
{{ myToken }}
<script setup>
import { PushNotifications as pNo } from "@capacitor/push-notifications";
import { ref } from 'vue'

const myToken = ref('')

function askToken() {
  pNo.checkPermission().then((result) => {
    if (result.granted) {
      // you have to get pushNotifications register,
      // then they will give you an token.
      pNo.register();
    } else {
      pNo.requestPermission();
      pNo.register();
    }
  });

  pNo.addListener("registration", (token) => {
    alert("my token", token);
    console.log("My token: " + JSON.stringify(token));
  });

  pNo.addListener("registrationError", (error) => {
    alert("Ops! we have error occurs...");
    console.log("Error: " + JSON.stringify(error));
  });

  pNo.addListener("pushNotificationReceived", async (notification) => {
    console.log("Push received: " + JSON.stringify(notification));
  });

  pNo.addListener("pushNotificationActionPerformed", async (notification) => {
    console.log("Action performed: " + JSON.stringify(notification.notification));
    router.push({ name: 'home'})
  });
}
</script>

And make sure you have google-service of both android and ios.

after that, I believed you have to add this one to AppDelegate.swift

also, you may add this one into your Xcode project.

I hope my comment will help you out the day. Wish you have a good day.