Unregister (Unsubscribe) from Push Notifications


#1

Is there a way to unregister (unsubscribe) from Push Notifications in Ionic2?


#2

If you are using Ionic native

I didn’t see anything about it in the documentation, but there is supposed to be an unregister method on the PushObject.

export declare class PushObject {
    private _objectInstance;
    constructor(options: PushOptions);
    /**
     * Adds an event listener
     * @param event {string}
     * @return {Observable<EventResponse>}
     */
    on(event: PushEvent): Observable<EventResponse>;
    /**
     * The unregister method is used when the application no longer wants to receive push notifications.
     * Beware that this cleans up all event handlers previously registered,
     * so you will need to re-register them if you want them to function again without an application reload.
     */
    unregister(): Promise<any>;

However, I’m having trouble making this work correctly. I would like to add a register/unregister button in my user settings. Unfortunately, the unregister method does not appear unregister the device. That or the hasPermissions method does not seem to function properly.

Has anyone else had any luck with this? I would like for users to be able to opt in or opt out any within the app.


#3

Are you saying unregister calls the successHandler, but the device remains registered?


#4

I guess my problem is this: I don’t know how to tell if the device is registered or not with the ionic native api.

If I use this.pushObject.unregister(); I do get a success handler.

But if I check this.push.hasPermission(): It says that my device still has push notifications enabled.


#5

You use pushObject.unregister() instead of push.unregister() ? Pretty sure if you want to unsubscribe from everything push.unregister() is the way to go. Have you tried that?


#6

Permission is what the user has to give the app on install when the permissions are presented (Android) or when the App asks for permission to send notifications (iOS). This doesn’t change with register and unregister, only when you go to the app settings on the OS level.

What you want is to tell the server to “unregister” you for push notifications. I don’t really know what this actually means in the context of the plugin, as “register” only really registers you with the “transmission server” of Google/Apple and returns the ID that then can be used on your own server to send push notifications (via e.g. Ionic Push) to that specific, registered device. So does “unregister” mean that this ID is invalidated?

Most apps only have a “send push = 0/1” field in the database that they change when you set push to on/off in the setting. The device is still registered and pushed to, but the app’s backend chooses not to as to this setting.


#7

I also had a look at the code behind the plugin:


As I understand this the unregister methods can either be used to unsubscribe from a topic (another concept used by push…) or, what you are looking for, in Android tell the plugin’s notification “manager” that everything gets removed or on iOS to actually unregister on the server side!

Here is some more info on the iOS side of things:

You should call this method in rare circumstances only, such as when a new version of the app removes support for all types of remote notifications. Users can temporarily prevent apps from receiving remote notifications through the Notifications section of the Settings app. Apps unregistered through this method can always re-register.

https://developer.apple.com/reference/uikit/uiapplication/1623093-unregisterforremotenotifications?language=objc

Pretty harsh wording, so I would say “unregister” is definitely not what you want.
You want to have a server side setting that contains if the user wants to receive push or not.


#8

Thanks for the explanation. It looks like I’ll have to do something on the server side then.

I’d still like to use the hasPermission() method to determine whether a user has changed their push notification preferences at the OS level.

Right now, it doesn’t seem to work.

For example:
I’ll download my app on my iOS device, then see the prompt for permission to send push notifications, I’ll select No.

Then I’ll close my app, go to settings and turn allow push notifications on. When I open the app and check hasPermission() - it still says that the user has not enabled push notifications (though I just changed this setting at the OS level.)

Conversely, if I enable push notifications with the OS prompt for permission, then go back to device settings to disable push notifications for my app - then hasPermission() still says that push notifications are enabled.