In my Xamarin.iOS app, I want to retry an upload after a failure due to http error.
But I'm not sure how to implement.
I was thinking I should just keep creating a new task every time it completes with an http error?
Is this the correct way to implement?
NSUrlSessionTaskDelegate:
public async override void DidCompleteWithError(NSUrlSession session, NSUrlSessionTask task, NSError error)
{
if (task.Response != null)
{
if (task.Response is NSHttpUrlResponse httpResponse)
{
if (httpResponse.StatusCode == 200)
{
//some clean up logic here
}
else if (httpResponse.StatusCode > 400 && httpResponse.StatusCode < 500)
{
//what to do to upload again?
}
else
{
var applicationException = new ApplicationException("Server returned 500");
Crashes.TrackError(applicationException);
}
}
}
}
AppDelegate:
public NSUrlSession InitBackgroundSession()
{
string identifier = "com.uba.BookingUploadBackgroundSession";
INSUrlSessionDelegate backgroundUploadSessionDelegate = new BackgroundBookingUploadSessionDelegate();
using (var config = NSUrlSessionConfiguration.CreateBackgroundSessionConfiguration(identifier))
{
config.HttpMaximumConnectionsPerHost = 4;
config.TimeoutIntervalForResource = 300.0; //5mins
return NSUrlSession.FromConfiguration(config, backgroundUploadSessionDelegate, new NSOperationQueue());
}
}
//gets called when user taps Submit from ViewController
public void UploadFile(BookingDTO newBookingDTO)
{
//logic to create NSMutableUrlRequest omitted
var uploadTask = session.CreateUploadTask(request, NSUrl.FromFilename(filename));
uploadTask.TaskDescription = newBookingDTO.ClientId; //we need the booking id so we can delete it later
uploadTask.Resume();
}
I'll accept any answer in C#, Swift or Objective-C
NSURLSessionTask has four different states.As shown in the following image.
When the upload finished.Whether success or fail,the states will become Completed.And the property CurrentRequest will be null. You can only handle the Response of the task.So .I think you can use the same session but have to create a new task.You can use the Notification to send message.
NSUrlSessionTaskDelegate:
public async override void DidCompleteWithError(NSUrlSession session, NSUrlSessionTask task, NSError error)
{
if (task.Response != null)
{
if (task.Response is NSHttpUrlResponse httpResponse)
{
if (httpResponse.StatusCode == 200)
{
//some clean up logic here
}
else if (httpResponse.StatusCode > 400 && httpResponse.StatusCode < 500)
{
//send notification here
NSNotification notification = NSNotification.FromName("UploadFail",null,null);
NSNotificationCenter.DefaultCenter.PostNotification(notification);
}
else
{
var applicationException = new ApplicationException("Server returned 500");
Crashes.TrackError(applicationException);
}
}
}
}
And in AppDelegate.cs , add the notification in method FinishedLaunching
NSNotificationCenter.DefaultCenter.AddObserver(this,new Selector("UploadAgain"),new NSString("UploadFail"),null);
[Export("UploadAgain")]
void UploadAgain()
{
//call your upload code here
}
Related
here is the code can anyone tell how can I setSkuDetails()
as I was using vision one now I update it to 4
However, setSku and setType seem to be deprecated in the BillingFlowParams.Builder class. Instead, we should be using setSkuDetails(SkuDetails).
private void BillingFunction() {
mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
// Establish connection to billing client
mBillingClient = BillingClient.newBuilder(MainActivity.this).setListener(MainActivity.this).build();
mBillingClient.startConnection(new BillingClientStateListener() {
#Override
public void onBillingSetupFinished(#NonNull BillingResult billingResult) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
// The billing client is ready. You can query purchases here.
getPricesMonthlyTime();
getPricesYearlyTime();
getPricesONeTime();
}
}
#Override
public void onBillingServiceDisconnected() {
//TODO implement your own retry policy
Toast.makeText(MainActivity.this, getResources().getString(R.string.billing_connection_failure), Toast.LENGTH_SHORT);
// Try to restart the connection on the next request to
// Google Play by calling the startConnection() method.
}
});
continue_button.setOnClickListener(view -> {
if (select_radio_one.getVisibility() == View.VISIBLE) {
BillingFlowParams flowParams = BillingFlowParams.newBuilder()
.setSkuDetails()
.build();
BillingResult responseCode = mBillingClient.launchBillingFlow(MainActivity.this, flowParams);
brandDialogInAppPurchase.dismiss();
} else if (select_radio_two.getVisibility() == View.VISIBLE) {
BillingFlowParams flowParams = BillingFlowParams.newBuilder()
.setSkuDetails()
.build();
BillingResult responseCode = mBillingClient.launchBillingFlow(MainActivity.this, flowParams);
brandDialogInAppPurchase.dismiss();
} else if (select_radio_three.getVisibility() == View.VISIBLE) {
BillingFlowParams flowParams = BillingFlowParams.newBuilder()
.setSkuDetails()
.build();
BillingResult responseCode = mBillingClient.launchBillingFlow(MainActivity.this, flowParams);
brandDialogInAppPurchase.dismiss();
} else {
Toast.makeText(MainActivity.this, "Nothing selected", Toast.LENGTH_SHORT).show();
}
});
// queryPrefPurchases();
queryPurchases();
}
You should send the object skuDetail.
To do so you need to retrieve it by calling querySkuDetailsAsync().
fun querySkuDetails() {
val skuList = ArrayList<String>()
skuList.add("premium_upgrade")
skuList.add("gas")
val params = SkuDetailsParams.newBuilder()
params.setSkusList(skuList).setType(SkuType.INAPP)
// leverage querySkuDetails Kotlin extension function
val skuDetailsResult = withContext(Dispatchers.IO) {
billingClient.querySkuDetails(params.build())
}
// Process the result.
}
I have developed app for ios and android in xamarin form cross platform. The problem I am facing in notification is that i can receive notification in android and its working absolutely fine, even I can open specific page from notification data, but in IOS I am not able to receieve notification. Here is my code from where I am sending notification
public async void NewTaskNotification(string user_token, string titlee, string bodyy, string openpage, long? mrno, DateTime? appointmentdate)
{
i++;
WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
tRequest.Method = "post";
tRequest.ContentType = "application/json";
var objNotification = new
{
to = user_token,
data = new
{
title = titlee,
body = bodyy,
priority = "high",
id = i,
icon = "ic_stat_ic_stat_ic_notification",
color = "#2F3C51",
Background = "#2F3C51",
open_page = openpage,
mr_no = mrno,
appointmentDate = appointmentdate,
},
};
string jsonNotificationFormat = Newtonsoft.Json.JsonConvert.SerializeObject(objNotification);
Byte[] byteArray = Encoding.UTF8.GetBytes(jsonNotificationFormat);
tRequest.Headers.Add(string.Format("Authorization: key={0}", "AAAAoQ445fk:APA91bHagr12v6bGUqE2d8soHCMXwo4rD6wyM_LFp6yD9b968J3SQQ9u8T5rsFBtsPzL-ct_cCjad_YPpEjaw5tq_OqR_asB5-zgKqyQfhV2djFxAAbK7PBPCZHMI2Y6KmNN8R-MItOA"));
tRequest.Headers.Add(string.Format("Sender: id={0}", "691728344569"));
tRequest.ContentLength = byteArray.Length;
tRequest.ContentType = "application/json";
using (Stream dataStream = tRequest.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
using (WebResponse tResponse = tRequest.GetResponse())
{
using (Stream dataStreamResponse = tResponse.GetResponseStream())
{
using (StreamReader tReader = new StreamReader(dataStreamResponse))
{
String responseFromFirebaseServer = tReader.ReadToEnd();
FCMResponse response = Newtonsoft.Json.JsonConvert.DeserializeObject<FCMResponse>(responseFromFirebaseServer);
if (response.success == 1)
{
Console.WriteLine("succeeded");
}
else if (response.failure == 1)
{
Console.WriteLine("failed");
}
}
}
}
}
}
with this code i am able to recieve notification and even open specific page but only with android, its not working with ios, but when i try to send notification to IOS from firebase console, its working fine.
Here is my appdelegate.cs
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
public override bool FinishedLaunching(UIApplication uiApplication, NSDictionary launchOptions)
{
Rg.Plugins.Popup.Popup.Init();
CachedImageRenderer.Init();
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App());
FirebasePushNotificationManager.Initialize(launchOptions, true);
FirebasePushNotificationManager.CurrentNotificationPresentationOption = UNNotificationPresentationOptions.Alert | UNNotificationPresentationOptions.Badge;
UNUserNotificationCenter.Current.Delegate = new UserNotificationCenterDelegate();
return base.FinishedLaunching(uiApplication, launchOptions);
}
public class UserNotificationCenterDelegate : UNUserNotificationCenterDelegate
{
public UserNotificationCenterDelegate()
{
}
public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
// Do something with the notification
Console.WriteLine("Active Notification: {0}", notification);
// Tell system to display the notification anyway or use
// `None` to say we have handled the display locally.
completionHandler(UNNotificationPresentationOptions.Alert);
}
}
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
FirebasePushNotificationManager.DidRegisterRemoteNotifications(deviceToken);
}
public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
{
FirebasePushNotificationManager.RemoteNotificationRegistrationFailed(error);
}
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
FirebasePushNotificationManager.DidReceiveMessage(userInfo);
// Do your magic to handle the notification data
System.Console.WriteLine(userInfo);
completionHandler(UIBackgroundFetchResult.NewData);
}
}
I need to know where I am doing wrong for IOS
I am trying to insert a new entry in my database using web api. I have two web projects: one is a UI project where all the user interaction will occur and the other is a services project which will handle all interactions with my database.
Below is my post method that will take in form data for creating a new team.
// POST: Api/Team/Create
[HttpPost]
public ActionResult Create(Team team)
{
try
{
if (ModelState.IsValid)
{
HttpEndPointContext httpEndPoint = new HttpEndPointContext()
{
AuthenticationMethod = HttpAuthenticationMethods.None,
Ssl = false,
HttpMethod = HttpMethod.Post,
Path = "localhost:32173/api/team/",
QueryStrings = null,
PayloadData = SerializationHelper.Current.Serialize(team.ToString(), SerializationTypes.Xml)
};
IProcessResult result = HttpConnectionManager.Current.SendMessage(httpEndPoint);
}
return RedirectToAction("Index");
}
catch
{
return View();
}
}
And this is my method for dealing with my PayloadStream/PayloadData attribute in the above method:
private void StreamPayload(HttpWebRequest webRequest, HttpEndPointContext httpEndPointContext)
{
if (httpEndPointContext.HttpMethod == new HttpMethod("GET"))
return;
//TODO: FIX MAYBE .... sometimes we want to post body with GET.
//Stream vs string
if (httpEndPointContext.PayloadStream == null)
{
//Wrap with SOAP Envelope and method if defined in SoapDefinition
string data = httpEndPointContext.PayloadData ?? String.Empty;
if (httpEndPointContext.SoapDefinition != null)
{
//If parameters is set, clear existing payload data.
data = String.Empty;
if (httpEndPointContext.SoapDefinition.Parameters != null)
foreach (var parameter in httpEndPointContext.SoapDefinition.Parameters)
{
data += String.Format("<{0}>{1}</{0}>", parameter.Key, parameter.Value);
}
data = String.Format("<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'>" +
"<s:Body><{0} xmlns='{2}'>" +
"{1}</{0}></s:Body></s:Envelope>",
httpEndPointContext.SoapDefinition.SoapMethod, data,httpEndPointContext.SoapDefinition.SoapGlobalKey);
}
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(data);
httpEndPointContext.PayloadStream = new MemoryStream(byteArray);
}
using (Stream requestStream = webRequest.GetRequestStream())
{
StreamHelper.Current.CopyStreams(httpEndPointContext.PayloadStream, requestStream);
requestStream.Close();
}
}
And the code for getting the server response. I'm currently getting an Internal Server (500) Error. Not sure why.
public IProcessResult SendMessage(HttpEndPointContext httpEndPointContext)
{
HttpWebRequest webRequest = CreateWebRequest(httpEndPointContext);
StreamPayload(webRequest, httpEndPointContext);
IProcessResult result = GetWebResponse(webRequest, httpEndPointContext);
return result;
}
private IProcessResult GetWebResponse(HttpWebRequest webRequest, HttpEndPointContext httpEndPointContext)
{
//Get Response
WebResponse response;
IProcessResult result = new ProcessResult(Statuses.Success);
try
{
response = webRequest.GetResponse();
}
catch (System.Net.WebException ex)
{
//Do exception handling. Still get the response for 500s etc.
result.Error.Exception = ex;
result.Status = Constants.Statuses.FailedUnknown;
result.ResponseCodeDescription = ex.Status.ToString();
result.ResponseCode = ex.Status.ToString();
result.Error.ErrorCode = ex.Status.ToString();
response = ex.Response;
//The error did not have any response, such as DNS lookup.
if (response == null)
return result;
}
try
{
//Get the response stream.
Stream responseData = response.GetResponseStream();
if (responseData == null)
throw new CoreException("No Response Data in GetWebResponse.",
"No Response Data in GetWebResponse. EndPoint:{0}", httpEndPointContext.ToString());
// Open the stream using a StreamReader for easy access.
var reader = new StreamReader(responseData);
// Read the content.
result.ResponseData = reader.ReadToEnd();
}
finally
{
response.Close();
}
result.ResponseCode = ((int)((HttpWebResponse)response).StatusCode).ToString();
result.ResponseCodeDescription = ((HttpWebResponse) response).StatusDescription;
return result;
}
And finally, my method for inserting to the database, found in my services project:
//POST api/controller/5
public IProcessResult Insert(Team team)
{
return TeamBusinessManager.Current.Insert(SecurityManager.Current.ConnectionContext, new Team());
}
I'm confused as to why I'm getting the 500 error. I'm not sure if it's the PayloadData attribute in my POST method or is it something wrong with my method in my services project.
I am using the following code (on Xamarin) to log in using the latest Facebook SDK (I am also using Parse to manage my backend):
partial void LoginWithFacebook (UIButton sender)
{
LoginManager login = new LoginManager();
login.LogInWithReadPermissionsAsync(kPermissions).ContinueWith(t => {
if (t.IsFaulted && t.Exception != null) {
Console.Error.WriteLine("Error while authenticating: {0}", t.Exception.Message);
} else {
var result = t.Result;
if (result.IsCancelled) {
Console.Error.WriteLine("User canceled the operation");
} else {
Console.WriteLine("Authenticated!");
ParseFacebookUtils.LogInAsync(result.Token.UserID, result.Token.TokenString, (DateTime)result.Token.ExpirationDate).ContinueWith(loginTask => {
if (!loginTask.IsFaulted) {
InvokeOnMainThread(() => PerformSegue("GoToDashboard", null));
} else {
Console.Error.WriteLine("Could not login to Parse");
}
});
}
}
});
}
And then, to load the friends list (who are also using the App) I use the following code:
if (AccessToken.CurrentAccessToken != null) {
var request = new GraphRequest ("me/friends", null);
request.Start (new GraphRequestHandler ((connection, result, error) => {
if (error != null) {
Console.Error.WriteLine("Error fetching the friends list");
} else {
Console.WriteLine("Result: {0}", result);
}
hud.Hide(true);
}));
}
But the AccessToken always seem to be null even if the authentication is successful. I tried setting it by hand after the authentication but when the App restarts, it is lost again.
EDIT
I have removed the condition "if (AccessToken.CurrentAccessToken != null)" and it makes the request with no problems so I guess I am just using the wrong way to detect if the user is logged in. What's the correct way?
Thanks to the question referenced by #VijayMasiwal I was able to discover the problem. I had forgot to initialize Facebook in the App Delegate.
Here is how the FinishedLaunching method should be implemented when using Facebook:
public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
{
Settings.AppID = kFacebookAppID;
Settings.DisplayName = kFacebookDisplayName;
// I had forgotten this line :)
return ApplicationDelegate.SharedInstance.FinishedLaunching (application, launchOptions);
}
Hope that helps.
I've created the Class StartConnection to handle my NSURL requests. It gets called from my AppDelegate twice and that works well. It's called by one other class as well and that also works well. This is the implementation of StartConnection:
#import "StartConnection.h"
#import "BlaBlaBlog-swift.h"
#implementation StartConnection
{
BOOL startedForBlog;
}
- (void)getRssFileWithUrl: (NSString*)rssUrlString forBlog:(BOOL)forBlog
{
startedForBlog = forBlog;
NSURL *url = [NSURL URLWithString:rssUrlString];
NSURLRequest *rssRequest = [NSURLRequest requestWithURL:url];
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:rssRequest delegate:self];
[connection start];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
dataSize = [response expectedContentLength];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
if (receivedData==nil )
{
receivedData = [[NSMutableData alloc]init];
}
// Append the new data to the instance variable you declared
[receivedData appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
self.receivedDataComplete = receivedData;
if (startedForBlog){
[self.delegate performSelector: #selector(receiveDataCompleted)];
}
else
[self.delegate performSelector: #selector(receivePodCastDataCompleted)];
}
To get some hands on experience with SWIFT I've added an experimental SWIFT class to my code that also uses StartConnection.h. In the debugger I can see an instance of StartConnection being created and the getFileWithUrl methode seems to be kicked of normally. But that's all that happens. None of the delegate methods is called.
This is SWIFT class:
import UIKit
class CheckActuality: NSObject, GetReceivedDataProtocol, WordPressParserDelegate {
var retrievePostData = StartConnection()
var parseCompleted: Bool=false
var result: Bool = true
lazy var wpParser = WordPressParser()
lazy var defaults = NSUserDefaults.standardUserDefaults()
func isActual () -> Bool {
var url = "http://blablablog.nl/new_api.php?function=get_recent_posts&count=1"
self.retrievePostData.delegate=self
self.retrievePostData.getRssFileWithUrl(url, forBlog:true)
while !self.parseCompleted
{
// wait till wpparser has completed
}
if self.wpParser.arrayWithPostDictionaries.count == 1
// Some info has been retrieved
{
var posts: NSArray = wpParser.arrayWithPostDictionaries
var post: NSDictionary = posts.objectAtIndex(0) as NSDictionary
var latestPostUrl: String = post.objectForKey("postUrl") as String
var currentLatestPostUrl = defaults.stringForKey("ttt")
if latestPostUrl != currentLatestPostUrl {
result = false
}
else {
result = true
}
}
return result
}
func receiveDataCompleted () {
if self.retrievePostData.receivedDataComplete != nil
{
self.wpParser.delegate=self
self.wpParser.parseData(retrievePostData.receivedDataComplete)
}
else
{
// warning no internet
}
}
func wpParseCompleted () {
self.parseCompleted=true
}
}
And to be complete, the call in my AppDelegate look like this:
//
// retrieving PostData. Create a connection, set delegate to self en start with created url
//
retrievePostData = [[StartConnection alloc]init];
retrievePostData.delegate = self;
NSString *url = [[wordPressUrl stringByAppendingString:apiString] stringByAppendingString:pageCountString];
[retrievePostData getRssFileWithUrl:url forBlog:(BOOL)true];
//
// retrieving PodcastData. Create a connection, set delegate to self en start with created url
//
retrievePodCastData = [[StartConnection alloc]init];
retrievePodCastData.delegate = self;
[retrievePodCastData getRssFileWithUrl:podcastUrl forBlog:(BOOL)false];
I'm breaking my head for almost a day. Hope some of you far more experienced guys can help this starter out.
The while !parseCompleted loop is blocking the main thread until the download and parsing is done. But the processing of the received data happens on the main thread, too, so if that thread is blocked, your app will be deadlocked.
I would eliminate that while loop and put all of the post processing inside your receivedDataComplete method.
By the way, implicit in this change is the fact that isActual must be an asynchronous method. Thus, rather than returning a Bool value, it should be a Void return type but you should employ the completionHandler pattern (and I'd change it to also return an error object, too):
class CheckActuality: NSObject, GetReceivedDataProtocol, WordPressParserDelegate {
let errorDomain = "com.domain.app.CheckActuality"
lazy var retrievePostData = StartConnection()
lazy var wpParser = WordPressParser()
lazy var defaults = NSUserDefaults.standardUserDefaults()
var completionHandler: ((latestPost: Bool!, error: NSError?)->())!
func isActual(completionHandler: (latestPost: Bool!, error: NSError?)->()) {
// save the completionHandler, which will be called by `receiveDataCompleted` or `wpParseCompleted`
self.completionHandler = completionHandler
// now perform query
let url = "http://blablablog.nl/new_api.php?function=get_recent_posts&count=1" // as an aside, use `let` here
retrievePostData.delegate = self // also note that use of `self` is redundant here
retrievePostData.getRssFileWithUrl(url, forBlog:true)
}
func receiveDataCompleted () {
if retrievePostData.receivedDataComplete != nil {
wpParser.delegate = self
wpParser.parseData(retrievePostData.receivedDataComplete)
} else {
// frankly, I'd rather see you change this `receiveDataCompleted` return the `NSError` from the connection, but in the absence of that, let's send our own error
let error = NSError(domain: errorDomain, code: 1, userInfo: nil)
completionHandler(latestPost: nil, error: error)
}
}
func wpParseCompleted () {
if wpParser.arrayWithPostDictionaries.count == 1 { // Some info has been retrieved
let posts: NSArray = wpParser.arrayWithPostDictionaries
let post: NSDictionary = posts.objectAtIndex(0) as NSDictionary
let latestPost: String = post.objectForKey("postUrl") as String
let currentlatestPost = defaults.stringForKey("ttt")
completionHandler(latestPost: (latestPost != currentlatestPost), error: nil)
}
// again, I'd rather see you return a meaningful error returned by the WordPressParser, but I'll make up an error object for now
let error = NSError(domain: errorDomain, code: 2, userInfo: nil)
completionHandler(latestPost: nil, error: error)
}
}
Now, I don't know if latestPost is the appropriate name for the value you were trying to return, so change that to whatever makes sense for your routine. Also, the name isActual doesn't really make sense, but I'll let you change that to whatever you want.
Anyway, when you use it, you'd use the trailing closure syntax to specify the completionHandler block that will be performed asynchronously:
let checkActuality = CheckActuality()
func someFunc() {
checkActuality.isActual() { latestPost, error in
if error != nil {
// do whatever error handling you want
println(error)
} else if latestPost {
// yes, latest post
} else {
// nope
}
}
// note, do not try to check `latestPost` here because the
// above closure runs asynchronously
}
Needless to say, this is a completionHandler pattern, but looking at your code, you seem to favor delegate patterns. If you wanted to implement this using the delegate pattern, you can. But the idea is the same: This isActual method (whatever you end up renaming it to) runs asynchronously, so you have to inform the caller when it is complete.