Пример #1
0
        private void OnCheckedChanged(object sender, CompoundButton.CheckedChangeEventArgs e)
        {
            InstanceID id = InstanceID.GetInstance(Context);

            if (e.IsChecked)
            {
                Intent intent = new Intent(Activity, typeof(RegistrationIntentService));
                Context.StartService(intent);
            }
            else
            {
                try
                {
                    id.DeleteInstanceID();
                }catch (Exception exp)
                {
                    Console.Write(exp.Message);
                }
            }
        }
Пример #2
0
        protected override void OnHandleIntent(Intent intent)
        {
            try
            {
                Bundle extras = intent.Extras;
                lock (syncLock)
                {
                    InstanceID instanceID = InstanceID.GetInstance(Android.App.Application.Context);

#if _DEBUG_
                    try
                    {
                        instanceID.DeleteInstanceID();
                    }
                    catch (Exception e)
                    {
                        Android.Util.Log.Debug(Tag, e.ToString());
                    }
                    instanceID = InstanceID.GetInstance(Android.App.Application.Context);
#endif

                    string token = instanceID.GetToken(CrossPushNotification.SenderId,
                                                       GoogleCloudMessaging.InstanceIdScope, null);

                    CrossPushNotification.PushNotificationListener.OnRegistered(token, DeviceType.Android);
                    PushNotificationImplementation.StoreRegistrationId(Android.App.Application.Context, token);
                    this.SubscribeTopics(token);

                    System.Diagnostics.Debug.WriteLine($"{token} - Device registered, registration ID={Tag}");
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"{ex.Message} - Error : {Tag}");

                CrossPushNotification.PushNotificationListener.OnError($"{ex.ToString()} - Register - {Tag}", DeviceType.Android);
            }

            // Intent registrationComplete = new Intent(PushNotificationKey.RegistrationComplete);
            // LocalBroadcastManager.GetInstance(Android.App.Application.Context).SendBroadcast(registrationComplete);
        }
Пример #3
0
        protected override void OnHandleIntent(Intent intent)
        {
            try
            {
                var sharedPreferences = PreferenceManager.GetDefaultSharedPreferences(this);
                Log.Info("RegistrationIntentService", "Calling InstanceID.GetToken");
                lock (locker)
                {
                    InstanceID instanceID = InstanceID.GetInstance(this);
                    try
                    {
                        instanceID.DeleteInstanceID();
                    }
                    catch (Java.IO.IOException e)
                    {
                        e.PrintStackTrace();
                    }
                    instanceID = InstanceID.GetInstance(this);
                    var sender_id = "510976446814";
                    var token     = InstanceID.GetInstance(this).GetToken(sender_id
                                                                          , GoogleCloudMessaging.InstanceIdScope, null);

                    var sentToken = sharedPreferences.GetString("token", "");

                    if (token != sentToken)
                    {
                        Log.Info("RegistrationIntentService", "GCM Registration Token: " + token);
                        sharedPreferences.Edit().PutString("token", token).Apply();
                        if (token != null && token != "")
                        {
                            SendRegistrationToAppServer(token);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Debug("RegistrationIntentService", "Failed to get a registration token " + ex.Message + "");
                return;
            }
        }