PushSharp Apns notification error: 'ConnectionError' - ios

I'm using PushSharp 4.0.10, MVC 4 with c#
In the OnNotificationFailed event of the Apns broker, I get ConnectionError exception.
This exception happened suddenly after change certificate(.p12) file; and it worked fine before this change.
Please advise how to troubleshoot this error.
var certificate = System.IO.File.ReadAllBytes(System.Web.Hosting.HostingEnvironment.MapPath("~/Content/Mobile/consumer_dev.p12"));
var config = new ApnsConfiguration(ApnsConfiguration.ApnsServerEnvironment.Sandbox, certificate, "", true);
var apnsBroker = new ApnsServiceBroker(config);
apnsBroker.OnNotificationFailed += (notification, aggregateEx) => {
aggregateEx.Handle (ex => {
if (ex is ApnsNotificationException) {
var notificationException = (ApnsNotificationException)ex;
var apnsNotification = notificationException.Notification;
var statusCode = notificationException.ErrorStatusCode;
Debug.WriteLine(apnsNotification.Identifier + ", " + statusCode);
} else {
Debug.WriteLine(ex.InnerException);
}
return true;
});
};
apnsBroker.OnNotificationSucceeded += (notification) => {
Debug.WriteLine("Apple Notification Sent!");
};
apnsBroker.Start();
foreach (var deviceToken in to)
{
apnsBroker.QueueNotification(new ApnsNotification
{
DeviceToken = deviceToken,
Payload = JObject.Parse("{\"aps\":" + aps.ToString().Replace('=', ':') + "}")
});
}
apnsBroker.Stop();

this error is because certificate you used is not enabled pushnotification.
you have to to enable it from apple id and then create new certificate (.12) and provisioning profile.
try with that new certificate will resolve your error.

Try by passing only first two parameters to apnsconfiguration constructor, or else remove validateIsApnsCertificate (bool ) parameter.
It's working fine for me first three parameters.
var config = new ApnsConfiguration(ApnsConfiguration.ApnsServerEnvironment.Production, appleCert, P12Password);

Related

How to send mail with attachments in Flutter?

I found dart plugin called mailer3: "^1.1.9". Previously I create an image in mobile temp directory. In Flutter mobile app I try to send this saved picture using mailer3 plugin as a mail. The mail reach the destination, I don't get error but seems lost the attachment in process.
In dart it works very well and send the attachment as well. In flutter I can use the temp directory to show the image in app but cannot be able attache to mail.
The image location is in the temp folder of the device:
'/data/user/0/com.myApp.myApp/app_flutter/20180700087.jpg'
I can show the image using below code:
new FileImage(File('$newDekontImage'),
Error:
E/flutter (21184): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
E/flutter (21184): FileSystemException: Cannot open file, path = '/data/user/0/com.myApp.myApp/app_flutter/20180700087.jpg' (OS Error: No such file or directory, errno = 2)
How to send mail with attachments in Flutter with provided information in this question?
The Flutter Code:
// TODO: SEND MAIL
void _sendMail() async {
if (!_formKey.currentState.validate()) {
return;
} else {
_formKey.currentState.save();
var _options = new GmailSmtpOptions()
..username = “mymailaddress#gmail.com"
..password = “myPassword”;
var _emailTransport = new SmtpTransport(_options);
var _envelope = new Envelope()
..from = "mymailaddress#gmail.com"
..recipients.add(_receiverMailAddress)
..subject = "${_userDekontDetails[0][0].toString()} - Receipt”
..attachments.add(await new Attachment(file: await new File('$newDekontImage')))
..text = "${_userDekontDetails[0][0].toString()} - Receipt"
..html = '<h3>${_userDekontDetails[0][0].toString()} Receipt.</h3>'
'<p>Hi, registered under my name, I am sending the receipt (${widget._currentUserReceiptNo}) with attached to this mail.</p>'
'<p></p>'
'<h5>Regards, </br></h5>'
'${_userDekontDetails[0][0].toString()}';
_emailTransport.send(_envelope)
..then((envelope) => print('Email sent'))
..catchError((e) => print('Error occured: $e'));
}
}
As of this writing, the mailer3 plugin is outdated and mailer is the most up-to-date plugin for sending emails. The mailer plugin currently contains important fixes that mailer2 and mailer3 has. I suggest opting to use mailer package instead of mailer3.
Here's a port of your code snippet from mailer3 to mailer
_sendMail(String username, String accessToken) async {
// Read https://pub.dev/documentation/mailer/latest/smtp_server_gmail/gmailSaslXoauth2.html
var _emailTransport = gmailSaslXoauth2(username, accessToken);
var _envelope = new Message()
..from = "mymailaddress#gmail.com"
..recipients.add("recepient#gmail.com")
..subject = '{EMAIL_SUBJECT_GOES_HERE}'
// Read https://pub.dev/documentation/mailer/latest/mailer/FileAttachment-class.html
..attachments
.add(FileAttachment(File('{FILE_PATH}')))
..text = '{PLAIN_TEXT_GOES_HERE}'
..html = '{HTML_CONTENT_GOES_HERE}';
send(_envelope, _emailTransport)
..then((envelope) => print('Email sent'))
..catchError((e) => print('Error occured: $e'));
}
I have used enough_mail, Hope it will be helpful.
MessageBuilder messageBuilder = MessageBuilder();
Future<bool> onFileSelect(BuildContext context) async {
final result = await FilePicker.platform
.pickFiles(type: FileType.any, allowMultiple: true, withData: true);
if (result == null) {
return false;
}
for (final file in result.files) {
final lastDotIndex = file.path.lastIndexOf('.');
MediaType mediaType;
if (lastDotIndex == -1 || lastDotIndex == file.path.length - 1) {
mediaType = MediaType.fromSubtype(MediaSubtype.applicationOctetStream);
} else {
final ext = file.path.substring(lastDotIndex + 1);
mediaType = MediaType.guessFromFileExtension(ext);
}
messageBuilder.addBinary(file.bytes, mediaType, filename: file.name);
}
return true;
}
Future<void> sendMail(BuildContext buildContext) async {
setState(() {
needToFreezeUi = true;
});
MySnackBar.show(buildContext, MySnackBar.loadingIcon, "Please wait...!");
SmtpClient smtpClient = SmtpClient(domain, isLogEnabled: true);
try {
await smtpClient.connectToServer(
"$serverPrefix.${userInfo.domainName}",
smtpServerPort,
isSecure: isSmtpServerSecure
);
await smtpClient.ehlo();
await smtpClient.authenticate(userInfo.email, userInfo.password);
messageBuilder.from = [MailAddress('', userInfo.email)];
messageBuilder.to = [MailAddress('', toTextEditCtl.text)];
messageBuilder.cc = selectedCCEmailInfos.map((e) => MailAddress('',e.emailAddress)).toList();
messageBuilder.bcc = selectedBCCEmailInfos.map((e) => MailAddress('',e.emailAddress)).toList();
messageBuilder.subject = subjectTextEditCtl.text;
String htmlText = await htmlEditorController.getText();
messageBuilder.addTextHtml(htmlText);
messageBuilder.hasAttachments ? messageBuilder.getPart(
MediaSubtype.multipartAlternative,
recursive: false
) : messageBuilder.addPart(
mediaSubtype: MediaSubtype.multipartAlternative,
insert: true
);
if (!messageBuilder.hasAttachments) {
messageBuilder.setContentType(
MediaType.fromSubtype(MediaSubtype.multipartAlternative)
);
}
MimeMessage mimeMessage = messageBuilder.buildMimeMessage();
SmtpResponse smtpResponse = await smtpClient.sendMessage(mimeMessage);
MySnackBar.hide(buildContext);
if(smtpResponse.isOkStatus){
MySnackBar.show(buildContext,MySnackBar.successIcon,"Mail send successfully");
clearInputFields(buildContext);
}else {
MySnackBar.show(buildContext,MySnackBar.errorIcon,"Something went wrong, please try again!");
}
} on SmtpException catch (e) {
MySnackBar.show(buildContext,MySnackBar.errorIcon,"Something went wrong, please try again!");
}
setState(() {
needToFreezeUi = false;
});
}
============================
dependencies:
enough_mail: ^1.3.4
html_editor_enhanced: ^1.4.0
file_picker: ^3.0.2+2

I'm getting the errorNum :8 while sending the push notifications to ios devices

var apn = require('apn');
var gcm = require('android-gcm');
export default function notification( devicetype, devicetoken, alert, userid, action, profilepic, image, youtubeimage, id ) {
if(devicetoken != "(null)") {
var androidApiKey = '', cert = '', key = '', passphrase = '';
if(process.env.NODE_ENV.toLowerCase() == "production") {
cert = '/../config/ios_support/apns-cert.pem';
key = '/../config/ios_support/apns-key.pem';
passphrase = '*****';
androidApiKey = "*******";
}
else {
cert = '/../config/ios_support/apns-dev-cert.pem';
key = '/../config/ios_support/apns-dev-key.pem';
passphrase = '*******';
androidApiKey = "********";
}
if(devicetype == "ios"){
var myDevice = new apn.Device(devicetoken);
var note = new apn.Notification();
note.badge = 1;
note.sound = "notification-beep.wav";
note.alert = alert;
note.category = "respond"
note.device = myDevice;
note.payload = { 'action': action, 'userid': userid, 'profilepic': profilepic, 'id':id};
console.log("note.payload: "+ JSON.stringify(note.payload));
//, 'WatchKit Simulator Actions': [{"title": "Show", "identifier": "showButtonAction"}]
var callback = function (errorNum, notification) {
console.log('Error is:.....', errorNum);
}
var options = {
gateway: 'gateway.push.apple.com',
//'gateway.sandbox.push.apple.com',
// this URL is different for Apple's Production Servers and changes when you go to production
errorCallback: callback,
cert: __dirname.split('src/')[0] + cert,
key: __dirname.split('src/')[0] + key,
passphrase: passphrase,
port: ****,
cacheLength: 100
}
var apnsConnection = new apn.Connection(options);
apnsConnection.sendNotification(note);
}
else if(devicetype == "android"){
var gcmObject = new gcm.AndroidGcm(androidApiKey);
var message = new gcm.Message({
registration_ids: [devicetoken],
data: {
body: alert,
action: action,
userid: userid,
profilepic: profilepic,
id: id
}
});
gcmObject.send(message, function(err, response) {
if(err) console.error("error: "+err);
// else console.log("response: "+response);
});
}
}
}
Here is my code. In console I'm getting all the stuff and device token is also fine. Android mobiles are getting notifications. But notifications are not sending to ios devices. I'm getting this error in console : Error is:...... 8.
One more thing is, for the same device I'm able to send the notification for other functionality with other code.
Really I'm pulling my hair out for this issue. And can't understand what's wrong with my code. Anyone please give solution for this.
You are using an old version. Apple has changed some thing in the push api last year in march i guess.
Also you forgot to set your topic which is mandetory for apn push Notifications
Try something like this for you if (devicetype == "ios") block
if(devicetype == "ios") {
var myDevice = new apn.Device(devicetoken);
var note = new apn.Notification();
note.badge = 1;
note.sound = "notification-beep.wav";
note.alert = alert;
note.category = "respond"
note.payload = {'action': action, 'userid': userid, 'profilepic': profilepic, 'id': id};
//you missed this one i guess
note.topic = "<your-app-bundle-id>";
console.log("note.payload: " + JSON.stringify(note.payload));
//, 'WatchKit Simulator Actions': [{"title": "Show", "identifier": "showButtonAction"}]
var callback = function(errorNum, notification) {
console.log('Error is:.....', errorNum);
}
var options = {
token: {
key: __dirname.split('src/')[0] + cert,
keyId: __dirname.split('src/')[0] + key,
teamId: "developer-team-id"
},
production: false // for development
};
var apnProvider = new apn.Provider(options);
apnProvider.send(note, myDevice).then( (result) => {
// see documentation for an explanation of result
console.log(result);
});
}
You can find the documentation here apn

Ajax calls working in Android but not iOS

I'm developing an app in Nativescript for the first time and running into an issue where AJAX calls work on Android but not iOS. I have a login.js file which requires a user-view-model (user-view-model.js), and when I test the code on Android it takes me to the "home" page but it hits the catch function on iOS.
login.js:
var dialogsModule = require("ui/dialogs");
var UserViewModel = require("../../shared/view-models/user-view-model");
var applicationSettings = require("application-settings");
var user = new UserViewModel({
email: "aaa#aaa.com",
password: "aaa"
});
var frameModule = require("ui/frame");
var page;
exports.loaded = function(args) {
page = args.object;
page.bindingContext = user;
};
exports.login = function () {
user.login().catch(function(error) {
dialogsModule.alert({
message: "Unfortunately we could not find your account.",
okButtonText: "OK"
});
return Promise.reject();
}).then(function(response) {
console.dir(response)
console.log("past response")
applicationSettings.setString("user_id", response.user_id);
applicationSettings.setString("first_name", response.first_name);
applicationSettings.setString("last_name", response.last_name);
applicationSettings.setString("user_type", response.user_type);
var topmost = frameModule.topmost();
topmost.navigate("views/home/home");
});
};
user-view-model.js:
var config = require("../../shared/config");
var fetchModule = require("fetch");
var observableModule = require("data/observable");
var http = require("http");
function User(info) {
info = info || {};
var viewModel = new observableModule.fromObject({
email: info.email || "",
password: info.password || ""
});
viewModel.login = function() {
let loginEmail = JSON.stringify(this.get("email")).replace(/['"]+/g, '');
let loginPassword = JSON.stringify(this.get("password")).replace(/['"]+/g, '');
console.log(loginEmail, loginPassword);
let loginUrl = config.serverPHPServiceUrl + "Login.php?user_id=" + loginEmail + "&password=" + loginPassword;
console.log(loginUrl);
// I tried this way first and wasn't able to login on iOS, which made me try the second method below.
// return fetchModule.fetch(loginUrl, {
// method: "POST",
// headers: {
// "Content-Type": "application/json"
// }
// }).then(handleErrors).then(function(response) {
// return response.json();
// }).then(function(data) {
// console.dir(data);
// console.log(data["results"][0]["user_id"])
// return data["results"][0];
// });
// This method works on Android but not iOS.
return http.getJSON(loginUrl).then(function(response) {
console.dir(response);
return response.results[0];
})
};
return viewModel;
};
function handleErrors(response) {
console.log("in errors")
if (!response.ok) {
console.log(JSON.stringify(response));
throw Error(response.statusText);
}
return response;
}
module.exports = User;
Is there anything fundamentally wrong with my code, or do asynchronous calls work differently on iOS vs Android in Nativescript? I did the Grocery tutorial and didn't run into this issue, so I didn't think this was the case. Does it matter that the backend is using PHP?
I fixed my issue: I started a new project with Angular 2 and ran into the same error, but then it gave me the error message "Error: The resource could not be loaded because the App Transport Security policy requires the use of a secure connection." I solved it by adding "https" to my url call, but this post has another solution.

Pushsharp 4.0.10 "Connection Error" for IOS device tokens

I developed windows service for sending push notifications to both iOS and Android apps using pushsharp library. Recently I updated the library from 2.x.x to 4.0.10 and GCM notifications are sending fine but Ios notifications are not sending, always getting "Connection Error" And I need to send notifications to thousands of tokens, So I am looping through all the tokens and queuing. Even for 10 tokens also getting same error.
Please suggest me what's wrong with my code. Here is my code snippet
public static void SendNotifications(List currentBrandNotications, long brandId)
{
byte[] appleCert = null;
string p12File = #"aps_production_brand" + brandId.ToString().Trim() + ".p12";
try
{
appleCert = File.ReadAllBytes(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "P12\\" + p12File));
}
catch (Exception ex)
{
logger.Debug("P12 certificate is not avilable for BrandId: " + brandId);
}
try
{
logger.Debug(" Send PushNotifications To Apple :- ");
if (appleCert != null)
{
// Configuration
var config = new ApnsConfiguration(ApnsConfiguration.ApnsServerEnvironment.Production, appleCert, currentBrandNotications[0].P12Password);
// Create a new broker
var apnsBroker = new ApnsServiceBroker(config);
var fbs = new FeedbackService(config);
// Wire up events
apnsBroker.OnNotificationFailed += (Notification, aggregateEx) =>
{
//ScheduledNotification ScheduledNotification = new InstantPNScheduler.ScheduledNotification();
aggregateEx.Handle(ex =>
{
// See what kind of exception it was to further diagnose
if (ex is ApnsNotificationException)
{
var notificationException = (ApnsNotificationException)ex;
// Deal with the failed notification
var apnsNotification = notificationException.Notification;
var statusCode = notificationException.ErrorStatusCode;
logger.Debug("Apple Notification Failed: ID=" + apnsNotification.Identifier + " Code=" + statusCode);
}
else
{
// Inner exception might hold more useful information like an ApnsConnectionException
logger.Debug(ex.InnerException.ToString());
}
// Mark it as handled
return true;
});
};
apnsBroker.OnNotificationSucceeded += (Notification) =>
{
logger.Debug("Apple Notification Sent!");
};
// Start the broker
apnsBroker.Start();
foreach (ScheduledNotification notification in currentBrandNotications)
{
try
{
//logger.Debug("iOS Device token=" + notification.DeviceToken); apnsBroker.QueueNotification(new ApnsNotification
{
DeviceToken = notification.DeviceToken,
Payload = JObject.Parse("{\"aps\":{\"alert\":\"" + notification.Message + "\",\"badge\":1,\"sound\":\"sound.caf\",\"BrandId\":\"" + brandId.ToString() + "\",\"notificationType\":\"Basic\",\"DeviceType\":\"" + notification.DeviceType + "\",\"DeviceToken\":\"" + notification.DeviceToken + "\",\"NotificationId\":\"" + notification.NotificationId + "\"}}")
});
}
Thread.Sleep(800);
}
catch (Exception ex)
{
logger.Debug(" SendPushNotificationToApple :- " + ex.Message);
}
}
// Stop the broker, wait for it to finish
// This isn't done after every message, but after you're
// done with the broker
apnsBroker.Stop();
}
}
catch (Exception ex)
{
logger.Debug("Error" + ex.Message);
}
finally
{
//apnsBroker = null;
}
}
Note : If I put thread.sleep(800) in for loop, then notifications will be sent but it's getting too late in case of thousands of tokens. I need it without thread.sleep(800), even If I reduce below 800ms, getting same exception.
Please help me what's wrong with my code.
Any help would be appreciated.

Why is Xamarin.Auth throwing authentication error with OAuth1Authenticator and Twitter

I am currently using Xamarin.Auth on a iOS project to handle some user authentication via Facebook and Twitter in my application. The Facebook authentication using OAuth2Authenticator works great and my implementation was based mainly off the docs (http://components.xamarin.com/gettingstarted/xamarin.auth). Twitter however still uses OAuth1 it seems and thus I based my implementation mainly off the answer in this StackOverflow questions (https://stackoverflow.com/a/21982205). Everything works properly and I am able to retrieve user, tweets, etc. but after all the code executes I receive a "Authentication Error" popup on the screen saying "Object reference not set to an instance of an object." there is nothing printed to the console however as is the case with most normal errors I have seen thus far. I can dismiss the popup and everything continues to preform correctly. I believe I have narrowed the problem down to something within the OAuth1Authenticator request as I still receive the error when all of the other handling code has been commented out. Please reference the code below to see what might be the cause of this.
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
signupBtn.TouchUpInside += delegate {
LoginToTwitter(true, this);
};
}
void LoginToTwitter(bool allowCancel, UIViewController _vc)
{
var auth = new OAuth1Authenticator (
consumerKey: "My Consumer Key",
consumerSecret: "My Consumer Secret",
requestTokenUrl: new Uri("https://api.twitter.com/oauth/request_token"),
authorizeUrl: new Uri("https://api.twitter.com/oauth/authorize"),
accessTokenUrl: new Uri("https://api.twitter.com/oauth/access_token"),
callbackUrl: new Uri("My callback url"),
getUsernameAsync: (IDictionary<string, string> accountProperties) => {
string screen_name = "";
if (accountProperties.TryGetValue("screen_name", out screen_name)) {
Console.WriteLine("SN: {0}", screen_name);
Account a = new Account(screen_name, accountProperties);
AuthenticatorCompletedEventArgs e = new AuthenticatorCompletedEventArgs(a);
TwitterCompleted(e, _vc);
}
return null;}
);
auth.AllowCancel = allowCancel;
UIViewController authView = auth.GetUI ();
_vc.PresentViewController (authView, true, null);
}
void TwitterCompleted (AuthenticatorCompletedEventArgs e, UIViewController _vc)
{
var theAccount = e.Account;
var theProperties = theAccount.Properties;
foreach (var item in theProperties) {
Console.WriteLine (item); //debugging
}
InvokeOnMainThread (delegate {
_vc.DismissViewController (true, null);
});
AccountStore.Create ().Save (e.Account, "Twitter");
if (!e.IsAuthenticated) {
Console.WriteLine("Not authorized");
return;
}
theScreenName = e.Account.Properties["screen_name"];
theCount = "2";
IDictionary<string, string> theDict = new Dictionary<string, string>();;
theDict.Add("screen_name", theScreenName);
theDict.Add("count", theCount);
var request = new OAuth1Request("GET", new Uri("https://api.twitter.com/1.1/statuses/user_timeline.json"), theDict, e.Account, false);
request.GetResponseAsync().ContinueWith (t => {
if (t.IsFaulted)
Console.WriteLine("Error: {0}", t.Exception.InnerException.Message);
else if (t.IsCanceled)
Console.WriteLine("Canceled");
else
{
var obj = JsonValue.Parse (t.Result.GetResponseText());
Console.WriteLine("object: {0}", obj); // debugging
}
}, uiScheduler);
return;
}
private readonly TaskScheduler uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
instead of returning null in "getUsernameAsync" return Task

Resources