Azure通知中心的Windows Phone推送通知

时间:2014-11-26 08:42:03

标签: c# azure windows-phone-8 notifications push-notification

我是Android应用开发者。现在,我正在为Windows手机开发应用。我是第一次通过Azure通知集成在Windows Phone中集成推送通知。我想要通知中心的帮助。如何整合?如何向所有设备发送通知而不是所有设备?

我是综合通知但一段时间后通知停止了。我已经完成了许多通知中心凭据。现在通知即将到来。

    private async void Application_Launching(object sender, LaunchingEventArgs e)
    {
        StorageFile MyDBFile = null;
        try
        {
            // Read the db file from DB path
            MyDBFile = await StorageFile.GetFileFromPathAsync(DB_PATH);
        }
        catch (FileNotFoundException)
        {
            if (MyDBFile == null)
            {
                // Copy file from installation folder to local folder.
                IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
                // Create a stream for the file in the installation folder.
                using (Stream input = Application.GetResourceStream(new Uri(DbConst.DATABASE, UriKind.Relative)).Stream)
                {
                    // Create a stream for the new file in the local folder.
                    using (IsolatedStorageFileStream output = iso.CreateFile(DB_PATH))
                    {
                        // Initialize the buffer.
                        byte[] readBuffer = new byte[4096];
                        int bytesRead = -1;

                        // Copy the file from the installation folder to the local folder. 
                        while ((bytesRead = input.Read(readBuffer, 0, readBuffer.Length)) > 0)
                        {
                            output.Write(readBuffer, 0, bytesRead);
                        }
                    }
                }
            }
        }


        var channel = HttpNotificationChannel.Find("MyPushChannel");
        if (channel == null)
        {
            channel = new HttpNotificationChannel("MyPushChannel");
            channel.Open();
            channel.BindToShellToast();
        }

        channel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(async (o, args) =>
        {
            //var hub = new NotificationHub("<hub name>", "<connection string>");
            var hub = new NotificationHub("mnokhgjhjhjbohkjkl", "Endpoint=sb://connect-hub.servicebus.windows.net/;SharedAccessKeyName=DefaultListenSharedAccessSignature;SharedAccessKey=RM6jjnbjnbjAnjhttD4yxqnknknklmkmnkkmkkkmmkbnl5rSk=");
            Registration x= await hub.RegisterNativeAsync(args.ChannelUri.ToString());
            //mChennelURI = x.ChannelUri;
            Debug.WriteLine("Chennel URI: " + x.ChannelUri);

            Deployment.Current.Dispatcher.BeginInvoke(() =>
            { 
                System.Diagnostics.Debug.WriteLine(args.ChannelUri.ToString());

            });


        });
        channel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(async (o,args) =>
        {
            // Error handling logic for your particular application would be here.
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                // Show the notification since toasts aren't 
                // displayed when the app is currently running.
                MessageBox.Show(String.Format("A push notification {0} error occurred.  {1} ({2}) {3}", args.ErrorType, args.Message, args.ErrorCode,args.ErrorAdditionalData));


            });
        });

        // Handle the event that is raised when a toast is received.
        channel.ShellToastNotificationReceived +=new EventHandler<NotificationEventArgs>((o, args) =>
            {
                string message = "";
                foreach (string key in args.Collection.Keys)
                {
                    message += args.Collection[key] + ": ";
                }

                //string[] separators = { "#"};
               // string[] words = message.Split(separators, StringSplitOptions.RemoveEmptyEntries);
               // string type = words[0];
               // string title = words[1];
               // string body = words[2];
               // string attach = words[3];
               //string date = words[4];
                //string time = words[5];
                //string msdId = words[6];
                //string groupIDS = words[7];

                //foreach (var word in words)
                    //Console.WriteLine(word);
                Console.WriteLine("Push notification : " + message);
                Debug.WriteLine("Push notification : " + message);
                Debugger.Log(1, "Push", "Push notification : " + message);
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    // Show the notification since toasts aren't 
                    // displayed when the app is currently running.
                    MessageBox.Show(message);
                    //<Temp>
                   // DatabaseHelperClass dbHelper = new DatabaseHelperClass();
                   // dbHelper.Insert(new Msg(msdId, groupIDS, type, title, body, attach, date, time, Constants.NO));
                    //</Temp>

                });

            });

    }

0 个答案:

没有答案