Beispiel #1
0
        public void StartRegisterDevice(Context ctx)
        {
            GcmClient.CheckDevice(ctx);
            GcmClient.CheckManifest(ctx);

            if (!GcmClient.IsRegistered(ctx))
            {
                try
                {
                    string projectId;
                    try
                    {
                        _host     = _host.ToCurrentScheme(false);
                        projectId = GetGoogleProjectId();
                    }
                    catch (WebException e)
                    {
                        _host     = _host.ToCurrentScheme(true);
                        projectId = GetGoogleProjectId();
                    }

                    GcmBroadcastReceiver.SenderIds = new[] { projectId };
                    GcmClient.Register(ctx, GcmBroadcastReceiver.SenderIds);
                }
                catch (Exception e)
                {
                    _applicationContext.HandleException(e);
                }
            }
        }
        /// <summary>
        /// Called when [registered].
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="registrationId">The registration identifier.</param>
        protected override void OnRegistered(Context context, string registrationId)
        {
            RegistrationId = registrationId;

            /*var myservice = SimpleIoc.Default.GetInstance<IMyWebService>();
             * myservice.PnsHandler = registrationId;
             * myservice.Platform = "gcm";*/

            var isRegister = GcmClient.IsRegistered(MainActivity.AppMainContext);

            CreateNotification("GcmService-GCM Registered", "The device has been registered in GCM!");
        }
Beispiel #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
            {
            }
        }
Beispiel #4
0
        protected override void OnCreate(Bundle bundle)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource   = Resource.Layout.Toolbar;

            base.OnCreate(bundle);

            var options = new FirebaseOptions.Builder()
                          .SetApplicationId("simplemessager")
                          .SetApiKey("AIzaSyCg847Wo53-3PLgUpkMWNpda9yL3qR-7HQ")
                          .SetDatabaseUrl("https://simplemessager.firebaseio.com/")
                          // .SetGcmSenderId(GcmSenderId)
                          .Build();

            instance = this;

            FirebaseApp.InitializeApp(this, options);
            VideoViewRenderer.Init();
            // QuickbloxPlatform.Init();
            global::Xamarin.Forms.Forms.Init(this, bundle);

            var layoutParams = new LinearLayout.LayoutParams(
                Resources.DisplayMetrics.WidthPixels, Resources.DisplayMetrics.HeightPixels);

            //App.NetworkProvider.GetSessionInfo();

            this.Receiver = new AcceptInvitationListener();

            isPublished = false;

            LoadApplication(new App());

            try
            {
                GcmClient.CheckDevice(this);
                GcmClient.CheckManifest(this);

                System.Diagnostics.Debug.WriteLine("Registering...");

                GcmClient.Register(this, PushHandlerBroadcastReceiver.SENDER_IDS);
                var isRegistered         = GcmClient.IsRegistered(this);
                var isOnServerRegistered = GcmClient.IsRegisteredOnServer(this);
            }
            catch (Java.Net.MalformedURLException)
            {
                CreateAndShowDialog("There was an error creating the client.Verify the URL.", "Error");
            }
            catch (Exception e)
            {
                CreateAndShowDialog(e.Message, "Error");
            }
        }
        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");
            }
        }