Inheritance: IRawNotification
コード例 #1
0
        /// <summary>
        /// Phương thức này sẽ chạy khi Trigger được kích hoạt, không cần gọi tới nó
        /// </summary>
        /// <param name="taskInstance">hệ thống sẽ tự động tạo instance này khi chạy</param>
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            //nếu sử dụng một phương thức async nào trong hàm này, hàm run sẽ kết thúc trước khi phương thức đó thực hiện xong.
            // sử dụng defferal để thông báo với hệ thống rằng chưa được phép kết thúc phương thức run
            // nếu bạn không sử dụng phương thức async nào, bạn có thể bỏ defferal đi.
            BackgroundTaskDeferral defferal = taskInstance.GetDeferral();

            System.Diagnostics.Debug.WriteLine("Đã nhận được toast");

            // TODO: làm những thứ bạn muốn trong background task ở đây
            // Lưu ý: tất cả các phương thức async nào nằm ngoài khu này đều sẽ không thực hiện được
            Windows.Networking.PushNotifications.RawNotification notification = taskInstance.TriggerDetails as Windows.Networking.PushNotifications.RawNotification;
            string content = notification.Content;

            try
            {
                // lấy id
                long notificationid = long.Parse(content);
                System.Diagnostics.Debug.WriteLine("Notification ID : " + notificationid.ToString());

                await MobileInterface.MobileInterface.ReceivedNewNotificatonID(notificationid);
            }
            catch (Exception ex)
            {
                // đường truyền lỗi nên không nhận được id
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }
            // sau khi xử lí xong, ta thông báo với hệ thống là hàm run đã thực hiện xong và hệ thống có thể đóng hàm run lại
            //việc này đồng nghĩa với background task sẽ kết thúc
            defferal.Complete();
        }
 /// <summary>
 /// Extrahiert den Inhalt der RawNotification und gibt die Notification
 /// in Form eines PushMessage Objekts an den Aufrufer zurück.
 /// </summary>
 /// <param name="receivedNotification">Die empfangene RawNotification als Objekt.</param>
 /// <returns>Eine Instanz der Klasse PushMessage.</returns>
 public PushMessage GetPushMessageFromNotification(RawNotification receivedNotification)
 {
     // Parse JSON Inhalt der Notification. 
     string notificationJsonContent = receivedNotification.Content;
     PushMessage pushMessage = jsonParser.ParsePushMessageFromJson(notificationJsonContent);
     
     return pushMessage;
 }