Maxim-Kolmogorov/cordova-plugin-push-notifications

How are we supposed to install, import, and use this in Angular?

Closed this issue · 2 comments

obuw commented

I feel like I must be missing something. Isn't there supposed to be an accompanying npm install step in order to let us import and reference the service?

I'm a bit of a beginner when it comes to cordova plugins so I'd appreciate some direction. Thanks.

Hi.

I'll tell you right away - I'm not familiar with Angular. After your question, I will probably have to do it.

Many Cordova plugins work this way, you simply install them in your project and call them, for example, through the window variable or cordova.pluginName().

Here's an example (as you can see, I haven't come up with anything):

https://github.com/apache/cordova-plugin-statusbar

@obuw I can give you an example of what I did.

Install via NPM
npm i cordova-plugin-push-notifications
I put my notification code in a separate service and call it when a user logs in.
In the service, I import the push notification library
import PushNotification from 'cordova-plugin-push-notifications/types';

I declare the global variable of deviceToken and then set it to the token returned in the callback:

`var deviceToken = "";

window.pushNotification.registration(
(token: string) => {
deviceToken = token;
},
(error: string) => {
console.error(error);
}
) as PushNotification;
`

NOTE: I did this outside of the class declaration in the service file, because it is not injectable in the service.

I then added a function within the service class called submitNotificationToken() which submitted the notification token to my server, so it can register it with FCM and APS.

Then I called the submitNotificationToken() when a user clicks "Login" in my app.
submitForm(e) { e.preventDefault(); this.pushNotificationService.submitNotificationToken(); }

There are probably better ways of doing it, but this is what worked for me! Now I can push notifications to my Apple or Android devices. I'm so glad I found this plugin!