How to read Toast Notification attributes in Windows Phone 8.1? - windows

I have created below toast notification
ToastTemplateType toastType = ToastTemplateType.ToastText02;
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastType);
XmlNodeList toastTextElement = toastXml.GetElementsByTagName("text");
toastTextElement[0].AppendChild(toastXml.CreateTextNode("Hello C# Corner"));
toastTextElement[1].AppendChild(toastXml.CreateTextNode("I am poping you from a Winmdows Phone App"));
IXmlNode toastNode = toastXml.SelectSingleNode("/toast");
((XmlElement)toastNode).SetAttribute("duration", "long");
((XmlElement)toastNode).SetAttribute("type", "Employee");
((XmlElement)toastNode).SetAttribute("launch", "<cat state='angry'><facebite state='true' /></cat>");
ToastNotification toast = new ToastNotification(toastXml);
ToastNotificationManager.CreateToastNotifier().Show(toast);
I want to know how to access the type attribute.
When I tried with below code it's returning null
var att = toast.Content.Attributes;

Debugging shows you the answer.
var att = toast.Content.ChildNodes[0].Attributes[1].NodeValue;

var att =
toast.Content
.SelectSingleNode("/toast/visual/binding")
.SelectNodes("text")[0].InnerText

Related

Toast notification not working in Xamarin UWP Windows app

I have UWP Windows application, developed under the Xamarin.forms. I have implemented the Toast notifications but I am facing the issue with this. In some Windows 10 systems, it is working and showing the toast notification properly, but in some of the Windows 10 systems (even having the same Windows 10 OS update) it is not working.
Below first code snippets that I have implemented in the Native UWP.
string msg = "Toast Notification Header";
string subMsg = "Toast Notification Title";
var toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
var toastTextElements = toastXml.GetElementsByTagName("text");
toastTextElements[0].AppendChild(toastXml.CreateTextNode(msg));
toastTextElements[1].AppendChild(toastXml.CreateTextNode(subMsg));
//To play the custom sound
var toastNode = toastXml.SelectSingleNode("/toast");
var audio = toastXml.CreateElement("audio");
audio.SetAttribute("src", "ms-appx:///Assets/incoming_message.wav");
audio.SetAttribute("loop", "false");
toastNode.AppendChild(audio);
var toast = new ToastNotification(toastXml);
ToastNotificationManager.CreateToastNotifier().Show(toast);
Below second code snippets that I have implemented in the Native UWP.
// "With Microsoft.Toolkit.Uwp.Notifications"
// Construct the toast content
ToastContent toastContent = new ToastContent()
{
Visual = new ToastVisual()
{
BindingGeneric = new ToastBindingGeneric()
{
Children =
{
new AdaptiveText()
{
Text = "Toast Notification Header"
},
new AdaptiveText()
{
Text = "Toast Notification Content"
}
}
}
}
};
bool supportsCustomAudio = true;
// If we're running on Desktop before Version 1511, do NOT include custom audio
// since it was not supported until Version 1511, and would result in a silent toast.
if (AnalyticsInfo.VersionInfo.DeviceFamily.Equals("Windows.Desktop")
&& !ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 2))
{
supportsCustomAudio = false;
}
if (supportsCustomAudio)
{
toastContent.Audio = new ToastAudio()
{
Src = new Uri("ms-appx:///Assets/incoming_message.wav")
};
}
// And create the toast notification
ToastNotification notification = new ToastNotification(toastContent.GetXml());
// And then send the toast
ToastNotificationManager.CreateToastNotifier().Show(notification);
Above code snips showing the Toast notification in some Windows 10 system and not working in some other Windows 10 system.
Kindly guide me on this. Thanks in advance.
Regards,
Vivek
Please follow these steps to add toast notification in UWP Project.
Step 1:- Create a new UWP project.
Step 2:- Go to the code-behind and add the namespace.
using Windows.UI.Notifications;
using
NotificationsExtensions.Toasts;
Step 3:- I created a Toast Generic Template like the following code:
public static Windows.Data.Xml.Dom.XmlDocument CreateToast()
{
var xDoc = new XDocument(
new XElement("toast",
new XElement("visual",
new XElement("binding", new XAttribute("template", "ToastGeneric"),
new XElement("text", "C# Corner"),
new XElement("text", "Do you got MVP award?")
)
),// actions
new XElement("actions",
new XElement("action", new XAttribute("activationType", "background"),
new XAttribute("content", "Yes"), new XAttribute("arguments", "yes")),
new XElement("action", new XAttribute("activationType", "background"),
new XAttribute("content", "No"), new XAttribute("arguments", "no"))
)
)
);
var xmlDoc = new Windows.Data.Xml.Dom.XmlDocument();
xmlDoc.LoadXml(xDoc.ToString());
return xmlDoc;
}
Step 4:- Create a toast notification object using XML document.
var xmdock = CreateToast();
var toast = new ToastNotification(xmdock);
Next show the toast using ToastNotificationManager class.
var notifi = Windows.UI.Notifications.ToastNotificationManager.CreateToastNotifier();
notifi.Show(toast);
Step 5:- C# code-behind:
private void showToastBtn_Click(object sender, RoutedEventArgs e)
{
var xmdock = CreateToast();
var toast = new ToastNotification(xmdock);
var notifi = Windows.UI.Notifications.ToastNotificationManager.CreateToastNotifier();
notifi.Show(toast);
}
I hope the above code will be useful for you.
Thank you

V4 Botframework ActionTypes.PostBack shows selected option in chat

We are building a bot where ActionType.Postback is not working.
There are places in the Bot where it does work however the example attached does not.
The sample was build using Bot framework 4.1.5.
Any help appreciated.
if (turnContext.Activity.Type == ActivityTypes.Message)
{
if (turnContext.Activity.Text == "help")
{
var reply = turnContext.Activity.CreateReply();
reply.Text = $"Hello {turnContext.Activity.From.Name}! How can i help you today? ";
var welcomeCard = new HeroCard
{
Buttons = new List<CardAction>
{
new CardAction {Title = "option1", Value = "option1", Type = ActionTypes.PostBack},
new CardAction {Title = "option2", Value = "option2", Type = ActionTypes.PostBack},
new CardAction {Title = "option3", Value = "option3", Type = ActionTypes.PostBack}
}
}.ToAttachment();
reply.Attachments.Add(welcomeCard);
await turnContext.SendActivityAsync(reply, cancellationToken);
}
else
{
This appears to just be an error in the Bot Framework Emulator and should work fine if you publish your bot to another channel. I've gone ahead and submitted this as a bug to the development team. https://github.com/Microsoft/BotFramework-Emulator/issues/1140
Note that the PostBack Action only works in certain channels and will default to ImShow if it is not supported. In the channels where PostBack is not supported, the response text value will be visible to all participants in the conversation.

how to get inputs from wearable devices

I'm implementing a notification system using Xamarin platform, which extends to wearable devices to send the notification. I also want to get the input of user from the wear notification and i have programed it in such away that user can select text or use voice. i followed the following tutorial
http://developer.android.com/training/wearables/notifications/voice-input.html
my code is:
void SendWearNotification (string message, string from)
{
var valuesForActivity = new Bundle();
valuesForActivity.PutString ("message", message);
String groupkey = "group_key_emails";
var intent = new Intent (this, typeof(MyMainActivity));
intent.PutExtras (valuesForActivity);
intent.AddFlags (ActivityFlags.ClearTop);
var pendingIntent = PendingIntent.GetActivity (this, 0, intent, PendingIntentFlags.OneShot);
var builder = new NotificationCompat.Builder (this)
.SetAutoCancel (true)
.SetContentIntent (pendingIntent)
.SetContentTitle (from)
.SetSmallIcon (Resource.Drawable.Iconlogo)
.SetContentText (message) //message is the one recieved from the notification
.SetTicker(from)
.SetGroup (groupkey) //creates groups
.SetPriority((int)NotificationPriority.High);
//
//for viewing the message in second page
var pagestyle= new NotificationCompat.BigTextStyle();
pagestyle.SetBigContentTitle (from)
.BigText (messagefromapp); //message from app is the one rerieved from the wcf app
//second page
var secondpagenotification = new NotificationCompat.Builder (this)
.SetStyle (pagestyle)
.Build ();
//intent for voice input or text selection
var wear_intent = new Intent (Intent.ActionView);
var wear_pending_intent = PendingIntent.GetActivity (this,0,wear_intent,0);
// Create the reply action and add the remote input
setRemoteInput ();
var action = new NotificationCompat.Action.Builder (Resource.Drawable.ic_mes,
GetString (Resource.String.messages), wear_pending_intent)
.AddRemoteInput (remoteinput)
.Build ();
//add it to the notification builder
Notification notification = builder.Extend (new NotificationCompat.WearableExtender ()
.AddPage (secondpagenotification).AddAction(action)).Build ();
//create different notitfication id so that we can as list
if(notification_id<9){
notification_id += 1;
}else{
notification_id=0;
}
var notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
notificationManager.Notify (notification_id+2, notification);
}
this method is implmented inside GCMListnerService class.
According to the tutorial from the above link, i can retreive the input data user selected or spoke uing the following code:
private void getResponse(Intent intent){
Bundle remoteInput = RemoteInput.GetResultsFromIntent(intent);
if (remoteInput != null) {
Toast.MakeText(this, remoteInput.GetCharSequence(EXTRA_VOICE_REPLY), ToastLength.Short);
}
//return null;
}
My question is when do i call this method, how do i know if user have selected a text en send from the wearable device. if there is any event which i can use.
I got the solution. the method the gets the remote input (the "getresponse" in my case) should be called from the "Oncreate" method of an activity that is used when the notification is created. In my case the actvity i used is "MyMainActivity" when i create the intent of the notification as u can see it in the code. So this means the method will be called twice, when the application runs, and when user reponds from the wear. but ony in the second case will the "remoteinput.getResultfromIntent" will have a value. I hope it will help for someone with same issues.

Toast Notification From Background Task

I work on universal app that one of it's features is to send a notification to the user after a specific time he/she want . Notification appear when Set The System Event (i.e disable and enable wifi) as Follow
builder.SetTrigger(new SystemTrigger(SystemTriggerType.NetworkStateChange, false));
but when work with TimeTrigger The Notification Doesn't appear !
Here is my code :
var builder = new BackgroundTaskBuilder();
builder.Name = "MyBackgroundTask";
builder.TaskEntryPoint = "SampleWindowsStoreApp.BackgroundTask.MyBackgroundTask";
var _taskbuilder = new TimeTrigger(20, false);
builder.SetTrigger(_taskbuilder);
builder.Register();
knowing that I enable Toast From Package.appxmanifest and declare Timer and backgroundTask
It's my BackgroundTask code
public sealed class MyBackgroundTask : IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance)
{
var deferral = taskInstance.GetDeferral();
ToastNotificationUtilities.ShowMessage("Hello from the background task. ");
deferral.Complete();
}
}
I want to know why the notification that should appear after 20 min doesn't appear .!
Maybe you must use something like that
ToastTemplateType toastTemplate = ToastTemplateType.ToastText02;
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate); XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");
toastTextElements[0].AppendChild(toastXml.CreateTextNode("Hello from the background task. ")); ToastNotification toast = new ToastNotification(toastXml); ToastNotificationManager.CreateToastNotifier().Show(toast);
instand of: ToastNotificationUtilities.ShowMessage("Hello from the background task. ")

Background image Windows Phone 7

I have a little question, I want to change the background of my application with C#.
I tried this code :
var app = Application.Current as App;
var imageBrush = new ImageBrush
{
ImageSource = new BitmapImage(new Uri(imageName, UriKind.Relative))
};
app.RootFrame.Background = imageBrush;
But it doesn't work, the background is dark.. I tried to do :
app.RootFrame.Background = new SolidColorBrush(Colors.Blue);
And it works well. So I don't understand where is the problem, my image is 480*800 px and I set Build Action to Content and Copy to Output Directory to Copy if newer .
Thanks for all
maybe you can try it
var app = Application.Current as App;
if (app == null)
return;
var imageBrush = new ImageBrush
{
};
var uu = new BitmapImage(new Uri(imageName, UriKind.Relative));
uu.CreateOptions = BitmapCreateOptions.None;
imageBrush.ImageSource = uu;
app.RootFrame.Background = imageBrush;
Mark: uu.CreateOptions = BitmapCreateOptions.None;
I'm doing almost the exact same thing successfully with an image, except I'm setting the image as the background to a panorama control. I know someone else had the same problem you did in this post so I suggested setting the background of the LayoutRoot or other control instead of the app.RootFrame.
Thank you both !!
I had previously tried the two solutions separately and it didn't work. But together it works perfectly!
The code :
var imageBrush = new ImageBrush
{
};
var uu = new BitmapImage(new Uri("/Images/image.png", UriKind.Relative));
uu.CreateOptions = BitmapCreateOptions.None;
imageBrush.ImageSource = uu;
LayoutRoot.Background = imageBrush;

Resources