Introduction:
A highly requested Notification Center feature is now available in WindowsPhone 8.1, Action Center will allow you to quickly access the settings pages like Airplane mode, Wi-Fi Settings, Bluetooth settings etc. This will also report you about alerts, non-toast notifications, update histories etc. Action Center is available via a swipe down from the top of your device, even from the lock screen.This is probably one of the longest article in my blog :) .Fortunately a developer can access 'Action Center' like following variety of ways.
- Sending a local toast notification to action center with popup .
- Sending a toast notification directly to action center, without showing a popup.
- Remove a notification from the Action Center.
- Update a notification from the Action Center.
Requirements:
- Make sure you’ve downloaded and installed the Windows Phone 8.1 SDK. For more information, see Get the SDK.
- I assumes that you’re going to test your app on the Windows Phone emulator. If you want to test your app on a phone, you have to take some additional steps. For more info, see Register your Windows Phone device for development.
- This post assumes you’re using Microsoft Visual Studio Express 2013 for Windows.
Description:
Previously in windowsphone 8.0,there was no action center on phone.One of the biggest changes in Windows Phone 8.1 was the introduction of the Windows Phone Action Center. So Action Center will allow you to quickly access the settings pages like Airplane mode, Wi-Fi Settings, Bluetooth settings etc.Ok its time to start development with following steps.
Step 1:
1)Open Microsoft Visual Studio Express 2013 for Windows.
2)Create new project using the "Blank App" template available under Visual C# -> Store Apps -> Windows Phone Apps. (for example project name :ActionCenterSample)
2)Create new project using the "Blank App" template available under Visual C# -> Store Apps -> Windows Phone Apps. (for example project name :ActionCenterSample)
Step 2:
Add following xaml code in MainPage.xaml page and run the app.
- <Grid Background="White">
- <ScrollViewer ZoomMode="Enabled">
- <StackPanel Margin="5" Orientation="Vertical">
- <!--Title of tutorial-->
- <TextBlock Margin="5" TextLineBounds="Full" FontSize="28" Text="WP8.1 ActionCenter Tutorials" Foreground="#FF1BC982" />
- <Rectangle Fill="Blue" Height="0.5"/>
- <!--Iteracting with Action Center-->
- <Button Name="BtnSendToast" HorizontalAlignment="Stretch" Content="Send notification with toast popup" Background="#FF3FD483" Click="BtnSendToast_Click"/>
- <Button Name="BtnSendToastNoPopup" HorizontalAlignment="Stretch" Content="Send notification without toast popup" Background="#FF3FD483" Click="BtnSendToastNoPopup_Click"/>
- <Button Name="BtnRemoveToast" HorizontalAlignment="Stretch" Content="Remove notifications from app" Background="#FF3FD483"/>
- <Button Name="BtnUpdateToast" HorizontalAlignment="Stretch" Content="Update notifications from app" Background="#FF3FD483" Click="BtnUpdateToast_Click"/>
- <!--About ActionCenter-->
- <TextBox VerticalAlignment="Bottom" Header="About ActionCenter:" TextWrapping="Wrap" BorderBrush="#FF23E0D8" Foreground="#FF736B74" IsHitTestVisible="False" IsColorFontEnabled="true">
- <TextBox.HeaderTemplate>
- <DataTemplate>
- <TextBlock Text="{Binding}" FontSize="23" Foreground="#FFD318E6"/>
- </DataTemplate>
- </TextBox.HeaderTemplate>
- <TextBox.Text >
- A highly requested Notification Center feature included in Windows Phone 8.1,Action Center will allow you to quickly access the settings pages like Airplane mode, Wi-Fi Settings, Bluetooth settings etc.
- from a native app. This will also report you about alerts, non-toast notifications, update histories etc.Action Center is available via a swipe down from the top of your device, even from the lock screen.
- </TextBox.Text>
- </TextBox>
- </StackPanel>
- </ScrollViewer>
- </Grid>
When run the above xaml code,your screen will be appeared as
Note: To interact with the Windows Phone Notification Center, developer must be set Toast Capable option to 'Yes' from 'Application Tab' of Package.appxmanifest file.
Otherwise when you click on send notification button from MainPage, you will be warning like this
Step 3:
Before start to send notifications from app.we need to check following phone NotificationSettings with help of 'ToastNotificationManager' class.
- public bool CanSendToasts()
- {
- bool canSend = true;
- var NotifierStatus = ToastNotificationManager.CreateToastNotifier();
- //Check Notification settings status
- if (NotifierStatus.Setting != NotificationSetting.Enabled)
- {
- string ReasonMessage = "unknown error";
- switch (NotifierStatus.Setting)
- {
- case NotificationSetting.DisabledByGroupPolicy:
- ReasonMessage = "An administrator has disabled all notifications on this computer through group policy. The group policy setting overrides the user's setting.";
- break;
- case NotificationSetting.DisabledByManifest:
- ReasonMessage = "To send a toast from app,developer must be set Toast Capable option to 'Yes' from 'Application Tab' of Package.appxmanifest file.";
- break;
- case NotificationSetting.DisabledForApplication:
- ReasonMessage = "The user has disabled notifications for this app.";
- break;
- case NotificationSetting.DisabledForUser:
- ReasonMessage = "The user or administrator has disabled all notifications for this user on this computer.";
- break;
- }
- string errroMessage = String.Format("Can't send a toast.\n{0}", ReasonMessage);
- DisplayMessage(errroMessage);
- canSend = false;
- }
- return canSend;
- }
I hope you already know ,from 8.1 OS version in phone settings there is an option for turn off /on notifications from a specific app /all apps.So in this case we can't send toast and need to check the notification settings to displaying related error message.See in above code i wrote a method called CanSendToasts(),if it is return 'true' then it means you can able to send toast,otherwise you can't send toast from app.
Step 4:
Lets assume we pass all 'NotificationSettings'.And now we are ready to send local toast notifications to action center.For simplicity i made a common method to send local toast notifications .So helper method name is MakingToastNotification() which is having parameters (ToastTitle ,ToastBody ,Tag,Group,IsToastPopUpRequired). You’ll need to manipulate the XML document to set the different tile properties, like in the following example.
- public void MakingToastNotification(string ToastTitle, string ToastBody, string strTag, string strGroup, bool IsToastPopUpRequired)
- {
- // Using the ToastText02 toast template.This template contains a maximum of two text elements. The first text element is treated as a header text and is always bold.
- ToastTemplateType toastTemplate = ToastTemplateType.ToastText02;
- // Retrieve the content part of the toast so we can change the text.
- XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);
- //Find the text component of the content
- XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");
- // Set the text on the toast.
- // The first line of text in the ToastText02 template is treated as header text, and will be bold.
- toastTextElements[0].AppendChild(toastXml.CreateTextNode(ToastTitle));//Toast notification title
- toastTextElements[1].AppendChild(toastXml.CreateTextNode(ToastBody + " (Tag:" + strTag + ", Group:" + strGroup + ")"));//Toast notification body
- // Set the duration on the toast
- IXmlNode toastNode = toastXml.SelectSingleNode("/toast");
- ((XmlElement)toastNode).SetAttribute("duration", "long");
- ToastNotification toast = new ToastNotification(toastXml);
- toast.ExpirationTime = DateTimeOffset.UtcNow.AddSeconds(10);
- //Check Toast popup required to display
- if(!IsToastPopUpRequired)
- {
- toast.SuppressPopup = true;//to send notification directly to action center without displaying a popup on phone.
- }
- //Note: Tag & Group properties are optional,but these are userful for delete/update particular notification from app
- toast.Tag = strTag;
- toast.Group = strGroup;
- ToastNotificationManager.CreateToastNotifier().Show(toast);
- }
Note:See the highlighted color in above code,When we want to send a local toast notification directly to action center without showing toast popup, we need to set "toast.SuppressPopup = true;"
Step 5:
Now we made all neccessary methods for interacting with notification center. OK let's play with following action center interactions from app.
1)Sending a local notification to action center
Initially action center will shows you with 'no notifications' message.
In below code i am trying send local toast notification to action center with toast popup,so i set fifth parameter of helper method (i.e IsToastPopUpRequired) is 'true'
- //Sending local toast notifications to action scenter with popup .
- int ToastCount = 0;
- private void BtnSendToast_Click(object sender, RoutedEventArgs e)
- {
- //Check notificationsettings status from user device
- if (CanSendToasts())
- {
- MakingToastNotification("Toast_with_Popup: ", "Notitification " + ToastCount++, "T" + ToastCount.ToString(), "G1", true);
- }
- }
After sending some local notifications , our app notifications will be shown in action center like this
2)Sending a local notification to action center without toast popup:
In below code i am trying send local toast notification to action center without showing toast popup,so i set fifth parameter of helper method (i.e IsToastPopUpRequired) is 'false'
- //Sending local toast notifications to action scenter without popup .
- private void BtnSendToastNoPopup_Click(object sender, RoutedEventArgs e)
- {
- //Check notification settings status from user device
- if (CanSendToasts())
- {
- MakingToastNotification("Toast_without_Popup: ", "Notitification " + ToastCount++, "T"+ToastCount.ToString(), "G2", false);
- }
- }
3)Remove specific notification from the Action Center:
We can remove a specific notification based on its Tag & Group property values . Here i am trying to remove notification which is having Tag property value is 'T1' ,However i am also explained different ways for removing notification in comment lines.
- //Remove notifications from code .
- private void BtnRemoveToast_Click(object sender, RoutedEventArgs e)
- {
- //To Remove all notifications with the given Tag id
- ToastNotificationManager.History.Remove("T1");
- //To Remove all notifications with the given Tag id and Group id
- //ToastNotificationManager.History.RemoveGroup("T1","G1");
- //To Remove all notifications with the given Group id
- // ToastNotificationManager.History.RemoveGroup("G1");
- //To Remove all notifications
- //ToastNotificationManager.History.Clear();
- DisplayMessage("Notifications with Tag 'T1' have been removed.\n Open action center to verify.");
- }
4)Update specific notification from the Action Center:
We can update a specific notification based on its Tag & Group property values . Here i am trying to update notification which is having Tag property value is 'T1' and Group property value is 'G1'.
- //Update notifications from code .
- private void BtnUpdateToast_Click(object sender, RoutedEventArgs e)
- {
- MakingToastNotification("Toast_without_Popup: ", "Notitification 1 is updated ","T1", "G1", false);//Update the notifcation which is having Tag T1 ,Group G1
- DisplayMessage("Notifications has been updated with new content.");
- }
From this article we have learned "Integrate Notification Center in windowsphone 8.1 application".Hope i wrote this post with my best level,When writing this article, i had taken lot of hard works and making nice presentation to understand the article at beginners level.
FeedBack Note:
Please share your thoughts,what you think about this post,Is this post really helpful for you?I always welcome if you drop comments on this post and it would be impressive.
FeedBack Note:
Follow me always at @Subramanyam_B
Have a nice day by Subramanyam Raju :)
Follow me always at @Subramanyam_B
Have a nice day by Subramanyam Raju :)
No comments:
Post a Comment