Exemplo n.º 1
0
        //Part: 7 Push Notification
        //private async Task InitNotificationsAsync()
        //{
        //    PushNotificationChannel channel = await Windows.Networking.PushNotifications.PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();

        //    //messageParam: is defined in edit favorite script from azure
        //    const string templateBodyWNS = "{\"message\":\"$(messageParam)\"}";
        //    //const string templateBodyWNS = "<toast><visual><binding template=\"ToastText01\"><text id=\"1\">$(messageParam)</text></binding></visual></toast>";

        //    Push push = FavoritesManager.DefaultManager.CurrentClient.GetPush();

        //    JObject headers = new JObject();
        //    headers["X-WNS-Type"] = "wns/raw";

        //    JObject templates = new JObject();
        //    templates["genericMessage"] = new JObject
        //            {
        //                {"body", templateBodyWNS},
        //                {"headers", headers}
        //            };

        //    await push.RegisterAsync(channel.Uri, templates);
        //    channel.PushNotificationReceived += OnNotificationReceived;
        //}

        //Part: 7 Push Notification
        //private void OnNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs args)
        //{
        //    //args.NotificationType
        //    //args.RawNotification

        //    //responding to the notification: show to device
        //    Helpers.ToastHelper.ProcessNotification(args);
        //}

        //Part 9: Adding Tagging Support: Pre-provisioned
        public async Task RegisterForPushNotificationsAsync(MobileServiceClient client)
        {
            PushNotificationChannel channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();

            if (channel != null)
            {
                try
                {
                    string             registrationId = channel.Uri.ToString();
                    DeviceInstallation installation   = new DeviceInstallation
                    {
                        InstallationId = client.InstallationId,
                        Platform       = "wns",
                        PushChannel    = registrationId
                    };

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

                    genericTemplate.Headers.Add("X-WNS-Type", "wns/raw");
                    installation.Templates.Add("genericTemplate", genericTemplate);

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

                    discussionTemplate.Headers.Add("X-WNS-Type", "wns/raw");
                    installation.Templates.Add("discussionTemplate", discussionTemplate);

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

                    DeviceInstallation recordedInstallation = 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);

                    channel.PushNotificationReceived += OnNotificationReceived;
                }
                catch (Exception ex)
                {
                }

                return;
            }
        }
Exemplo n.º 2
0
        public async Task RegisterForPushNotifications(MobileServiceClient client)
        {
            if (UWPPlatformProvider.Channel != null)
            {
                try
                {
                    var registrationId = UWPPlatformProvider.Channel.Uri.ToString();
                    var installation   = new DeviceInstallation
                    {
                        InstallationId = client.InstallationId,
                        Platform       = "wns",
                        PushChannel    = registrationId
                    };
                    // Set up tags to request
                    installation.Tags.Add("topic:Events");
                    // Set up templates to request
                    var genericTemplate = new WindowsPushTemplate
                    {
                        Body = @"<toast>
                                    <visual>
                                        <binding template=""ToastText01"">
                                            <text id=""1"">Microsoft House</text>
                                            <text id=""1"">$(message)</text>
                                        </binding>
                                    </visual>
                                </toast>"
                    };
                    genericTemplate.Headers.Add("X-WNS-Type", "wns/toast");

                    installation.Templates.Add("ToastText01", 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($"[UWPPlatformProvider]: Could not register with NH: {ex.Message}");
                }
            }
        }