HTTP upload using WinInet - http

I'm doing http(PUT) upload using WinInet library, on WinXP its working fine but on Win7 its not working and the strange thing is 'InterWriteFile' function of WinInet files is returning true. Any help will be appreciated.
Thanks in Advance.
//this is the first function that is called when upload is requested.
void Upload()
{
//check if handle is already open or not
if(!m_hNetOpen)
{
//open interconnect: Initializes an application's use of the WinINet functions.
m_hNetOpen = InternetOpen("XYZ",
INTERNET_OPEN_TYPE_DIRECT,
NULL,
NULL,
INTERNET_INVALID_PORT_NUMBER);
//return if handle is NULL
if(m_hNetOpen == NULL)
{
return FALSE;
}
}
//we'll get user specified URL, could be some xyz.com too.
if(!m_hNetConnect)
{
//opens HTTP or FTP connection for a site requested...pchServerName requested
m_hNetConnect = InternetConnect(m_hNetOpen,
strServerName,
INTERNET_DEFAULT_HTTP_PORT,
NULL,
NULL,
INTERNET_SERVICE_HTTP,
INTERNET_FLAG_PASSIVE,
dwContext);
//return if handle is NULL
if(m_hNetConnect == NULL)
{
return FALSE;
}
}
dwContext = 0;
//now, create HTTP request
m_hHttpConnect = HttpOpenRequest(m_hNetConnect,
"PUT",
strObjectName,
NULL,
NULL,
NULL,
INTERNET_FLAG_CACHE_IF_NET_FAIL,
dwContext);
//return if handle is NULL
if(m_hHttpConnect == NULL)
{
return FALSE;
}
//call UseHttpSendReqEx
BOOL bRet = UseHttpSendReqEx(m_hHttpConnect, strUploadFileName);
//if not suuceeded log the message
if(!bRet)
{
//Logmessage
}
if(m_hHttpConnect)
{
//close the handle
InternetCloseHandle(m_hHttpConnect);
}
return bRet;
}
//this function is will send HTTP request and writes data to internetfile
BOOL UseHttpSendReqEx(HINTERNET hConnect, char *upFile)
{
INTERNET_BUFFERS BufferIn = {0};
DWORD dwBytesRead = 0;
DWORD dwBytesWritten = 0;
//buffer
BYTE strBuffer[20480] = {0};
BOOL bRead;
BOOL bRet = TRUE;
//get the size of structure
BufferIn.dwStructSize = sizeof(INTERNET_BUFFERS);
//create file
m_hFile = CreateFile(upFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_DELETE,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
//check for invalid handle
if (m_hFile == INVALID_HANDLE_VALUE)
{
return FALSE;
}
//get the file size
BufferIn.dwBufferTotal = GetFileSize(m_hFile, NULL);
//send request to HTTP server
if(!HttpSendRequestEx(hConnect, &BufferIn, NULL, HSR_INITIATE, 0))
{
//close the file handle
CloseHandle(m_hFile);
m_hFile = NULL;
return FALSE;
}
DWORD sum = 0;
do
{
//read the file that is to be uploaded
if (!(bRead = ReadFile(m_hFile, strBuffer, sizeof(strBuffer),
&dwBytesRead, NULL)))
{
bRet = FALSE;
break;
}
//write the internet file
bRet=InternetWriteFile(hConnect, strBuffer, dwBytesRead,
&dwBytesWritten);
if(!bRet)
{
DWORD d = GetLastError();
bRet = FALSE;
break;
}
memset(strBuffer, '\0', sizeof(strBuffer));
sum += dwBytesWritten;
}
while(dwBytesRead == sizeof(strBuffer)) ;
//This sleep was introduced as if we immediately after the above loop
//if we try to close the handle then file does not get created on the server
Sleep(100);
BOOL b = CloseHandle(m_hFile);
m_hFile = NULL;
return bRet;
}

Related

Bluetooth Disconnection Takes Too Long

I can connect to my bluetooth device and when I want to disconnect it takes about 30 seconds to disconnect and for a few seconds it disconnects and automatically reconnects to the device in the background I don't want this to happen, how can I disconnect it literally?
I dispose service and device and pull to null but that doesn't work
This is Disconnect method :
public bool DisConnect()
{
try
{
IsScannerActiwe = false;
ButtonPressed = false;
IsConnected = false;
if (currentSelectedGattCharacteristic != null && currentSelectedService != null)
{
currentSelectedGattCharacteristic.Service?.Dispose();
currentSelectedService?.Dispose();
currentSelectedGattCharacteristic = null;
currentSelectedService = null;
if (bluetoothLeDevice != null)
{
bluetoothLeDevice.Dispose();
bluetoothLeDevice = null;
return true;
}
}
}
catch (System.Exception ex)
{
Trace.WriteLine("Exception Handled -> DisConnect: " + ex);
}
return false;
}
I dispose the device and the service and pull it to null and the function returns true but it does not break the connection and it is null for a few seconds and the service continues to run in the background automatically
I also tried another function for disconnection
public async Task<bool> ClearBluetoothLEDevice()
{
if (IsConnected)
{
IsScannerActiwe = false;
ButtonPressed = false;
IsConnected = false;
// Need to clear the CCCD from the remote device so we stop receiving notifications
var result = await currentSelectedGattCharacteristic.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.None);
if (result != GattCommunicationStatus.Success)
{
return false;
}
else
{
if (currentSelectedGattCharacteristic != null)
{
currentSelectedGattCharacteristic.ValueChanged -= CurrentSelectedGattCharacteristic_ValueChanged;
IsConnected = false;
currentSelectedService.Dispose();
currentSelectedGattCharacteristic.Service.Dispose();
currentSelectedService?.Dispose(); //added code
currentSelectedService = null; //added code
bluetoothLeDevice?.Dispose();
bluetoothLeDevice = null;
currentSelectedGattCharacteristic = null;
return true;
}
}
}
return false;
}
I literally cannot disconnect from the device

Any solution for file upload problem on Arduino ESP32 via http

First the problem.
The user can upload file from the web with ajax. If the file is relatively big, the uploading takes a while. If the user's connection is lost or something happens during the uploading process, the file is going to be damaged or empty.
How should I secure the upload process so the file remains the same if it fails for some reason?
I'm using the following libraries on the Arduino ESP32:
ESPAsyncWebServer
LITTLEFS
I have a basic file upload handler on my esp32 which looks like this:
server.on("/uploading", HTTP_POST, [](AsyncWebServerRequest * request) {
}, handleFileUpload);
void handleFileUpload(AsyncWebServerRequest * request, String filename,size_t index, uint8_t *data, size_t len, bool final) {
if (!index) {
if (!filename.startsWith("/"))
filename = "/" + filename;
if (LITTLEFS.exists(filename)) {
LITTLEFS.remove(filename);
}
uploadFile = LITTLEFS.open(filename, "w");
}
for (size_t i = 0; i < len; i++) {
uploadFile.write(data[i]);
}
if (final) {
uploadFile.close();
if(filename == "/myHomeProgram.json"){initProgram = true;}
AsyncWebServerResponse *response = request->beginResponse(200, "text/plain", "File Uploaded;"+filename);
response->addHeader("Access-Control-Allow-Origin","*");
request->send(response);
}
}
This is working pretty well, the files are uploaded correctly 99% of the cases, but if it fails I lost the file data, or if some other part of the program wants to open the same file it fails too.
Should I write to a temporary file and after if it succeeded write the content to the intended file somehow?
Here is an example from client ( JS ) side:
// Example call:
saveFile(JSON.stringify(places),"/myHomeProgram.json","application/json");
function saveFile(data, filename, type) {
var file = new Blob([data], {type: type});
form = new FormData();
form.append("blob", file, filename);
$.ajax({
url: '/uploading',
type: 'POST',
data: form,
processData: false,
contentType: false
}).done(function(resp){
var response = resp.split(";");
$(".saveIconGraph").removeClass("fas fa-spinner fa-spin");
$(".saveIconGraph").addClass("far fa-save");
if(response[1] == "/myHomeProgram.json"){
toast("success","saveOk","progInfo",3500);
showSaved();
setTimeout(() => {
$("#saveMe").fadeOut( "slow", function() {
showSave();
});
}, 1000);
initPlaces();
}
}).fail(function(resp){
var response = resp.split(";");
$(".saveIconGraph").removeClass("fas fa-spinner fa-spin");
$(".saveIconGraph").addClass("far fa-save");
if(response[1] == "/myHomeProgram.json"){
toast("error","saveNotOk","progInfo",3500);
showSaveError();
$("#saveMeBtn").addClass("shakeEffect");
setTimeout(() => {
$("#saveMeBtn").removeClass("shakeEffect");
showSave();
}, 4500);
}
});
}
I could save the file in a temporary char variable before write, and on the final I could match the size of the file and the temporary variable size and if it is not the same, roll back to the previous. Is this manageable?
Something like this:
String uploadTemp = "";
inline boolean saveFileToTemp(String fileName){
uploadTemp = "";
File f = LITTLEFS.open(fileName, "r");
if (!f) {
f.close();
return false;
}else{
for (int i = 0; i < f.size(); i++){
uploadTemp += (char)f.read();
}
}
f.close();
return true;
}
inline boolean revertBackFile(String fileName){
File g = LITTLEFS.open(fileName, "w");
if (!g) {
g.close();
return false;
}else{
g.print(uploadTemp);
}
g.close();
return true;
}
inline boolean matchFileSizes(String fileName,boolean isFileExists){
boolean isCorrect = false;
if(isFileExists){
File writedFile = LITTLEFS.open(fileName, "w");
if( writedFile.size() == uploadTemp.length()){
isCorrect = true;
}else{
isCorrect = false;
}
writedFile.close();
return isCorrect;
}else{
return true;
}
}
void handleFileUpload(AsyncWebServerRequest * request, String filename,size_t index, uint8_t *data, size_t len, bool final) {
String webResponse;
boolean error = false,isFileExists = false;
if (!index) {
if (!filename.startsWith("/"))
filename = "/" + filename;
if (LITTLEFS.exists(filename)) {
isFileExists = true;
// Save the file to a temporary String if it success we continue.
if( saveFileToTemp(filename) ){
LITTLEFS.remove(filename);
}else{
// If the file save was fail we abort everything.
webResponse = "File NOT Uploaded " + filename;
final = true;
error = true;
}
}
if( !error ){
uploadFile = LITTLEFS.open(filename, "w");
}
}
if( !error ){
// Copy content to the actual file
for (size_t i = 0; i < len; i++) {
uploadFile.write(data[i]);
}
}
if (final) {
uploadFile.close();
if( !error ){
if( matchFileSizes(filename,isFileExists) ){
if(filename == "/myHomeProgram.json"){initProgram = true;}
webResponse = "File Uploaded " + filename;
}else{
error = true;
webResponse = "File length mismatch";
}
}
if( error ){
revertBackFile(filename);
}
Serial.println(webResponse);
AsyncWebServerResponse *response = request->beginResponse(200, "text/plain", webResponse);
response->addHeader("Access-Control-Allow-Origin","*");
request->send(response);
}
}
It seems to me that the problem solved.
I have managed to replace the String buffer with a char one in external memory. It seems stable but requires more testing. I'll post the solution but if anyone has a better approach feel free to comment here.
Thanks.
char * uploadTemp;
inline boolean saveFileToTemp(String fileName){
File f = LITTLEFS.open(fileName, "r");
if (!f) {
f.close();
return false;
}else{
size_t fileSize = f.size();
uploadTemp = (char*)ps_malloc(fileSize + 1);
for (int i = 0; i < fileSize; i++){
uploadTemp[i] = (char)f.read();
}
uploadTemp[fileSize] = '\0';
}
f.close();
return true;
}
inline boolean revertBackFile(String fileName){
File g = LITTLEFS.open(fileName, "w");
if (!g) {
g.close();
return false;
}else{
g.print(uploadTemp);
}
g.close();
return true;
}
inline boolean matchFileSizes(String fileName,boolean isFileExists){
boolean isCorrect = false;
if(isFileExists){
File writedFile = LITTLEFS.open(fileName, "w");
if( writedFile.size() == sizeof(uploadTemp)){
isCorrect = true;
}else{
isCorrect = false;
}
writedFile.close();
return isCorrect;
}else{
return true;
}
}
void handleFileUpload(AsyncWebServerRequest * request, String filename,size_t index, uint8_t *data, size_t len, bool final) {
boolean isFileExists = false,error = false;
String webResponse = "";
int httpStatus = 200;
// Start of the file upload
if (!index) {
// Make sure that there is a / char at the start of the string
if (!filename.startsWith("/")){ filename = "/" + filename; }
// Check if the file exists
if (LITTLEFS.exists(filename)) {
isFileExists = true;
// Get the file contents for safety reasons
// If it succeded we can create a new file in the palce
if( saveFileToTemp(filename) ){
uploadFile = LITTLEFS.open(filename, "w");
}else{
// If we can not save it abort the upload process.
webResponse = "File NOT Uploaded " + filename;
final = true;error = true;
}
}
}
// If we have no error at this point, we can start to copy the content to the file.
if( !error ){
for (size_t i = 0; i < len; i++) {
uploadFile.write(data[i]);
}
}
// If no more data we can start responding back to the client
if (final) {
uploadFile.close();
// Check if we got any error before.
if( !error && matchFileSizes(filename,isFileExists) ){
// Copyed file is the same, upload success.
if(filename == "/myHomeProgram.json"){initProgram = true;}
webResponse = "File Uploaded " + filename;
}else{
webResponse = "File length mismatch";
revertBackFile(filename);
httpStatus = 500;
}
free(uploadTemp);
AsyncWebServerResponse *response = request->beginResponse(httpStatus, "text/plain", webResponse);
response->addHeader("Access-Control-Allow-Origin","*");
request->send(response);
}
}
EDIT:
Yeah, so it was completely wrong.
I have to do the following things:
Save the file we want to upload if it exist into a temporary char array.
Get the uploaded file into a temporary file on upload.
If everything was a success, copy the contents of the temporary file to the intended file.
If something fails, revert back the saved file to the original and report an error.
Something like this ( still in test ):
char * prevFileTemp;
inline boolean saveFileToTemp(String fileName){
File f = LITTLEFS.open(fileName, "r");
if (!f) {
f.close();
return false;
}else{
size_t fileSize = f.size();
prevFileTemp = (char*)ps_malloc(fileSize + 1);
for (int i = 0; i < fileSize; i++){
prevFileTemp[i] = (char)f.read();
}
}
f.close();
return true;
}
inline boolean revertBackFile(String fileName){
if (LITTLEFS.exists(fileName)) {
Serial.println("Reverting back the file");
File g = LITTLEFS.open(fileName, "w");
if (!g) {
g.close();
return false;
}else{
g.print(prevFileTemp);
}
g.close();
}
return true;
}
static const inline boolean copyContent(String fileName){
File arrivedFile = LITTLEFS.open(uploadTemp, "r");
File newFile = LITTLEFS.open(fileName, "w");
// Check if we can open the files as intended.
if( !arrivedFile || !newFile){
revertBackFile(fileName);
return false;
}
// Copy one file content to another.
for (size_t i = 0; i < arrivedFile.size(); i++) { newFile.write( (char)arrivedFile.read() ); }
// Check the sizes, if no match, abort mission.
if( newFile.size() != arrivedFile.size()){ return false; }
arrivedFile.close();newFile.close();
return true;
}
boolean isFileExists = false,uploadError = false,newFileArrived = false;
String webResponse = "",newArrivalFileName = "";
int httpStatus = 200;
inline void resetVariables(){
isFileExists = false;
uploadError = false;
webResponse = "";
httpStatus = 200;
}
void handleFileUpload(AsyncWebServerRequest * request, String filename,size_t index, uint8_t *data, size_t len, bool final) {
// Start file upload process
if (!index) {
// Reset all the variables
resetVariables();
// Make sure that there is a '/' char at the start of the string
if (!filename.startsWith("/")){ filename = "/" + filename; }
// Open the temporary file for content copy if it is exist
if (LITTLEFS.exists(filename)) {
if( saveFileToTemp(filename) ){
uploadFile = LITTLEFS.open(uploadTemp, "w");
}else{
// If we can not save it abort the upload process.
webResponse = "File NOT Uploaded " + filename;
final = true;uploadError = true;
}
}
}
// If we have no error at this point, we can start to copy the content to the temporary file.
if( !uploadError ){
for (size_t i = 0; i < len; i++) {
uploadFile.write(data[i]);
}
}
// If no more data we can start responding back to the client
if (final) {
if (!filename.startsWith("/")){ filename = "/" + filename; }
uploadFile.close();
if( !uploadError && copyContent(filename) ){
webResponse = "File Uploaded " + filename;
}else{
webResponse = "File length mismatch";
revertBackFile(filename);
httpStatus = 500;
}
free(prevFileTemp);
AsyncWebServerResponse *response = request->beginResponse(httpStatus, "text/plain", webResponse);
response->addHeader("Access-Control-Allow-Origin","*");
request->send(response);
}
}

GWT read mime type client side

I'm trying to read the mime type in GWT client side in order to validate a file before upload it. To do this I use JSNI to read the file header using HTML5 filereader API. However my problem is that GWT does not wait for the result of the reading and continue the code execution. The side effect is that my boolean is not set yet and my condition goes wrong. Is there any mechanism like promise implemented in GWT?
Any help on this would be much appreciated!
UploadImageButtonWidget.java
private boolean isMimeTypeValid = false;
private String mimeType = null;
public native boolean isValid(Element element)/*-{
var widget = this;
var files = element.files;
var reader = new FileReader();
var CountdownLatch = function (limit){
this.limit = limit;
this.count = 0;
this.waitBlock = function (){};
};
CountdownLatch.prototype.countDown = function (){
this.count = this.count + 1;
if(this.limit <= this.count){
return this.waitBlock();
}
};
CountdownLatch.prototype.await = function(callback){
this.waitBlock = callback;
};
var barrier = new CountdownLatch(1);
reader.readAsArrayBuffer(files[0]);
reader.onloadend = function(e) {
var arr = (new Uint8Array(e.target.result)).subarray(0, 4);
var header = "";
for (var i = 0; i < arr.length; i++) {
header += arr[i].toString(16);
}
widget.#com.portal.client.widgets.base.UploadImageButtonWidget::setMimeType(Ljava/lang/String;)(header);
barrier.countDown();
}
return barrier.await(function(){
return widget.#com.portal.client.widgets.base.UploadImageButtonWidget::isMimeTypeValid();
});
}-*/
public void setMimeType(String headerString) {
boolean mimeValid = true;
if (headerString.equalsIgnoreCase(PNG_HEADER)) {
mimeType = PNG_MIMETYPE;
} else if (headerString.equalsIgnoreCase(GIF_HEADER)) {
mimeType = GIF_MIMETYPE;
} else if (headerString.equalsIgnoreCase(JPG_HEADER1) || headerString.equalsIgnoreCase(JPG_HEADER2) || headerString.equalsIgnoreCase(JPG_HEADER3)) {
mimeType = JPG_MIMETYPE;
} else {
mimeValid = false;
setValidationError(i18n.uploadErrorNotImageBasedOnMimeType());
fileChooser.getElement().setPropertyJSO("files", null);
setErrorStatus();
}
setMimeTypeValid(mimeValid);
}
public boolean isMimeTypeValid() {
GWT.log("mimeType" + mimeType);
GWT.log("isMimetypeValid" + String.valueOf(isMimeTypeValid));
return mimeType != null;
}
in the activity:
public void validateAndUpload() {
UploadImageButtonWidget uploadImageButtonWidget = view.getUpload();
if (uploadImageButtonWidget.isValid()) {
GWT.log("mime ok: will be uploaded");
uploadImage();
} else {
GWT.log("mime not ok: will not be uploaded");
}
}

Desktop Computer - Detecting type of internet connection

Is there any way to detect if my computer is connected via cable or mobile network to the internet?
I tried to analyze the trace-route output but realized that it would need a very large and up-to-date database to detect the type of network the data packages take.
Have this two different types of network maybe some different attributes that are easily to detect?
You can use the NetworkListManager APIs to find if a network is connected to the internet. The example below does both that and checks to see if the network is free. You can just remove the Cost related code:
HRESULT GetFreeInternetInterface(_Out_ GUID* pInterfaceGuid)
{
bool fCoInitialized = false;
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (SUCCEEDED(hr))
{
fCoInitialized = true;
CComPtr<INetworkListManager> pNetworkListManager;
hr = pNetworkListManager.CoCreateInstance(__uuidof(NetworkListManager));
if (SUCCEEDED(hr))
{
CComPtr<IEnumNetworkConnections> pNetworkConnections;
hr = pNetworkListManager->GetNetworkConnections(&pNetworkConnections);
bool fFound = false;
while (SUCCEEDED(hr))
{
CComPtr<INetworkConnection> pNetworkConnection;
hr = pNetworkConnections->Next(1, &pNetworkConnection, nullptr);
if (hr == S_OK)
{
CComPtr<INetworkConnectionCost> pNetworkConnectionCost;
hr = pNetworkConnection.QueryInterface(&pNetworkConnectionCost);
if (SUCCEEDED(hr))
{
NLM_CONNECTIVITY nlmConnectivity = NLM_CONNECTIVITY_DISCONNECTED;
DWORD dwCost = NLM_CONNECTION_COST_UNKNOWN;
if (SUCCEEDED(pNetworkConnection->GetConnectivity(&nlmConnectivity)) &&
(nlmConnectivity & (NLM_CONNECTIVITY_IPV4_INTERNET | NLM_CONNECTIVITY_IPV6_INTERNET)) != 0 &&
SUCCEEDED(pNetworkConnectionCost->GetCost(&dwCost)) &&
dwCost == NLM_CONNECTION_COST_UNRESTRICTED &&
SUCCEEDED(pNetworkConnection->GetAdapterId(pInterfaceGuid)))
{
fFound = true;
break;
}
}
}
else hr = E_NOINTERFACE;
}
if (SUCCEEDED(hr) && !fFound)
hr = E_NOINTERFACE;
}
}
if (fCoInitialized) CoUninitialize();
return hr;
}
Once you have the interface GUID and IP Helper APIs to get the interface type. Here is some code I had that looks for non-cellular networks. You can take it and modify it to do what you need:
BOOL GetAdapterAddresses(
_Out_ PIP_ADAPTER_ADDRESSES * ppIAA
)
{
*ppIAA = NULL;
DWORD len = 0;
DWORD flags = GAA_FLAG_SKIP_ANYCAST|GAA_FLAG_SKIP_MULTICAST|GAA_FLAG_SKIP_DNS_SERVER;
if (GetAdaptersAddresses(AF_UNSPEC, flags, NULL, NULL, &len) != ERROR_BUFFER_OVERFLOW)
return FALSE;
PIP_ADAPTER_ADDRESSES pIAA = (PIP_ADAPTER_ADDRESSES)LocalAlloc(LPTR, len);
if (pIAA) {
GetAdaptersAddresses(AF_UNSPEC, flags, NULL, pIAA, &len);
*ppIAA = pIAA;
return TRUE;
}
return FALSE;
}
bool IsNonCellularIfType(
_In_ IFTYPE IfType
)
{
return // Ignore Loopback and WWAN
IfType != IF_TYPE_SOFTWARE_LOOPBACK &&
IfType != IF_TYPE_WWANPP &&
IfType != IF_TYPE_WWANPP2;
}
DWORD GetNonCellularIfIndex(
)
{
DWORD IfIndex = (DWORD)(-1);
PIP_ADAPTER_ADDRESSES pIAA;
if (GetAdapterAddresses(&pIAA)) {
PIP_ADAPTER_ADDRESSES pIAAList = pIAA;
while (pIAA) {
// Look for Non-Cellular interface
if (pIAA->OperStatus == IfOperStatusUp &&
IsNonCellularIfType(pIAA->IfType)) {
PIP_ADAPTER_UNICAST_ADDRESS_LH pUnicastAddr = pIAA->FirstUnicastAddress;
// Look through all unicast addresses for valid IPv4 or IPv6 addresses
while (pUnicastAddr) {
LPSOCKADDR pAddr = pUnicastAddr->Address.lpSockaddr;
if (pAddr->sa_family == AF_INET) {
if (!IN4_IS_ADDR_LINKLOCAL(&((SOCKADDR_IN *)pAddr)->sin_addr)) {
IfIndex = pIAA->IfIndex;
break;
}
}
else if (pAddr->sa_family == AF_INET6) {
if (!IN6_IS_ADDR_LINKLOCAL(&((SOCKADDR_IN6 *)pAddr)->sin6_addr)) {
IfIndex = pIAA->IfIndex;
break;
}
}
pUnicastAddr = pUnicastAddr->Next;
}
// No need to keep looking any more?
if (IfIndex != (DWORD)(-1))
break; // while (pIAA)
}
pIAA = pIAA->Next;
}
LocalFree(pIAAList);
}
return IfIndex;
}

Push notification not received, windows universal 8.1

HI i have implemented push notification in my application, serer is sending notification but not receiving at my end.
void OnPushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs e)
{
string typeString = String.Empty;
string notificationContent = String.Empty;
switch (e.NotificationType)
{
case PushNotificationType.Badge:
typeString = "Badge";
notificationContent = e.BadgeNotification.Content.GetXml();
break;
case PushNotificationType.Tile:
notificationContent = e.TileNotification.Content.GetXml();
typeString = "Tile";
break;
case PushNotificationType.Toast:
notificationContent = e.ToastNotification.Content.GetXml();
typeString = "Toast";
// Setting the cancel property prevents the notification from being delivered. It's especially important to do this for toasts:
// if your application is already on the screen, there's no need to display a toast from push notifications.
e.Cancel = true;
break;
case PushNotificationType.Raw:
notificationContent = e.RawNotification.Content;
typeString = "Raw";
break;
}
// string text = "Received a " + typeString + " notification, containing: " + notificationContent;
var ignored = dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
// rootPage.NotifyUser(text, NotifyType.StatusMessage);
if (typeString == "Toast")
{
PushNotificationHelper.AddTostNotification(0, notificationContent);
}
else if (typeString == "Badge")
{
PushNotificationHelper.AddBadgeNotification(0, notificationContent);
}
});
}
public async void InitChannel()
{
Windows.Storage.ApplicationDataContainer roamingSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
try
{
var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
if (channel != null)
{
//String existingChannel = (String)roamingSettings.Values["ExistingPushChannel"];
roamingSettings.Values["ExistingPushChannel"] = channel.Uri;
dispatcher = Windows.UI.Core.CoreWindow.GetForCurrentThread().Dispatcher;
channel.PushNotificationReceived += OnPushNotificationReceived;
}
else
{
roamingSettings.Values["ExistingPushChannel"] = "Failed to create channel";
}
}
catch
{
roamingSettings.Values["ExistingPushChannel"] = "Failed to create channel";
}
}
public async void InitNotificationsAsync()
{
try
{
Windows.Storage.ApplicationDataContainer roamingSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
String existingChannel = (String)roamingSettings.Values["ExistingPushChannel"];
string tempDevelopmentKey = "";
string Platform = "";
List<string> arrayTags = new List<string>();
#if WINDOWS_APP
Platform = "windows-tablet";
tempDevelopmentKey = "dev_WindowsTabletNotification";
#endif
#if WINDOWS_PHONE_APP
Platform = "windows-phone";
tempDevelopmentKey ="dev_WindowsPhoneNotification";
#endif
arrayTags.Add(Platform) ;
arrayTags.Add(tempDevelopmentKey) ;
string TMBNotification = (string)roamingSettings.Values["TMBNotification"];
if(TMBNotification != null)
{
if(TMBNotification == "on")
{
arrayTags.Add("TMB");
}
}
string TRSNotification = (string)roamingSettings.Values["TRSNotification"];
if (TRSNotification != null)
{
if (TRSNotification == "on")
{
arrayTags.Add("TRS");
}
}
string IMNotification = (string)roamingSettings.Values["IMNotification"];
if (IMNotification != null)
{
if (IMNotification == "on")
{
arrayTags.Add("IM");
}
}
string SWSNotification = (string)roamingSettings.Values["SWSNotification"];
if (SWSNotification != null)
{
if (SWSNotification == "on")
{
arrayTags.Add("ANC");
}
}
string VIDNotification = (string)roamingSettings.Values["VIDNotification"];
if (VIDNotification != null)
{
if (VIDNotification == "on")
{
arrayTags.Add("videos");
}
}
var hub = new NotificationHub("hubname", "endpoint");
var result = await hub.RegisterNativeAsync(existingChannel, arrayTags);
// Displays the registration ID so you know it was successful
if (result.RegistrationId != null)
{
}
}catch
{
}
}
So how can i confirm that there is no issue in my implementation or there is issue from server end.
There are couple of steps you can take to debug this problem:-
There is broadcast and sending notification, using notification hub, option available on azure portal(you can do from VS also from left side server explorer). When you did this there is a log that will show you whether a notifications sent successfully or not.
First just delete all your registrations with notification hub and for very new registration check is your device getting registered with correct tags/channel uri or not(this you cad do from server explorer too)
Make sure you are sending/registering the correct templates that are corresponding to WNS service.
make sure you are using WNS service this different from what is for
WP8 silverlight one.
You can see the errors just at the bottom of this page if any.
There is option for device registrations from where you can alter the registrations.

Resources