Message Retrieve Problem WPF?
I folks,
I developed a desktop-based Application. When Application runs automatically login. whenever I get message 2nd message onwards. every time the application opened any other people sent me the message I can't get properly. this problem comes once I opened an application.
for ex:
1. User1 -----> User2 -- message not comes
2. User1 -----> User2 -- message Received
3. User1 -----> User2 -- message Received
using Rainbow.Model;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Speech.Synthesis;
using System.Threading;
using System.Windows;
using Windows.UI.Notifications;
namespace WpfApp1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private static Mutex mutex = null;
const string AppName = "Notification POC";
SpeechSynthesizer speechSynthesizerObj;
private System.Windows.Forms.NotifyIcon _notifyIcon;
#region Declaration
MessageActionUpdateProcess messageupdateprocess = new MessageActionUpdateProcess();
Rainbow.Application rainbowApplication = new Rainbow.Application();
Rainbow.Contacts rainbowContacts; // To store Rainbow Contacts object
Rainbow.Conversations rainbowConversations; // To store Rainbow Conversations object
Rainbow.InstantMessaging rainbowInstantMessaging; // To store Rainbow InstantMessaging object
Rainbow.Invitations rainbowinvitations;
Rainbow.Model.Conversation rainbowConversation = new Conversation();
Contact rainbowMyContact; // To store My contact (i.e. the one connected to Rainbow Server)
List<Contact> rainbowContactsList; // To store contacts list of my roster
List<Conversation> rainbowConversationsList;// To store conversations list
string APP_ID = Convert.ToString(ConfigurationManager.AppSettings["ApplicationID"]);
string APP_SECRET_KEY = Convert.ToString(ConfigurationManager.AppSettings["ApplicationSecretKey"]);
string HOST_NAME = Convert.ToString(ConfigurationManager.AppSettings["Rainbow.Api.Url"]);
string LOGIN_USER = "Email"; // Convert.ToString(ConfigurationManager.AppSettings["FromEmailAddress"]);
string PASSWORD_USER = "Password"; // Convert.ToString(ConfigurationManager.AppSettings["EmailPassWord"]);
#endregion
public MainWindow()
{
InitializeComponent();
InitializeSDK();
AppLogin();
}
public void AppLogin()
{
if (!rainbowApplication.IsConnected())
{
rainbowApplication.Login(LOGIN_USER, PASSWORD_USER, callback =>
{
if (callback.Result.Success)
{
}
});
}
}
#region InitializeSDK
public void InitializeSDK()
{
string tempFolder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
string appFolderPath = Path.Combine(tempFolder, "Rainbow.CSharp.SDK");
rainbowApplication = new Rainbow.Application(appFolderPath);
rainbowApplication.SetTimeout(10000);
rainbowApplication.SetApplicationInfo(APP_ID, APP_SECRET_KEY);
rainbowApplication.SetHostInfo(HOST_NAME);
// Get Rainbow main objects
rainbowContacts = rainbowApplication.GetContacts();
rainbowConversations = rainbowApplication.GetConversations();
rainbowInstantMessaging = rainbowApplication.GetInstantMessaging();
rainbowinvitations = rainbowApplication.GetInvitations();
// EVENTS WE WANT TO MANAGE
rainbowApplication.ConnectionStateChanged += RainbowApplication_ConnectionStateChanged;
rainbowContacts.RosterContactAdded += RainbowContacts_RosterContactAdded;
rainbowInstantMessaging.MessageReceived += RainbowInstantMessaging_MessageReceived;
rainbowContactsList = new List<Contact>();
}
#endregion
#region RainbowContacts_RosterContactAdded
private void RainbowContacts_RosterContactAdded(object sender, Rainbow.Events.JidEventArgs e)
{
}
#endregion
#region RainbowApplication_ConnectionStateChanged
private void RainbowApplication_ConnectionStateChanged(object sender, Rainbow.Events.ConnectionStateEventArgs e)
{
string applicationDir = IconLocation();
if (e.State == Rainbow.Model.ConnectionState.Connected)
{
_notifyIcon.Icon = new System.Drawing.Icon(applicationDir + "/Content/mywilco-gr.ico");
}
else if (e.State == Rainbow.Model.ConnectionState.Connecting)
{
}
else if (e.State == Rainbow.Model.ConnectionState.Disconnected)
{
_notifyIcon.Icon = new System.Drawing.Icon(applicationDir + "/Content/mywilco-rd.ico");
rainbowContactsList.Clear();
if (rainbowConversationsList != null)
{
rainbowConversationsList.Clear();
}
AppLogin();
}
}
#endregion
#region RainbowInstantMessaging_MessageReceived
private void RainbowInstantMessaging_MessageReceived(object sender, Rainbow.Events.MessageEventArgs e)
{
try
{
String Msgcontent = e.Message.Content;
string messageId = "";
messageId = e.Message.Id;
}
catch (Exception ex)
{
MessageBox.Show("LOGIN_USER:- " + LOGIN_USER + " && PASSWORD_USER:- " + PASSWORD_USER + " && app.xaml Received " + " " + ex.Message);
}
}
#endregion
}
}
I get the second message onwards. First Message did not get it. How to Fix this?
Regards
Dinesh D
-
Hi,
I notice several pb in the code snippet provided ...1) You call twice InitializeComponent() (in App() method and in InitializeSDK())
2) Create main SDK objects (Contacts, Conversations, InstantMessaing, etc ...) before to use login method
3) Define events (ConnectionStateChanged, MessageReceived , etc ...) also before to use login method
4) As described in the documentation here you need to received event InitializationPerformed from Application object before to use any feature of the SDK
Once all thess problems has been corrected, do you still have the same trouble ?
-
Christophe IRLES Exactly I'm using this code in my Project Still am getting the same issue. Pls, Guide me.
using log4net;
using Rainbow.Model;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Windows;
using RainbowModel = Rainbow;using System.ComponentModel;
using System.Management;namespace WpfApp1
{public partial class App : Application
{
Rainbow.Application rainbowApplication = new RainbowModel.Application();
RainbowModel.Contacts rainbowContacts; // To store Rainbow Contacts object
RainbowModel.Conversations rainbowConversations; // To store Rainbow Conversations object
RainbowModel.InstantMessaging rainbowInstantMessaging; // To store Rainbow InstantMessaging object
RainbowModel.Invitations rainbowinvitations;RainbowModel.Model.Conversation rainbowConversation=new Conversation();
Contact rainbowMyContact; // To store My contact (i.e. the one connected to Rainbow Server)
List<Contact> rainbowContactsList; // To store contacts list of my roster
List<Conversation> rainbowConversationsList;// To store conversations list
string APP_ID = Convert.ToString(ConfigurationManager.AppSettings["ApplicationID"]);
string APP_SECRET_KEY = Convert.ToString(ConfigurationManager.AppSettings["ApplicationSecretKey"]);
string HOST_NAME = Convert.ToString(ConfigurationManager.AppSettings["Rainbow.Api.Url"]);string LOGIN_USER = "";
string PASSWORD_USER = "";
// [STAThread]
public App()
{
try
{
InitializeSDK();}
catch(Exception ex)
{
MessageBox.Show("app.xaml App " + " " + ex.Message);
}
}public void InitializeSDK()
{this.InitializeComponent();
rainbowApplication = new Rainbow.Application();
rainbowApplication.SetApplicationInfo(APP_ID, APP_SECRET_KEY);
rainbowApplication.SetHostInfo(HOST_NAME);// Get Rainbow main objects
rainbowContacts = rainbowApplication.GetContacts();
rainbowConversations = rainbowApplication.GetConversations();
rainbowInstantMessaging = rainbowApplication.GetInstantMessaging();
rainbowinvitations = rainbowApplication.GetInvitations();if (!rainbowApplication.IsConnected())
{
rainbowApplication.Login(LOGIN_USER, PASSWORD_USER, callback =>
{
if (callback.Result.Success)
{
rainbowContacts = rainbowApplication.GetContacts();
rainbowConversations = rainbowApplication.GetConversations();
rainbowInstantMessaging = rainbowApplication.GetInstantMessaging();
rainbowinvitations = rainbowApplication.GetInvitations();
rainbowApplication.ConnectionStateChanged += RainbowApplication_ConnectionStateChanged;
rainbowInstantMessaging.MessageReceived += RainbowInstantMessaging_MessageReceived;
rainbowContacts.RosterContactAdded += RainbowContacts_RosterContactAdded;rainbowContactsList = new List<Contact>();
}
});
}
}private void RainbowInstantMessaging_MessageReceived(object sender, Rainbow.Events.MessageEventArgs e)
{
try
{}
catch(exception ex)
{
}
}
-
Most of the code I am used from https://github.com/Rainbow-CPaaS/Rainbow-CSharp-SDK-Samples/blob/master/Windows_NetFwk/InstantMessaging/InstantMessaging.cs
-
Hi,
I have updated the InstantMessaging sample with an option to allow auto-logon when the application sample is started (previously you must have connected successfully with a valid account / password since we get info from the cache to get credentials)
There is no problem to receive the first incoming message (and all others).
Regards,
Christophe -
Hi Christophe IRLES,
I Already developed and tested in Windows Form Application it was working smoothly. I am getting a message first time also. but the only problem occurs in the WPF application. that only I am asking again. Please check once with the WPF application. thank you for your support.
Regards
Dinesh D
-
Rainbow C# SDK is built as a .Net Standard library. So it's compatible with any superior layer of .Net including WPF.
As example a full mobile application (for Android and IOS) is available in Xamarin Forms using this SDK without any trouble. I use this mobile Application every day on my own Android device for my work.
I spent already a lot of time with your question ... It would have been better to clearly describe it from the start.
And again, your latest code snippet shows a lot of your misunderstanding in coding in C #. I really think the problem is more here than in the SDK.
-
Hi, Christophe IRLES can you please validate my code. It was fulfilled as SDK required.
using System;
using System.Collections.Generic;
using Rainbow.Model;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Speech.Synthesis;
using System.Threading;
using System.Windows;
using Windows.UI.Notifications;
using Rainbow;namespace WpfApp1
{public partial class MainWindow : Window
{
private static Mutex mutex = null;
const string AppName = "Notification POC";string APP_ID = Convert.ToString(ConfigurationManager.AppSettings["ApplicationID"]);
string APP_SECRET_KEY = Convert.ToString(ConfigurationManager.AppSettings["ApplicationSecretKey"]);
string HOST_NAME = Convert.ToString(ConfigurationManager.AppSettings["Rainbow.Api.Url"]);string LOGIN_USER = "Email"; // Convert.ToString(ConfigurationManager.AppSettings["FromEmailAddress"]);
string PASSWORD_USER = "Password"; // Convert.ToString(ConfigurationManager.AppSettings["EmailPassWord"]);#region Declaration
// Define Rainbow objects
Rainbow.Application rainbowApplication; // To store Rainbow Application object
Contacts rainbowContacts; // To store Rainbow Contacts object
Conversations rainbowConversations; // To store Rainbow Conversations object
InstantMessaging rainbowInstantMessaging; // To store Rainbow InstantMessaging objectList<Contact> rainbowContactsList; // To store contacts list of my roster
#endregion
public MainWindow()
{
InitializeComponent();InitializeRainbowSDK();
AppLogin();
}#region InitializeSDK
public void InitializeRainbowSDK()
{
rainbowApplication = new Rainbow.Application();
rainbowApplication.SetTimeout(10000);rainbowApplication.SetApplicationInfo(APP_ID, APP_SECRET_KEY);
rainbowApplication.SetHostInfo(HOST_NAME);// Get Rainbow main objects
rainbowContacts = rainbowApplication.GetContacts();
rainbowConversations = rainbowApplication.GetConversations();
rainbowInstantMessaging = rainbowApplication.GetInstantMessaging();// EVENTS WE WANT TO MANAGE
rainbowApplication.ConnectionStateChanged += RainbowApplication_ConnectionStateChanged;
rainbowContacts.RosterContactAdded += RainbowContacts_RosterContactAdded;
rainbowInstantMessaging.MessageReceived += RainbowInstantMessaging_MessageReceived;rainbowContactsList = new List<Contact>();
}
#endregionpublic void AppLogin()
{
if (!rainbowApplication.IsConnected())
{
rainbowApplication.Login(LOGIN_USER, PASSWORD_USER, callback =>
{
if (callback.Result.Success)
{
//string logLine = String.Format("Impossible to logout:\r\n{0}", Util.SerialiseSdkError(callback.Result));
//AddStateLine(logLine);
//log.WarnFormat(logLine);
}
});
}
}
#region RainbowApplication_ConnectionStateChangedprivate void RainbowApplication_ConnectionStateChanged(object sender, Rainbow.Events.ConnectionStateEventArgs e)
{
//string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
//string basedir = System.AppDomain.CurrentDomain.BaseDirectory;string applicationDir = IconLocation();
if (e.State == Rainbow.Model.ConnectionState.Connected)
{
// _notifyIcon.Icon = new System.Drawing.Icon(applicationDir + "/Content/mywilco-gr.ico");
}
else if (e.State == Rainbow.Model.ConnectionState.Connecting)
{}
else if (e.State == Rainbow.Model.ConnectionState.Disconnected)
{// _notifyIcon.Icon = new System.Drawing.Icon(applicationDir + "/Content/mywilco-rd.ico");
// rainbowContactsList.Clear();
AppLogin();}
}
#endregion
#region RainbowContacts_RosterContactAdded
private void RainbowContacts_RosterContactAdded(object sender, Rainbow.Events.JidEventArgs e)
{
// AddStateLine($"A Contact has been added:[{e.Jid}]");
//UpdateContactsListComboBox();
}
#endregion#region RainbowInstantMessaging_MessageReceived
private void RainbowInstantMessaging_MessageReceived(object sender, Rainbow.Events.MessageEventArgs e)
{
try
{if (e.CarbonCopy == false)
{String Msgcontent = e.Message.Content;
string messageId = "";
}
}
catch (Exception ex)
{
MessageBox.Show("LOGIN_USER:- " + LOGIN_USER + " && PASSWORD_USER:- " + PASSWORD_USER + " && app.xaml Received " + " " + ex.Message);
}}
#endregion}
}
Bitte melden Sie sich an, um einen Kommentar zu hinterlassen.
Kommentare
10 Kommentare