Пример #1
0
 public async Task RegisterForPushNotifications(MobileServiceClient client)
 {
     if (AppDelegate.PushDeviceToken != null)
     {
         try
         {
             var installation = new DeviceInstallation
             {
                 InstallationId = client.InstallationId,
                 Platform       = "apns",
                 PushChannel    = AppDelegate.PushDeviceToken.ToString()
             };
             // Set up tags to request
             installation.Tags.Add("topic:Sports");
             // Set up templates to request
             PushTemplate genericTemplate = new PushTemplate
             {
                 Body = "{\"aps\":{\"alert\":\"$(messageParam)\"}}"
             };
             // Register with NH
             var response = await client.InvokeApiAsync <DeviceInstallation, DeviceInstallation>(
                 $"/push/installations/{client.InstallationId}",
                 installation,
                 HttpMethod.Put,
                 new Dictionary <string, string>());
         }
         catch (Exception ex)
         {
             System.Diagnostics.Debug.Fail($"[iOSPlatformProvider]: Could not register with NH: {ex.Message}");
         }
     }
 }
        public DeviceInstallation GetDeviceRegistration(params string[] tags)
        {
            var installationId = this.GetInstallationId();

            if (FirebaseInstanceId.Instance.Token == null)
            {
                return(null);
            }

            var installation = new DeviceInstallation
            {
                InstallationId = installationId,
                Platform       = "gcm",
                PushChannel    = FirebaseInstanceId.Instance.Token
            };

            // Set up tags to request
            installation.Tags.AddRange(tags);
            // Set up templates to request
            PushTemplate genericTemplate = new PushTemplate
            {
                Body = @"{""data"":{""message"":""$(messageParam)""}}"
            };
            PushTemplate silentTemplate = new PushTemplate
            {
                Body = @"{""data"":{""message"":""$(silentMessageParam)"", ""action"":""$(actionParam)"", ""silent"":""true""}}"
            };

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

            return(installation);
        }
        public DeviceInstallation GetDeviceRegistration(params string[] tags)
        {
            if (AppDelegate.PushDeviceToken == null)
            {
                return null;
            }

            // Format our app install information for NH
            var registrationId = AppDelegate.PushDeviceToken.Description
                .Trim('<', '>').Replace(" ", string.Empty).ToUpperInvariant();

            var installation = new DeviceInstallation
            {
                InstallationId = UIDevice.CurrentDevice.IdentifierForVendor.ToString(),
                Platform = "apns",
                PushChannel = registrationId
            };
            // Set up tags to request
            installation.Tags.AddRange(tags);
            // Set up templates to request
            PushTemplate genericTemplate = new PushTemplate
            {
                Body = "{\"aps\":{\"alert\":\"$(messageParam)\"}}"
            };
            PushTemplate silentTemplate = new PushTemplate
            {
                Body = "{\"aps\":{\"content-available\":\"$(silentMessageParam)\", \"sound\":\"\"},\"action\": \"$(actionParam)\"}"
            };
            installation.Templates.Add("genericTemplate", genericTemplate);
            installation.Templates.Add("silentTemplate", silentTemplate);

            return installation;
        }
Пример #4
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>());
        }
Пример #5
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
            {
            }
        }
Пример #6
0
        public static async Task <int> CreateOrUpdateTemplateAsync(PushTemplate template)
        {
            try
            {
#if DEBUG
                template.CreateUser = "******";
#endif
                if (template.PKID != 0)
                {
                    using (var client = new Tuhu.Service.Push.TemplatePushClient())
                    {
                        var result = await client.UpdatePushTemplateAsync(template);

                        result.ThrowIfException(true);
                        var datas = result.Result;

                        return(datas);
                    }
                }
                else
                {
                    if (template.BatchID != 0)
                    {
                        var temp = await SelectPushTemplateByBatchIDAndDeviceTypeAsync(template.BatchID, template.DeviceType);

                        if (temp != null)
                        {
                            template.PKID = temp.PKID;
                            return(await CreateOrUpdateTemplateAsync(template));
                        }
                    }
                    if (string.IsNullOrEmpty(template.PlanName))
                    {
                        template.PlanName = "计划名称";
                    }
                    using (var client = new Tuhu.Service.Push.TemplatePushClient())
                    {
                        var result = await client.CreateTemplateAsync(template);

                        result.ThrowIfException(true);
                        var datas = result.Result;

                        return(datas);
                    }
                }
            }
            catch (System.Exception ex)
            {
                WebLog.LogException(ex);
                return(0);
            }
        }
Пример #7
0
        //Part 9: Adding Tagging Support: Pre-provisioned
        public async Task RegisterForPushNotifications(MobileServiceClient client)
        {
            if (AppDelegate.PushDeviceToken != null)
            {
                try
                {
                    string registrationId = AppDelegate.PushDeviceToken
                                            .Description
                                            .Trim('<', '>')
                                            .Replace(" ", string.Empty)
                                            .ToUpperInvariant();

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

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

                    //Tag: specific device received this notification
                    //installation.Tags.Add("iOS");
                    //installation.Tags.Add("Texas");
                    installation.Templates.Add("genericTemplate", genericTemplate);

                    PushTemplate discussionTemplate = new PushTemplate
                    {
                        Body = "{\"aps\":{\"alert\":\"$(content)\"}}"
                    };

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

                    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)
                {
                }
            }
        }
Пример #8
0
        public async void RegisterAsync(Microsoft.WindowsAzure.MobileServices.Push push, IEnumerable <string> tags)
        {
            try
            {
                var installation = new DeviceInstallation
                {
                    InstallationId = Client.InstallationId,
                    Platform       = "gcm",
                    PushChannel    = RegistrationID
                };


                using (var scope = App.Container?.BeginLifetimeScope())
                {
                    (await scope?.Resolve <GrupoOfertaService>()?.CarregarGrupoDeOfertasUsuarioLogadoAsync()).Select(x => { installation.Tags.Add(x.Id); return(x); }).ToArray();
                    (await scope?.Resolve <ListaCompraService>()?.CarregarListasDeComprasUsuarioLogadoAsync()).Select(x => { installation.Tags.Add(x.Id); return(x); }).ToArray();
                    installation.Tags.Add("user:"******"{""data"":{""key"":""$(keyParam)"",""message"":""$(messageParam)""}}"
                };
                PushTemplate ofertaTemplate = new PushTemplate
                {
                    Body = @"{""data"":{""key"":""$(keyParam)"",""ofertaTitle"":""$(ofertaTitleParam)"",""ofertaDescription"":""$(ofertaDescriptionParam)"",""idOFerta"":""$(ofertaIdParam)""}}"
                };

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

                // Register with NH
                await Client.InvokeApiAsync <DeviceInstallation, DeviceInstallation>(
                    $"/push/installations/{Client.InstallationId}",
                    installation,
                    HttpMethod.Put,
                    new Dictionary <string, string>());

                await Client.InvokeApiAsync <string[], object>("refreshPushRegistration", installation.Tags.ToArray());

                //await push. RegisterAsync(RegistrationID, templates);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                Debugger.Break();
            }
        }
        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 async Task RegisterForPushNotifications(MobileServiceClient client)
        {
            if (AppDelegate.PushDeviceToken != null)
            {
                try
                {
                    var registrationId = AppDelegate.PushDeviceToken.Description
                                         .Trim('<', '>').Replace(" ", string.Empty).ToUpperInvariant();
                    var installation = new DeviceInstallation
                    {
                        InstallationId = client.InstallationId,
                        Platform       = "apns",
                        PushChannel    = registrationId
                    };
                    // Set up tags to request
                    installation.Tags.Add("topic:Sports");
                    // Set up templates to request
                    PushTemplate genericTemplate = new PushTemplate
                    {
                        Body = @"{""aps"":{""alert"":""$(message)"",""picture"":""$(picture)""}}"
                    };
                    installation.Templates.Add("genericTemplate", genericTemplate);

                    PushTemplate pushToSyncTemplate = new PushTemplate
                    {
                        Body = @"{""aps"":{""op"":""$(op)"",""table"":""$(table)"",""id"":""$(id)""},""content-available"":1}"
                    }
                    installation.Templates.Add("pushToSync", pushToSyncTemplate);

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

                    System.Diagnostics.Debug.WriteLine("Completed NH Push Installation");
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.Fail($"[iOSPlatformProvider]: Could not register with NH: {ex.Message}");
                }
            }
            else
            {
                System.Diagnostics.Debug.WriteLine($"[iOSPlatformProvider]: Are you running in a simulator?  Push Notifications are not available");
            }
        }
Пример #11
0
        public static async Task <HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, Routes.Post, Route = Routes.PublishContent)] DocumentUpdatedMessage updateMessage,
            [NotificationHub(ConnectionStringSetting = EnvironmentVariables.AzureWebJobsNotificationHubsConnectionString, /*Platform = NotificationPlatform.Apns,*/ TagExpression = "{NotificationTags}")] IAsyncCollector <Notification> notification,
            TraceWriter log)
        {
            log.Info(updateMessage?.ToString());

            UserStore userStore = null;

            var userId = Thread.CurrentPrincipal.GetClaimsIdentity()?.UniqueIdentifier();

            if (!string.IsNullOrEmpty(userId))
            {
                log.Info($"User is authenticated and has userId: {userId}");

                userStore = await DocumentClient.GetUserStore(userId, log);
            }


            if (!userStore?.UserRole.CanWrite() ?? false)
            {
                log.Info("Not authenticated");

                throw new HttpResponseException(HttpStatusCode.Unauthorized);
            }

            try
            {
                FunctionExtensions.HasValueOrThrow(updateMessage?.CollectionId, DocumentUpdatedMessage.CollectionIdKey);

                var template = PushTemplate.FromMessage(updateMessage);

                await notification.AddAsync(new TemplateNotification (template.GetProperties()));

                throw new HttpResponseException(HttpStatusCode.Accepted);
            }
            catch (HttpResponseException response)
            {
                return(response.Response);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                throw;
            }
        }
        async Task RegisterForAzurePushNotifications(NSData deviceToken)
        {
            if (deviceToken == null)
            {
                return;
            }


            var registrationId = deviceToken.Description
                                 .Trim('<', '>').Replace(" ", string.Empty).ToUpperInvariant();

            var push = _client.GetPush();

            var installation = new DeviceInstallation
            {
                InstallationId = _client.InstallationId,
                Platform       = "apns",
                PushChannel    = registrationId
            };

            // Set up tags to request
            installation.Tags.Add("topic:Sports");
            // Set up templates to request
            var genericTemplate = new PushTemplate
            {
                Body = "{\"aps\":{\"alert\":\"$(messageParam)\"}}"
            };

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

            try
            {
                // Register with NH
                var response = await _client.InvokeApiAsync <DeviceInstallation, DeviceInstallation>(
                    $"/push/installations/{_client.InstallationId}",
                    installation,
                    HttpMethod.Put,
                    new Dictionary <string, string>());
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Fail($"[iOSPlatformProvider]: Could not register with NH: {ex.Message}");
            }
        }
Пример #13
0
        public static bool PushTemplate(PushTemplate data, PushTemplateLog template)
        {
            var result = false;

            try
            {
                using (var client = new WeiXinPushClient())
                {
                    var getResult = client.PushByTemplate(data, template);
                    getResult.ThrowIfException(true);
                    result = getResult.Result;
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }
            return(result);
        }
        public DeviceInstallation GetDeviceRegistration(params string[] tags)
        {
            if (!_notificationsSupported())
            {
                throw new Exception(_getNotificationSupportError());
            }

            var installationId = GetDeviceId();
            var token          = _getDeviceToken();

            if (token == null)
            {
                return(null);
            }

            var pushChannel = NSDataToHex(token);

            var installation = new DeviceInstallation
            {
                InstallationId = installationId,
                Platform       = "apns",
                PushChannel    = pushChannel
            };

            installation.Tags.AddRange(tags);

            PushTemplate genericTemplate = new PushTemplate
            {
                Body = "{\"aps\":{\"alert\":\"$(alertMessage)\"}, \"action\": \"$(alertAction)\"}"
            };

            PushTemplate silentTemplate = new PushTemplate
            {
                Body = "{\"aps\":{\"content-available\":1, \"apns-priority\": 5, \"sound\":\"\", \"badge\": 0}, \"message\": \"$(silentMessage)\", \"action\": \"$(silentAction)\"}"
            };

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

            return(installation);
        }
Пример #15
0
        public static async Task Run(
            [QueueTrigger(MessageQueues.DocumentUpdate, Connection = EnvironmentVariables.AzureWebJobsStorage)] DocumentUpdatedMessage updateMessage,
            [NotificationHub(ConnectionStringSetting = EnvironmentVariables.AzureWebJobsNotificationHubsConnectionString, TagExpression = "{NotificationTags}")] IAsyncCollector <Notification> notification,
            TraceWriter log)
        {
            try
            {
                log.Info(updateMessage?.ToString());

                FunctionExtensions.HasValueOrThrow(updateMessage?.CollectionId, DocumentUpdatedMessage.CollectionIdKey);

                var template = PushTemplate.FromMessage(updateMessage);

                await notification.AddAsync(new TemplateNotification (template.GetProperties()));
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                throw;
            }
        }
Пример #16
0
        public DeviceInstallation GetDeviceRegistration(params string[] tags)
        {
            if (!_playServicesAvailable())
            {
                throw new Exception(_getPlayServicesError());
            }

            var installationId = GetDeviceId();
            var token          = _getFirebaseToken();

            if (token == null)
            {
                return(null);
            }

            var installation = new DeviceInstallation
            {
                InstallationId = installationId,
                Platform       = "fcm",
                PushChannel    = token
            };

            installation.Tags.AddRange(tags);

            PushTemplate genericTemplate = new PushTemplate
            {
                Body = "{\"data\":{\"message\":\"$(alertMessage)\", \"action\":\"$(alertAction)\"}}"
            };

            PushTemplate silentTemplate = new PushTemplate
            {
                Body = "{\"data\":{\"message\":\"$(silentMessage)\", \"action\":\"$(silentAction)\", \"silent\":\"true\"}}"
            };

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

            return(installation);
        }
Пример #17
0
        public async Task RegisterForPushNotifications(MobileServiceClient client)
        {
            if (AppDelegate.PushDeviceToken != null)
            {
                try
                {
                    var registrationId = AppDelegate.PushDeviceToken.Description
                                         .Trim('<', '>').Replace(" ", string.Empty).ToUpperInvariant();
                    var installation = new DeviceInstallation
                    {
                        InstallationId = client.InstallationId,
                        Platform       = "apns",
                        PushChannel    = registrationId
                    };
                    // Set up tags to request
                    installation.Tags.Add("silent-push");
                    // Set up templates to request
                    PushTemplate silentTemplate = new PushTemplate
                    {
                        Body = @"{""data"":{""op"":""$(op)"",""table"":""$(table)"",""id"":""$(id)""}}"
                    };

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

                    // Register with NH
                    var response = await client.InvokeApiAsync <DeviceInstallation, DeviceInstallation>(
                        $"/push/installations/{client.InstallationId}",
                        installation,
                        HttpMethod.Put,
                        new Dictionary <string, string>());
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.Fail($"[iOSPlatformProvider]: Could not register with NH: {ex.Message}");
                }
            }
        }
Пример #18
0
        public async Task RegisterForPushNotifications(MobileServiceClient client)
        {
            if (AppDelegate.PushDeviceToken != null)
            {
                try
                {
                    var registrationId = AppDelegate.PushDeviceToken.Description
                                         .Trim('<', '>').Replace(" ", string.Empty).ToUpperInvariant();
                    var installation = new DeviceInstallation
                    {
                        InstallationId = client.InstallationId,
                        Platform       = "apns",
                        PushChannel    = registrationId
                    };
                    // Set up tags to request
                    installation.Tags.Add("topic:Sports");
                    // Set up templates to request
                    PushTemplate genericTemplate = new PushTemplate
                    {
                        Body = "{\"aps\":{\"alert\":\"$(messageParam)\"}}"
                    };
                    installation.Templates.Add("genericTemplate", genericTemplate);
                    // Register with NH
                    var recordedInstallation = await client.InvokeApiAsync <DeviceInstallation, DeviceInstallation>(
                        $"/push/installations/{client.InstallationId}",
                        installation,
                        HttpMethod.Put,
                        new Dictionary <string, string>());

                    System.Diagnostics.Debug.WriteLine("Completed NH Push Installation");
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.Fail($"[iOSPlatformProvider]: Could not register with NH: {ex.Message}");
                }
            }
        }
Пример #19
0
        public static async Task <bool> PushByRegidAsync(IEnumerable <string> target, PushTemplate template)
        {
            using (var client = new Tuhu.Service.Push.TemplatePushClient())
            {
                var result = await client.PushByRegidAsync(target, template);

                result.ThrowIfException(true);
                return(result.Result);
            }
        }