rdelrosario / xamarin-plugins

Cross platform Xamarin & Windows plugins for PCLs
MIT License
179 stars 161 forks source link

Android - Don't Receive Notifications Unless I Unregister and Re-register #65

Open kspearrin opened 8 years ago

kspearrin commented 8 years ago

I'm using the android setup via the Application class. AVD emulator debug deployments from Visual Studio. I notice that unless I unregister and re-register after each deployment that the tokens do not work. If I use the same token as the last deployment the GCM server just returns:

{
  "multicast_id": 661xxxxxxxxxxxxx,
  "success": 0,
  "failure": 1,
  "canonical_ids": 0,
  "results": [
    {
      "error": "NotRegistered"
    }
  ]
}

Any ideas?

wilckerson commented 8 years ago

Same problem with a device.

If I open the app for the first time and register, I can get the token and send the notification normaly. Eventhough when I restart the app and I try to send the push, nothing happens, unless if I call unregister to get a new token.

CrossPushNotification.Current.Unregister(); CrossPushNotification.Current.Register();

kspearrin commented 8 years ago

Anyone have a workaround? Am I doing something incorrectly?

larghi-truelogic commented 8 years ago

I'm having the same issue, but even adding those CrossPushNotification.Current.Unregister(); CrossPushNotification.Current.Register(); lines, didn't make the token change. Can some one share an snippet of the code?

nielscup commented 8 years ago

I have been struggeling with this myself a while ago. It seems that android loses its registration with the token when the app is deployed again via debugging. So what I currently do is to force an unregester an register (reregister) when debugging. For your production app you should reregister on first launch, when your app is updated (versionnumber changed) and if there is no devicetoken for any other reason. Something like this:

if (debug || !HasDeviceToken() || IsFirstLaunch() || IsPackageUpdated()) { Reregister(); }

TorbenK commented 7 years ago

I think what you should do is create a BroadcastReceiver which listens to android.intent.action.MY_PACKAGE_REPLACED and re-register at GCM when its called.

I hope this will not be neccessary with FCM anymore.

rdelrosario commented 7 years ago

Just implemented that into the plugin. Let me know if works now

kspearrin commented 7 years ago

I don't think that will work @rdelrosario. You have to unregister first and then register again. Also I don't think that the plugin should be making these automatic decisions for us since there are scenarios around exactly when an author wants the registration process to happen. For example, I do not want push registration to happen until after my users have logged in first and I can identify them.

jelmarlaunchlabs commented 7 years ago

still does not work, even if with unregister(), in debugging, looks like this happens when the app is stopped (crash, swiped left in task manager and stop button in xamarin debugging).

rdelrosario commented 7 years ago

This should be working on:

https://www.nuget.org/packages/Xam.Plugin.PushNotification/1.2.5-beta

benevbright commented 7 years ago

Can't not update to 1.2.5-beta

Attempting to resolve dependencies for package 'Xam.Plugin.PushNotification.1.2.5-beta' with DependencyBehavior 'Lowest' One or more unresolved package dependency constraints detected in the existing packages.config file. All dependency constraints must be resolved to add or update packages. If these packages are being updated this message may be ignored, if not the following error(s) may be blocking the current package operation: 'Xam.Plugin.PushNotification 1.2.4' Unable to find a version of 'Xamarin.GooglePlayServices.Base' that is compatible with 'Xamarin.GooglePlayServices.Gcm 42.1001.0 constraint: Xamarin.GooglePlayServices.Base (= 42.1001.0)'.

AlcantaraRW commented 6 years ago

I'm trying to update to 1.2.5-beta and facing the same problem as @benevbright mentioned above. How can we update?!

benevbright commented 6 years ago

@AlcantaraRW I figured out by deleting all of GooglePlayServices nuget and install again.

benevbright commented 6 years ago

Hi guys,

Are you all doing OK? To use this nuget properly, I have to Unregister() everything when the app is started. And I send it to our server every single time. If I don't, the previous push token will be unusable after my app is updated.

But this solution does not seem good. How are you guys using this plugin?

Thanks.

AlcantaraRW commented 6 years ago

Can't update at all. Uninstalled all Google stuff and my project was filled with errors. Fortunately I managed to resolve. @benevbright Sadly every now and then a new error is added to my collection! Sometimes just the first notification works, sometimes it works more than once, sometimes when I click on a notification the method Register is called, sometimes Unregister is called... I simply don't know what the heck to do...