greenygh0st / capacitor-plugin-silent-notifications

Capacitor plugin designed to allow silent push notifications on iOS.
2 stars 3 forks source link

Right way to use capacitor-plugin-silent-notifications with PushNotifications? #1

Closed bluepuma77 closed 6 months ago

bluepuma77 commented 6 months ago

As the iOS push device token is needed, should the regular PushNotifications flow be used?

if (Capacitor.isNativePlatform()) {
    console.log('Manage push notifications')

    // Request Permissions
    PushNotifications.requestPermissions().then((permission) => {
        if (permission.receive === 'granted') {
            console.log('Register for push notifications')
            PushNotifications.register()
        } else {
            console.log('ERROR No permission to register for push notifications')
        }
    });

    // Registration token
    PushNotifications.addListener('registration', async (token) => {
        if (token) {
            console.log('Registered for push notifications:', token)
        }
    });

    // Error handler
    PushNotifications.addListener('registrationError', err => {
        console.log('ERROR Registering for push notifications:', err.error);
    });

    // Subscribed notifications
    PushNotifications.addListener('pushNotificationReceived', async (notification) => {
        if (notification.data.action === 'silent') {
            console.log('Silent push notification received:', notification)
        } else {
            console.log('Standard push notification received:', notification)
        }
    });

    // Silent push notifications
    CapacitorSilentNotifications.addListener('silentNotificationReceived', async (payload) => {
        console.log('silentNotificationReceived:', payload);
    });

} else {
    console.log('ERROR Push notification only possible on native');
}

What happens with PushNotifications.addListener('pushNotificationReceived',...)? Can it be used or will it disturb regular silent push notification handling?

greenygh0st commented 6 months ago

You will need to get the iOS push token and save that on away in whatever API/DB you are building to support the app. So you have to use the base PushNotifications package to get the push token.

This plugin does not interfere with the regular push notifications functionality. When a background notification is received it triggers a different callback (ie the one in the documentation) to be called which is separate from the regular callback which is what PushNotifications is hooked into. There is an example app and test notification files in test-files that you can use to play with the two.

bluepuma77 commented 6 months ago

Awesome, thank you!