Azure IoT Hubs: offline storage of messages - c#

Is there any functionality within the Azure IoT device SDKs to ensure that a message is sent to the hub from a device in the event of a loss of connectivity and subsequent termination and restart (after connectivity is restored) of the application sending the message?
I am struggling to find anything about this in the documentation; I have found this question, however it has not been updated in nearly 2 years and I suspect (hope) things have moved on since then.
The application will be WPF, running on Windows 10 Professional if that makes any difference.

This blog has a section on the reliability features in the SDKs. We will have more documentation on this soon. You may also want to implement some heat beat mechanism so devices can auto-reboot.

One way to implement that functionality is check the callback function result and the parent (SendConfirmationCallback - IoTHubClient_LL_SendEventAsync in SDK-C). With this You can check the quantity of messages without confirmation OK and restart your process of azure or Internet connection.

Related

Push Notification for web site

I have been tasked to add push notification for a web site being developed. Never done one before. I spent a long time googling this and I am not much closer than when I started searching, mostly because most articles describe specific details.
The web application is an intranet app and they are asking for things like when a new report is made available, let the users know about it; or basically any kind of organization/department announcements.
Can someone tell me what are the components involved in developing/deploying this type push notifications? If I know what the pieces are that make this process possible, I can take it from there. I must mention that this web application is being designed to be available on devices (desktop, tablets and phones; if this makes any difference in design/development).
You can try OneSignal Push Notification
in this you can send notifications to websites,android app and ios app
You can also check the documentation for sending notifications to website here
Hope this helps:)

How to setup Azure Notification Hub in C# backend

I'm trying to implement my C# backend's communication with Notification Hub. I've read almost every tutorial about Azure Notification Hub, and I can't find a decent tutorial that explains what actually corresponds to what. I think there are two ways of representing a device (from what I've understood): an Installation and Registration, and Installation seems to be newer and more preferred.
However, when I get into Installation, I get more questions in my mind:
What is InstallationId? Is it something that I create, or something that I get from somewhere, either device or PNS.
Do I need to set up ExpirationTime, or does it default to longest (it says 90 days is the longest)? What if I want it to be longer than 90 days?
What is PushChannel? Is it, just like InstallationId, something that I create, or get from somewhere?
How are templates exactly used?
Out of all these variables, which one is the actual device push token that I get at the client?
I usually don't ask those kind of questions that seek for a tutorial-ish answers and have multiple questions, but I've tried to search everywhere, but the more I search, the more it gets complicated and I don't even know where to start. Any answer would be a good starting point to anyone trying to learn Notification Hubs, just like me.
InstallationId is an abstraction on top of device token/key/channel etc. It's a unique id that you create when you 'install' a device (or user if your OS/platform allows separate tokens for multiple users). This is an id you can use to associate tags, templates etc with the device. You create an installation once and then keep the id somewhere in the local storage and then make updates to the installation associated with the id if you need to.
You don't need to explicitly set ExpirationDate. I think in the past tokens on most platforms used to expire, so this was a way to let Notification Hubs know when not to try to push to the token. I believe now it's not the case. So what happens is if Notification Hubs cannot deliver a notification to a device for a number of times, it just deletes the registration by itself.
Has this post on templates been useful?
PushChannel stands for 'The channel URI if registering the installation for WNS; Device Token if registering for APNS.'
Let me know in the comments if I can clarify any of these and I'll update the answer.
There is actually a 3rd option besides Installation and Notification: DirectSend.
The Direct-Send technique is less of an abstraction than Installation and Notification. With Direct-Send you can send notifications directly to one or more specific devices. Basically you use the actual deviceToken- as in what #NikitaG notes for PushChannel.
If you need to be able to decide specifically when your back-end application pushes to a specific-device-A-but-not-B vs. pushing to a specific-device-B-but-not-A vs. specific-device-A-and-specific-device-B according to some schedule Direct-Send can be enabling.
I'd listed some of the resources discussing Direct-Send at Azure Notification Hubs Send Notification to Specific Device.

Send and Receive Messages between Android App and Winform application

Looked for a long time and didn't find anything that showed this, so I apologize in advance I missed something.
I have an android app running on KitKat (Android 4.4.2) and a Winforms application running Windows 7. I need to send messages between the two of them.
Clearly GCM works to get the message to the Android device. I have code that sends from Winforms to the Android Device using GCM. I cannot find a way to have the Android App send anything BACK though. Is it possible to have a Winforms app RECEIVE a GCM Message? Do I have to use Azure? (All examples there seem to focus on the Windows App store and Windows 8.1 neither one of which can be a solution in this case due to client restraints.)
I'm putting this here in case anyone stumbles across it and has a similar question. This is not really an answer. Still hoping someone comes on here and shows that I am wrong. But after 26 days, no one has even offered a suggestion, so I'm guessing not.
There is no way that I have seen that allows for this the way I had hoped. Windows 7 doesn't have this built in to it (AFAIK). Windows 10 should have it built in, but Windows 7 pre-dates the huge shift to the cloud, and didn't have it in the design. If I were working with something like Xamarin -- which is a cross-platform tool, this might be possible. But there is no concept in Windows 7 for receiving messages from the Cloud.
In order to accomplish this, there would have to be some sort of server added to the mix that could take messages and pass them along via a REST API. This is beyond the scope of what I wanted to code.
The solution I found, and that works for me, is to use Microsofts API that wraps a REST service. This allows for communication to OneDrive, for example. That is what I am using as my intermediary REST server.
Microsoft LIVE SDK
This has a pretty good sample list of Android examples, and can be used for what I need. The good thing is the Upstream is just a simple call, and I don't need to have the Android device poll anything (which kills the battery). The laptop will need to poll OneDrive, but its plugged in so there is no battery life concern.
One thing to be aware of, though, is that Microsoft sort of hints that they don't want a bunch of traffic headed to OneDrive. This is from the overview doc:
Throttling
OneDrive has limits in place to make sure that individuals and apps do
not adversely affect the experience of other users. When an activity
exceeds OneDrive's limits, API requests will be rejected for a period
of time. OneDrive may also return a Retry-After header with the number
of seconds your app should wait before sending more requests.
Although, I have never seen what those limits actually are, so YMMV.

Options to communicate to a C# desktop app from a linux server?

The desktop application is being developed with a demo version that is supposed to run for a few minutes and after that would request the user to restart the application to run again (the user has to input their login and password to access it), since I dont know much in this field the way I can think of doing this without the user bypassing it would be having a realtime communication between both or something of the sorts.
After X minutes the server sends a
message to the client to close/disable
the client requiring the user to
restart it, it also limits the daily
usage on the demo for the same user.
As I am very inexperienced in this type or communication I would like to consult you guys with what options I have here ?
The desktop application is developed in c# to run mainly on windows OS as for the server we only have linux available and as to what sort of service, if it is possible to make a webapi or session in php or perl to work with it that would be reliable enough would be nice but if that is not possible we are open to hear other options.
PS: If I have'nt given enough information or am missing anything important here please drop me a comment i will update as soon as possible.
I'd give a go to HTTPS with mutual certificate-based authentication as the safest option. The desktop app can poll the server (=ask periodically) and quit in case of no response / no connection / negative reponse.
However, based on the type of app you are developing and the target audience, you can expect an important amount of users to have connectivity problems or have no connectivity at all.
Because of this, at the end of the day, you can come up with a lot simpler solution, like measuring run-time locally without any server involved, and gain pretty much the same effect.
I don't think you need to involve a server to do this.
Just have the desktop application save the date and time when it started. Periodically on a timer you can check the current time, and see if too much time has elapsed for the demo version, and tell the user they have to quit.
It is very unlikely many users will try to get around this. They are more likely to dump your trial software in favour of something that treats them decently!
Checking DateTime.Now could work as Ben stated, but you'd be better off with System.Timers.Timer. Set interval to be your desired interval between auth calls in milliseconds. Attach a handler to the elapsed event that asks for auth info. System.Timers.Timer works in a separate thread so you can take advantage of some parallelism here. Changing the system time shouldn't have any effect on System.Timers.Timer but I am not positive on that point.

How can I know when Windows is going into/out of sleep or Hibernate mode?

Is it possible to subscribe to a Windows event that fires when Windows is going into or coming out of Sleep or Hibernate state?
I need my application to be made aware when the computer is going to sleep to do some cleanup and avoid timing issues when it comes out of sleep.
Microsoft.Win32.SystemEvents.PowerModeChanged event will give you this information. This event is available in all variants of the .NET framework released by Microsoft so far.
In .NET, use the PowerModeChanged event.
In Win32, use the WM_POWERBROADCAST message.
You can monitor Win32_PowerManagementEvent WMI event
In a Visual Studio 2005 C++ MFC application you will need to add an ON_MESSAGE() to your message map looking for the WM_POWERBROADCAST message as in this example:
BEGIN_MESSAGE_MAP(CFrameworkWndDoc, CWindowDocument)
//{{AFX_MSG_MAP(CFrameworkWndDoc)
ON_WM_CHAR()
ON_WM_TIMER()
//}}AFX_MSG_MAP
ON_MESSAGE(WM_POWERBROADCAST, OnPowerMsgRcvd)
END_MESSAGE_MAP()
Then you will need to add the message handler function along with the class definition change to declare the member function for the message handler so that you can check the wParam variable for the message type as in this skeleton:
// Handle the WM_POWERBROADCAST message to process a message concerning power management
// such as going to Sleep or Waking Up.
LRESULT CFrameworkWndDoc::OnPowerMsgRcvd(WPARAM wParam, LPARAM lParam)
{
switch (wParam) {
case PBT_APMPOWERSTATUSCHANGE:
TRACE0("PBT_APMPOWERSTATUSCHANGE received\n");
break;
case PBT_APMRESUMEAUTOMATIC:
TRACE0("PBT_APMRESUMEAUTOMATIC received\n");
break;
case PBT_APMRESUMESUSPEND:
TRACE0("PBT_APMRESUMESUSPEND received\n");
break;
case PBT_APMSUSPEND:
TRACE0("PBT_APMSUSPEND received\n");
break;
}
return 0;
}
What I have seen is that a test using the above in an application running on Windows 7 that is started in the debugger and then I manually make my PC running the application to Sleep I will see the following message:
PBT_APMSUSPEND received
Then when the PC is restarted and I sign-in what I will see in the debugger output window are two messages one after the other:
PBT_APMRESUMESUSPEND received
PBT_APMRESUMEAUTOMATIC received
Everything that I have found thus far indicates that you have no indication whether you are coming out of a Sleep state or a Hibernate state. I am still doing further research on what needs to be done when suspending or when resuming so far as file and device handles. I have seen indications that file handles to COM ports are no longer valid after resuming. I also am unsure about interfaces to other processes for instance database connections.
In addition to the standard Sleep and Hibernate power management states Microsoft has introduced the Connected Standby power state with Windows 8 and 8.1 which has some application design ramifications depending on the type of application.
Desktop applications typically require no extra work to integrate with
connected standby.
The Desktop Activity Moderator (DAM) is the Windows component that
pauses all desktop applications and throttles the runtime of
third-party system services during connected standby. The purpose of
the DAM is to maintain basic software compatibility with existing
applications and services, but mitigate their impact on battery life
during sleep.
Windows prevents desktop applications from running during any part of
connected standby after the DAM phase completes. Windows allows
third-party system services to execute in a throttled mode after
completing the DAM phase. In this mode, a third-party service can run
for no more than one second of wall-clock time every 30 seconds.
The Art of Graceful Application Suspension by Lynn Merrill from Intel has some information about handling the various Windows message types associated with Power Management under Windows however it is date 2005 so not all material may pertain to Windows after Windows XP. There is at least one no longer used message in the message sequence described in this document as beginning with Windows Vista the PBT_APMQUERYSUSPEND message which was used to request whether an application was able to suspend is no longer used by Windows. The SetThreadExecutionState() function is now used to indicate that a thread can not be interrupted with a change to Sleep or Hibernate state. See the answers in stackoverflow Can't catch sleep suspend messages (winxp) for details on Power Management state message changes.
System power state events since Windows XP
Microsoft has improved power management since Windows XP as the various versions of the Windows OS share more components and versions of Windows are now being deployed on smaller devices with battery requiring more careful power management. See Registering for Power Events. There is both a RegisterPowerSettingNotification() function and an UnregisterPowerSettingNotification() function.
An application or service uses the RegisterPowerSettingNotification
function to register for notifications. When the corresponding power
setting changes, the system sends notifications as follows:
An application receives a WM_POWERBROADCAST message with a wParam of PBT_POWERSETTINGCHANGE and an lParam that points to a
POWERBROADCAST_SETTING structure.
A service receives a call to the HandlerEx callback function it registered by calling the RegisterServiceCtrlHandlerEx function. The
lpEventData parameter sent to the HandlerEx callback function
points to a POWERBROADCAST_SETTING structure.
In the POWERBROADCAST_SETTING structure, the PowerSetting member
contains the GUID that identifies the notification and the Data member
contains the new value of the power setting.
See also Power Management Functions for the list of the power management functions in the Windows API since Windows Vista.
System power status and battery condition
You can use the Windows System Services API function GetSystemPowerStatus() to retrieve the current power status.
Retrieves the power status of the system. The status indicates whether
the system is running on AC or DC power, whether the battery is
currently charging, how much battery life remains, and if battery
saver is on or off.
However note that the information returned in the SYSTEM_POWER_STATUS struct is about battery status and AC/DC power source and not the actual device power state such as Sleep or Hibernate.
And if the Windows device is in a Sleep or Hibernate state, your application will not be running so this function can not be used to determine the current Windows power state. I include this note as it may be useful for someone who arrives at this post while researching this topic.
GetSystemPowerStatus function (winbase.h)
SYSTEM_POWER_STATUS structure (winbase.h)
Not sure how often you want to monitor this, but if you write a service in .NET you can override ServiceBase, set CanHandlePowerEvent to true, and then you'll be notified of power changes via the PowerBroadcastStatus enumeration.
You can subscribe to NetworkChange.NetworkAvailabilityChanged and NetworkChange.NetworkAddressChanged.
I generally start a two second timer so that I can resume network communications after being in sleep mode when it times out.

Categories

Resources