Ejemplo n.º 1
0
        void updateView()
        {
            //Get the stored latest registration id
            var registrationId = GcmClient.GetRegistrationId(this);

            //If it's empty, we need to register
            if (string.IsNullOrEmpty(registrationId))
            {
                registered = false;
                this.textRegistrationStatus.Text = "Registered: No";
                this.textRegistrationId.Text     = "Id: N/A";
                this.buttonRegister.Text         = "Register...";

                Log.Info(TAG, "Not registered...");
            }
            else
            {
                registered = true;
                this.textRegistrationStatus.Text = "Registered: Yes";
                this.textRegistrationId.Text     = "Id: " + registrationId;
                this.buttonRegister.Text         = "Unregister...";

                Log.Info(TAG, "Already Registered: " + registrationId);
            }

            var prefs = GetSharedPreferences(this.PackageName, FileCreationMode.Private);

            this.textLastMsg.Text = "Last Msg: " + prefs.GetString("last_msg", "N/A");

            //Enable the button as it was normally disabled
            this.buttonRegister.Enabled = true;
        }
Ejemplo n.º 2
0
        public async Task RegisterForPushNotifications(MobileServiceClient client)
        {
            var registrationId = GcmClient.GetRegistrationId(RootView);

            var azurePush = client.GetPush();

            var installation = new DeviceInstallation
            {
                InstallationId = client.InstallationId,
                Platform       = "gcm",
                PushChannel    = registrationId
            };

            installation.Tags.Add("silent-push");

            var silentTemplate = new PushTemplate
            {
                Body = @"{""data"":{""op"":""$(op)"",""table"":""$(table)"",""id"":""$(id)""}}"
            };

            installation.Templates.Add("silent", silentTemplate);

            var response = await client.InvokeApiAsync <DeviceInstallation, DeviceInstallation>(
                $"/push/installations/{client.InstallationId}",
                installation,
                HttpMethod.Put,
                new System.Collections.Generic.Dictionary <string, string>());
        }
Ejemplo n.º 3
0
        //Part 9: Adding Tagging Support:  Pre-provisioned
        public async void RegisterForPushNotifications(Context context, MobileServiceClient client)
        {
            if (GcmClient.IsRegistered(context))
            {
                try
                {
                    string registrationId = GcmClient.GetRegistrationId(context);

                    DeviceInstallation installation = new DeviceInstallation
                    {
                        InstallationId = client.InstallationId,
                        Platform       = "gcm",
                        PushChannel    = registrationId
                    };

                    //Template: Specify exact format for receive notification
                    //$(messageParam): define a variable which server would fill this
                    PushTemplate genericTemplate = new PushTemplate
                    {
                        Body = "{\"data\":{\"message\":\"$(messageParam)\"}}"
                    };

                    installation.Templates.Add("genericTemplate", genericTemplate);

                    PushTemplate discussionTemplate = new PushTemplate
                    {
                        Body = "{\"data\":{\"message\":\"$(content)\"}}"
                    };

                    installation.Templates.Add("discussionTemplate", discussionTemplate);

                    //List<string> extraTags = new List<string>();
                    //Tag: specific device received this notification
                    //extraTags.Add("Android");
                    //installation.Tags = extraTags;

                    DeviceInstallation response = await client.InvokeApiAsync <DeviceInstallation, DeviceInstallation>($"/push/installations/{client.InstallationId}", installation, HttpMethod.Put, new Dictionary <string, string>());

                    //dynamicTags: what the user want such as social news as notification
                    List <string> dynamicTags = new List <string>();
                    dynamicTags.Add(XamarinMastering.Helpers.RegistrationHelper.CurrentPlatformId);

                    await XamarinMastering.Helpers.RegistrationHelper.UpdateInstallationTagsAsync(client.InstallationId, dynamicTags);
                }
                catch (Exception ex)
                {
                }
            }
            else
            {
            }
        }
        public async Task RegisterForPushNotifications(MobileServiceClient client)
        {
            if (GcmClient.IsRegistered(RootView))
            {
                try
                {
                    var registrationId = GcmClient.GetRegistrationId(RootView);
                    //var push = client.GetPush();
                    //await push.RegisterAsync(registrationId);

                    var installation = new DeviceInstallation
                    {
                        InstallationId = client.InstallationId,
                        Platform       = "gcm",
                        PushChannel    = registrationId
                    };
                    // Set up tags to request
                    installation.Tags.Add("topic:Sports");
                    // Set up templates to request
                    var genericTemplate = new PushTemplate
                    {
                        Body = @"{""data"":{""message"":""$(message)"",""picture"":""$(picture)""}}"
                    };
                    installation.Templates.Add("genericTemplate", genericTemplate);

                    var pushToSyncTemplate = new PushTemplate
                    {
                        Body = @"{""data"":{""op"":""$(op)"",""table"":""$(table)"",""id"":""$(id)""}}"
                    };
                    installation.Templates.Add("pushToSync", pushToSyncTemplate);

                    // Register with NH
                    var response = await client.InvokeApiAsync <DeviceInstallation, DeviceInstallation>(
                        $"/push/installations/{client.InstallationId}",
                        installation,
                        HttpMethod.Put,
                        new Dictionary <string, string>());
                }
                catch (Exception ex)
                {
                    Log.Error("DroidPlatformProvider", $"Could not register with NH: {ex.Message}");
                }
            }
            else
            {
                Log.Error("DroidPlatformProvider", $"Not registered with GCM");
            }
        }
        public void Init(Context context)
        {
            RootView     = context;
            AccountStore = AccountStore.Create(context);

            try
            {
                // Check to see if this client has the right permissions
                GcmClient.CheckDevice(RootView);
                GcmClient.CheckManifest(RootView);

                // Register for push
                GcmClient.Register(RootView, GcmHandler.SenderId);
                Debug.WriteLine($"GcmClient: Registered for push with GCM: {GcmClient.GetRegistrationId(RootView)}");
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"GcmClient: Cannot register for push: {ex.Message}");
            }
        }
Ejemplo n.º 6
0
        private void AuthThroughBuddy(string fbUserId, string fbUserToken)
        {
            //Check to ensure everything's setup right
            GcmClient.CheckDevice(this);
            GcmClient.CheckManifest(this);
            GcmClient.Register(this, GcmBroadcastReceiver.SENDER_IDS);
            var registrationId = GcmClient.GetRegistrationId(this);

            FacebookClient fb = new FacebookClient(fbUserToken);

            fb.AppId     = AppId;
            fb.AppSecret = AppSecret;

            BuddyClient              client              = new BuddyClient(BuddyAppName, BuddyAppKey);
            const string             BuddyAccountKey     = "BuddyAccount";
            const string             BuddyAccessTokenKey = "AccessToken";
            const string             FacebookIDTokenKey  = "FacebookIdTokenKey";
            var                      accountStore        = AccountStore.Create(this);
            var                      savedAccount        = accountStore.FindAccountsForService(BuddyAccountKey).LastOrDefault();
            Task <AuthenticatedUser> getUserTask;
            bool                     saveAccount = false;

            if (savedAccount != null && savedAccount.Properties.ContainsKey(BuddyAccessTokenKey))
            {
                saveAccount = false;
                var token = savedAccount.Properties [BuddyAccessTokenKey];
                getUserTask = client.LoginAsync(token);
            }
            else
            {
                saveAccount = true;
                getUserTask = client.SocialLoginAsync("Facebook", fbUserId, fbUserToken).ContinueWith((u) => {
                    if (u.IsFaulted)
                    {
                        // try again for kicks
                    }

                    return((AuthenticatedUser)u.Result);
                });
            }

            getUserTask.ContinueWith(r => {
                Console.WriteLine("Get User Task has happened result is faulted = " + r.IsFaulted.ToString());

                if (!r.IsFaulted)
                {
                    AuthenticatedUser user = r.Result;
                    var successActivity    = new Action(() => {
                        var repository = new POPpicLibrary.GameRepository(user, client, fb);
                        Console.WriteLine("Success task is running!");

                        if (saveAccount)
                        {
                            var properties = new Dictionary <string, string>();
                            properties[BuddyAccessTokenKey] = user.Token;
                            properties[FacebookIDTokenKey]  = fbUserId;
                            Account buddyAccount            = new Account(user.ID.ToString(), properties);
                            accountStore.Save(buddyAccount, BuddyAccountKey);
                        }

                        // Finish();

                        if (progressDialog != null)
                        {
                            progressDialog.Dismiss();
                        }

                        ((POPpicApplication)Application).SetGameRepository(repository);
                    });

                    if (string.IsNullOrEmpty(user.ApplicationTag))
                    {
                        user.PhotoAlbums.CreateAsync(user.ID.ToString(), true, null).ContinueWith(pa => {
                            if (!pa.IsFaulted)
                            {
                                var album = pa.Result;
                                user.VirtualAlbums.CreateAsync(user.ID.ToString(), null).ContinueWith(va => {
                                    if (!va.IsFaulted)
                                    {
                                        var virtualAlbum               = va.Result;
                                        var extraData                  = new UserExtraData();
                                        extraData.UploadAlbumId        = album.AlbumId;
                                        extraData.WinnerAblumVirtualId = virtualAlbum.ID;
                                        user.UpdateAsync(null, null, UserGender.Any, 0, null, UserStatus.Any, false, false, JsonConvert.SerializeObject(extraData)).ContinueWith(updateResult => {
                                            if (!updateResult.IsFaulted && updateResult.Result)
                                            {
                                                RunOnUiThread(successActivity);
                                            }
                                        });
                                    }
                                });
                            }
                        });
                    }
                    else
                    {
                        RunOnUiThread(successActivity);
                    }
                }
                else
                {
                    if (progressDialog != null)
                    {
                        progressDialog.Dismiss();
                    }
                    RunOnUiThread(() => {
                        AndroidUtilities.ShowAlert(this, "Error Getting User Account", r.Exception.Message, "Try Again", (object sender, DialogClickEventArgs e) => {
                            AuthThroughBuddy();
                        });
                    });

                    Console.WriteLine(r.Exception.Message);
                }
            });
        }