Example #1
0
 private void HandlePushNotificationEvent(PushNotificationEvent pushEvent)
 {
     lock (this)
     {
         this.SyncMessageId(pushEvent.ConversationId, pushEvent.MessageId);
     }
 }
        public void HandlePushNotificationExistingConversationTest()
        {
            List<List<ConversationModel>>  conversations = new List<List<ConversationModel>>();
            List<UserModel>  owners = new List<UserModel>();
            this.LoadConversations(conversations, owners);

            for (int i = 0; i < conversations.Count; i++)
            {
                if (conversations[i].Count == 0)
                {
                    return;
                }

                MockServiceProxy serviceProxy = new MockServiceProxy() { Conversations = conversations[i] };
                MockUserSettings userSettings = new MockUserSettings();
                MockDataContextWrapper dataContextWrapper = new MockDataContextWrapper(new MockDatabase() { Conversations = conversations[i] });
                userSettings.Save(owners[i]);

                using (AllConversationsViewModel allConversations = new AllConversationsViewModel(serviceProxy, userSettings, dataContextWrapper))
                {
                    allConversations.LoadInitialConversations();

                    NotifyCollectionChangedTester<ConversationModel> collectionChangedTester = new NotifyCollectionChangedTester<ConversationModel>(allConversations.Conversations);
                    long existingConversationId = conversations[i].Count > 0 ? conversations[i][0].ConversationId : long.MaxValue;
                    int existingConversationUserId = -1;

                    foreach (UserModel user in conversations[i][0].ConversationParticipants)
                    {
                        if (user.Id != owners[i].Id)
                        {
                            existingConversationUserId = user.Id;
                        }
                    }

                    foreach (ConversationModel conversation in conversations[i])
                    {
                        foreach (UserModel user in conversation.ConversationParticipants)
                        {
                            if (user.Id != owners[i].Id)
                            {
                                Assert.AreEqual(conversation, allConversations.GetConversationFromConversationId(conversation.ConversationId), "Conversation cannot be retrieved by guid");
                                Assert.AreEqual(conversation, allConversations.GetConversation(user.Id), "Conversation cannot be retrieved by recipient");
                            }
                        }
                    }

                    PushNotificationEvent pushEvent = new PushNotificationEvent(this, (long)2000, existingConversationId, "test", DateTime.Now.Ticks, "HandlePushNotificationExistingConversationTest" + i.ToString(), existingConversationUserId);
                    Messenger.Default.Send<PushNotificationEvent>(pushEvent);

                    Assert.AreEqual(collectionChangedTester.Count, 1, "Push notification was not handled. The new conversation was not added to the collection");
                }
            }
        }
        public void HandlePushNotificationForDifferentConversationTest()
        {
            for (int i = 0; i < this.conversationMessages.Count; i++)
            {
                for (int j = 0; j < this.conversationMessages[i].Count; j++)
                {

                    MockServiceProxy serviceProxy = new MockServiceProxy() { Messages = this.conversationMessages[i][j].m_Item2 };
                    MockUserSettings userSettings = new MockUserSettings();
                    userSettings.Save(owners[i]);

                    UserModel recipient = null;
                    foreach (UserModel user in this.conversationMessages[i][j].m_Item1.ConversationParticipants)
                    {
                        if (!user.Equals(userSettings.Me))
                        {
                            recipient = user;
                        }
                    }

                    ConversationMessagesViewModel messages = new ConversationMessagesViewModel(serviceProxy, userSettings, this.conversationMessages[i][j].m_Item1.ConversationId, recipient);

                    messages.LoadMessagesForConversations();

                    Random random = new Random((int)DateTime.Now.Ticks);
                    long differentConversationId = long.MaxValue;
                    int differentConversationUserId = random.Next(1000, 2000);

                    PushNotificationEvent pushEvent = new PushNotificationEvent(this, (long)2000, differentConversationId, "test", DateTime.Now.Ticks, "ConversationMessagesHandlePushNotificationTest" + i.ToString(), differentConversationUserId);

                    int messageCountBeforePush = messages.Messages.Count;
                    NotifyCollectionChangedTester<MessageModel> collectionChangedTester = new NotifyCollectionChangedTester<MessageModel>(messages.Messages);
                    Messenger.Default.Send<PushNotificationEvent>(pushEvent);

                    Assert.AreEqual(0, collectionChangedTester.Count, "Collection changed was generated when it should not be");
                    Assert.AreEqual(messageCountBeforePush, messages.Messages.Count, "Push notification was not handled properly");
                }
            }
        }
Example #4
0
        /// <summary>
        /// Event handler for when a toast notification arrives while your application is running.  
        /// The toast will not display if your application is running so you must add this
        /// event handler if you want to do something with the toast notification.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PushChannel_ShellToastNotificationReceived(object sender, NotificationEventArgs e)
        {
            // Parse out the information that was part of the message.
            string message = null;
            string messageSenderName = null;
            Guid conversationId = Guid.Empty;
            int senderId = -1;
            Guid messageId = Guid.Empty;
            long messagePostDate = -1;
            bool isGroup = false;
            string groupName = null;
            int groupId = -1;

            foreach (string key in e.Collection.Keys)
            {
                if (string.Compare(
                    key,
                    "wp:Param",
                    System.Globalization.CultureInfo.InvariantCulture,
                    System.Globalization.CompareOptions.IgnoreCase) == 0)
                {
                    PushNotification.GetMessageProperties(
                        e.Collection[key].Substring(PushNotification.RelativeUriPrefix.Length),
                        out conversationId,
                        out senderId,
                        out messageId,
                        out messagePostDate,
                        out groupId,
                        out groupName);

                    if (conversationId == Guid.Empty ||
                        messageId == Guid.Empty ||
                        senderId == -1 ||
                        messagePostDate == -1)
                    {
                        return;
                    }
                }

                if (string.Compare(
                    key,
                    "wp:Text2",
                    System.Globalization.CultureInfo.InvariantCulture,
                    System.Globalization.CompareOptions.IgnoreCase) == 0)
                {
                    message = e.Collection[key];
                }

                if (string.Compare(
                    key,
                    "wp:Text1",
                    System.Globalization.CultureInfo.InvariantCulture,
                    System.Globalization.CompareOptions.IgnoreCase) == 0)
                {
                    messageSenderName = e.Collection[key];
                }
            }

            // Handle the push only if it's not from this user
            // Or this is from group notifications
            if (senderId != UserSettingsModel.Instance.UserId)
            {
                PushNotificationEvent pushEvent =
                    new PushNotificationEvent(
                        this,
                        messageId,
                        conversationId,
                        message,
                        messagePostDate,
                        messageSenderName,
                        senderId,
                        groupId,
                        groupName);

                Messenger.Default.Send<PushNotificationEvent>(pushEvent);
                this.ClearTileCount();
            }
        }