cordova-plugin-fcm-with-dependecy-updated icon indicating copy to clipboard operation
cordova-plugin-fcm-with-dependecy-updated copied to clipboard

Unable to trigger an error callback from subscribeToTopic()

Open jeromemnx opened this issue 4 years ago • 7 comments

I'm looking for a way to know if a subscription has succeeded or not, but the only callback I can get is always "OK". Android / Cordova 10 / Plugin 7.7.0

FCM.subscribeToTopic('willwork').then(function(success) { alert('ok:'+JSON.stringify(success)); }).catch(function(error) { alert('err:'+JSON.stringify(error)); });

FCM.subscribeToTopic('will fail due to spaces').then(function(success) { alert('ok:'+JSON.stringify(success)); }).catch(function(error) { alert('err:'+JSON.stringify(error)); });

I also tried with syntax from forked version, with no more success.

FCM.subscribeToTopic('will fail due to spaces',function(success) { alert('ok:'+JSON.stringify(success)); },function(error) { alert('err:'+JSON.stringify(error)); });

Is there a way to trigger successCallback/errorCallback ?

jeromemnx avatar Jan 30 '21 23:01 jeromemnx

The function subscribeToTopic returns a promise void so there is no way to know, you just have to launch subscribeToTopic to subscribe and unsubscribeFromTopic to unsubscribe.

RidClick avatar Feb 02 '21 14:02 RidClick

My idea was to ensure that (un)subscribe was successful and manage a retry or display an error message if not. If an user uncheck a box to unsubscribe from a topic, but the unsubscribe fail, he will still receive notifications although the checkbox is off, what is an UX failure. It seem that subscribeToTopic() and unsubscribeToTopic() functions in the original plugin (https://github.com/fechanique/cordova-plugin-fcm) provide a success/error callback mechanism that allow to deal with this issue. I thought this would be also the case for this forked plugin. Either it's the case and I made a mistake in my syntax, either not and I suggest @andrehtissot should consider adding this functionality. To deal with this issue I (un)subscribe each user to each topic on each app restart, but I consider it's not a very nice way to do.

jeromemnx avatar Feb 03 '21 11:02 jeromemnx

Actually what is needed in your case would be a function to recover a user's subscriptions by Token. From my point of view your solution would be to save in the app storage the subscriptions that have been marked and in the action of the check button at the same time as save in the storage the true or false subscribe or unsubscribe from the topic.

RidClick avatar Feb 03 '21 11:02 RidClick

A function to recover an user subscriptions from its token could be another way to deal with it, you're right. But I don't need the token because my app never need to push a notification to an unique user (only to topics), so I currently do not even call getToken(). I do save in the local storage the subscriptions of the user, by that way I can periodically (on each app start) re-send all (un)subscribes to be almost sure they are synchronized with the user whish (if it fails to subscribe now due to network error, it will probably success tomorrow when the user relaunch the app). But in case the topic name is wrong (contains a space or is too long), it will never success and I have no way to alert the user about this issue.

jeromemnx avatar Feb 03 '21 11:02 jeromemnx

I understand then that you allow the end users to generate the name of the topic so that they subscribe? If this is the case, then you should control the input where they write to be all lowercase without spaces or strange characters or allow them to put the name they want but internally generate a unique code for the topic.

RidClick avatar Feb 03 '21 12:02 RidClick

They cannot choose the name by theirselves, but the app is based on imported data with daily update, and users can subscribe to each of them. I generate a topic name according to the "id" of each imported data, so I can manage to generate correct topic's names in (almost) all case. But by my experience, if something CAN go wrong, it WILL go wrong one day, so I'm used to manage issues that should never (but will) happen. Although my app is designed to generate correct names today, it could change in future if Google decide to change the allowed characters in topic's name (or the maxlength), or if some imported datas get very long "id" (I do not manage the datasource). If something like that (which I cannot predict today) happen, I would be glad to display an error message to my users (or report the issue to an admin), rather than let him thinks everything is fine while it's not.

jeromemnx avatar Feb 03 '21 13:02 jeromemnx

I understand, the only thing I can tell you then is that you could still take a look at the OneSignal plugin that is much more complete with respect to topics and notifications in general. The bad thing is that it is a bit more complex to install on iOS and in the end it is not free but I have it for an app with a relatively small number of users <5k and it has everything that we have been commenting on. Good luck :)

RidClick avatar Feb 03 '21 14:02 RidClick