Unable to connect to live 'ssl://gateway.push.apple.com:2195' - push-notification

I am facing issue with APNS php code for push notification on IOS devices, I have two separate connection for Development and Production.
I have configure the development connection on my server by adding the .pem file certificate and Passphares its working perfect and I received the notification also. Have a look my development configuration:
Url: 'ssl://gateway.sandbox.push.apple.com:2195'
$push = new ApnsPHP_Push(
ApnsPHP_Abstract::ENVIRONMENT_SANDBOX,
'APNS_Dev_ISAS.pem'
);
$myNewLogger = new MyNewLogger();
$push->setLogger($myNewLogger);
// Set the Provider Certificate passphrase
$push->setProviderCertificatePassphrase('1234567');
$push->setRootCertificationAuthority('APNS_Dev_ISAS.pem');
$push->connect();
Issue:
Than I configured the connection for Production by adding following parameters but I getting the connection error:
Url: ssl://gateway.push.apple.com:2195
$push = new ApnsPHP_Push(
ApnsPHP_Abstract::ENVIRONMENT_PRODUCTION,
'APNS_PROD_ISAS.pem'
);
$myNewLogger = new MyNewLogger();
$push->setLogger($myNewLogger);
// Set the Provider Certificate passphrase
$push->setProviderCertificatePassphrase('12345678');
$push->setRootCertificationAuthority('APNS_PROD_ISAS.pem');
$push->connect();
Error of connection:
INFO: Trying ssl://gateway.push.apple.com:2195...ERROR: Unable to connect to 'ssl://gateway.push.apple.com:2195': (0)
INFO: Retry to connect (1/3)...INFO: Trying ssl://gateway.push.apple.com:2195...ERROR: Unable to connect to 'ssl://gateway.push.apple.com:2195': (0)
INFO: Retry to connect (2/3)...
INFO: Trying ssl://gateway.push.apple.com:2195...ERROR: Unable to connect to 'ssl://gateway.push.apple.com:2195': (0)
INFO: Retry to connect (3/3)...
INFO: Trying ssl://gateway.push.apple.com:2195...ERROR: Unable to connect to 'ssl://gateway.push.apple.com:2195': (0)
I google the issue and I found the some solutions and I have check all and everything is fine but no success.
I have used the correct path for development and production.
I have created the separate certificate .pem files for both and tested the certificate on pusher app. Certificate are correct.
Port is also fine and no blocking from my server because same port is used in development url and development server push notification working fine.
Any help will be appreciated really. Thanks in advance.

Certificate (.pem) having issue that I was created for push notification.
Solution:
After few days trying on same issue I found that create certificate with mini character passpharess may be 1234, It will work perfect for you and make successful connection to IOS push notification server.
May be this will help someone else.
Thanks.

I was getting same problem when I execute my PHP script. After some research, I commented these three key-pair values 'cafile', 'CN_match' and 'ciphers'.
Then its started working properly. I hope this reply become useful to any other person as well.
$contextOptions = array(
'ssl' => array(
'verify_peer' => false, // You could skip all of the trouble by changing this to false, but it's WAY uncool for security reasons.
// 'cafile' => 'NiteVisionWebPushFile.pem',
// 'CN_match' => 'gateway.push.apple.com', // Change this to your certificates Common Name (or just comment this line out if not needed)
// 'ciphers' => 'HIGH:!SSLv2:!SSLv3',
'disable_compression' => true,
));

Related

React-Native fetch call to localhost endpoint is giving Type Error: Network request failed when using https

I have a react-native application running on expo client using npm run android on Android Studio Emulator, WebApi call to get data using fetch works perfectly fine when I disable Enable SSL on the Visual Studio project settings see below, which means it is running on http port 54715, if you see arrow mark I unchecked Enable SSL checkbox
And also I made changes in applicationhost.config file to point my localhost to 127.0.0.1 which is an alias for 10.0.2.2 to connect from Android emulator, see below:
<bindings>
<binding protocol="http" bindingInformation="*:54715:127.0.0.1" />
<binding protocol="http" bindingInformation="*:54715:localhost" />
<binding protocol="https" bindingInformation="*:44367:127.0.0.1" />
</bindings>
So from above if I make fetch call from react native app it is working just fine, see code below:
fetch("http://10.0.2.2:54715/WeatherForecast/getAllProgramTypes")
.then((response) => {
return response.json();
})
.then((data) => {
console.log(data);
})
.catch((err) => {
console.log(err);
});
But the moment I click on EnableSSL on the project properties it would run on https port which is self certified
https://10.0.2.2:44367/WeatherForecast/getAllProgramTypes
https run on 44367 port, and when I make fetch to the web api:
fetch("https://10.0.2.2:44367/WeatherForecast/getAllProgramTypes")
.then((response) => {
return response.json();
})
.then((data) => {
console.log(data);
})
.catch((err) => {
console.log(err);
});
now with https I am getting TypeError: Network request failed, it is unable to make https call on localhost.
I got it working with http when I un-check Enable SSL and run the .Net Core solution but how to make fetch call with local https work ?
When I look at Microsoft docs it says Bypass the certificate security check, how do i do it with react-native code ?
https://learn.microsoft.com/en-us/xamarin/cross-platform/deploy-test/connect-to-local-web-services#bypass-the-certificate-security-check
Ciao unfortunately seems that is not possible to bypass the certificate security check in Javascript.
Possible solution:
Add the self-signed certificate to your root certificate repository on your local machine.
Obtain a valid signed certificate from a free service such as Let's Encrypt.
So, http or https even if it's working on Android Emulator, the question again is how it will work on your iPhone device or an Android device, I mean if you are using Expo Client app to open you react native app, now how do you connect your localhost .NET Core WebApi or any other endpoints on your local machine to your mobile devices to have the app running.
I have a common working solution which works for both emulators and mobile apps on the individual devices.
Step 1: Go to https://ngrok.com/
Step 2: Sign Up, you may use GitHub account
Step 3: Click on Download for windows if you have windows OS and unzip the file, you will
see the .exe, copy that to your desktop or the location you wanted.
Step 4: Go windows explorer and open command prompt on the location where you copied the
exe, run dir to check to see if you have ngrok.exe in the location you are on.
Step 5: Run ngrok authtoken yourTokenGivenDuringSignUp , it’s one-time thing you may need
to do
Step 6: Run ngrok http https://localhost:PortNumber -host-header="localhost:PortNumber" , Port number is the port that your .NET core solution is running, Enable SSL in the project properties we need https for this to work.
Step 7: Copy https link which will be ngrok created https URL which is highlighted, given as below example
Now you can use above link to replace in the fetch url to make the mobile app or Emulators work from your local code.
Note*** We need to be running the ngrok on step 6 & our solution, also url is different every time so run the step 6 and .NET Core solution to generate new web address and use it in the react native app.
Happy Coding :)

How do you initialize the Twilio Client in Meteor JS?

I'm having incredible difficulty setting up the Twilio Client in Meteor JS, and would really appreciate any help.
I have extracted the relevant code and error logs below. So far as I can tell, it should be simple. The code is just grabbing an authtoken which I have previously generated, and then trying to set up the device using that authtoken. But it's not working.
'click #initializeDevice'(event) {
var thisAuthToken = Session.get('myAuthToken');
console.log(thisAuthToken); // I have confirmed with Twilio support that these authtokens are correctly generated
const Device = require('twilio-client').Device;
Device.setup(thisAuthToken, { debug: true });
var myStatus = Device.status()
console.log(myStatus); //this is logging "offline"
Device.on('ready',function (device) {
log('Twilio.Device Ready!'); //this is not logging anything
});
},
When that code runs, it generates the following logs:
eyJhbGciDpvdXRnb2luZz9hcHBTaWQ9QVA2NDE2MzJmMzA1ZjJiY2I[Note:I have deleted part of the middle of the logged authtoken for the purpose of this public post]5YmMxOGQyOWVlNGU2ZGM0NjdmMzRiNDVhNCIsImV4cCI6MTU3Nz0ygbJKTx15GgNCWDkm-iUPjn_O1NZU6yovp4vjE
modules.js?hash=69069bec9aeba9503ae3467590cf182be57d9e62:3605 Setting up VSP
modules.js?hash=69069bec9aeba9503ae3467590cf182be57d9e62:3605 WSTransport.open() called...
modules.js?hash=69069bec9aeba9503ae3467590cf182be57d9e62:3605 Attempting to connect...
modules.js?hash=69069bec9aeba9503ae3467590cf182be57d9e62:3605 Closing and cleaning up WebSocket...
modules.js?hash=69069bec9aeba9503ae3467590cf182be57d9e62:3605 No WebSocket to clean up.
modules.js?hash=69069bec9aeba9503ae3467590cf182be57d9e62:3605 Could not connect to endpoint: ws does not work in the browser. Browser clients must use the native WebSocket object
modules.js?hash=69069bec9aeba9503ae3467590cf182be57d9e62:3605 Closing and cleaning up WebSocket...
modules.js?hash=69069bec9aeba9503ae3467590cf182be57d9e62:3605 No WebSocket to clean up.
calltemplate.js:31 offline
I'm doing this all from a local server, tunneled through NGROK. I've also set up the Twilio back end, linked the app, purchased a number, etc.
So far as I can tell, the issue, from the logs, appears to be something to do with the way that Meteor uses WebSockets.
Could not connect to endpoint: ws does not work in the browser. Browser clients must use the native WebSocket object
This is a not a Meteor related problem rather than browser issue.
Make sure your browser supports WebRTC
BTW, Your browser might be supporting it but you'd need to enable it.

How to use Firebase behind Firewall / Proxy?

We are running a simple application that connects to Firebase are reads some data. It fails to connect with the following timeout error:
#firebase/database: FIREBASE WARNING: {"code":"app/invalid-credential",
"message":"Credential implementation provided to initializeApp()
via the \"credential\" property failed to fetch a valid Google OAuth2 access token
with the following error: \"Failed to parse access token response: Error: Error
while making request: connect ETIMEDOUT
We are behind Firewall / Proxy and it appears that is blocking traffic to/from Firebase and hence failed connection. My question is what ports need to be opened and to what destination URLs to make this application work normally?
Any help will be much appreciated!
Finally, after struggling with the issue for several days got it working. Needed to contact network team and request to perform following actions:
Open ports 5228, 5229, 5230 for Firebase communication.
Opened communication at proxy level between the source server and following URLs:
fcm.googleapis.com
gcm-http.googleapis.com
accounts.google.com
{project-name}.firebaseio.com
Added following code in my node.js application:
var globalTunnel = require('global-tunnel-ng');
globalTunnel.initialize({
host: '<proxy-url>',
port: <proxy-port>,
//proxyAuth: 'userId:password', // optional authentication
sockets: 50 // optional pool size for each http and https
});
Installed module global-tunnel-ng:
npm install global-tunnel-ng
It solved the my problem and I hope it can help others too. :-)
I used Wireshark to monitor a local install of a Node.js application using the Admin SDK for firestore. I also referenced this list by Netify. This is what I found:
*.firebaseio.com
*.google.com
*.google-analytics.com
*.googleapis.com
*.firebase.com
*.firebaseapp.com

FCM getting MismatchSenderId

I have an application that uses Google FCM for sending push notifications.
When i send a push notification to a group of users, i get a response of MismatchSenderId for some of them. Even though, all users have the exact same application. How can some of the users get a success response and others get a MismatchSenderId?
I have researched a lot and made sure I have added all prerequisites that FCM needs.
Any suggestions?
EDIT:
Sample response:
{"multicast_id":5340432438815499122,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"MismatchSenderId"}]}
EDIT 2:
Here is the server side sending code (PHP):
$fields = array
(
'to' => $token,
'data' => $data
);
$headers = array
(
'Authorization: key=AIza**************************',
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, true );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode($fields) );
curl_exec( $ch );
curl_close( $ch );
UPDATE:
It seems that the issue has been resolved with the SDK updates. I am using now the latest com.google.firebase:firebase-messaging:9.6.1, I don't get "MismatchSenderId" anymore.
Firebase has upgraded their server keys to new version.
Use new keys instead of old one.
go to settings->project settings->cloud messaging tab
I found this solution:
First I check server key is correct or not it was correct which is
like AIzaXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Then I check Sender Id like 79XXXXXXXX it was also correct.
Main issue was in device_ID(UDID) to whom I have to send the notification. Actually DeviceId we got on android side in FCM is different than GCM. You can't use GCM created DeviceId in FCM.
Just an FYI. I was running into this error, even though I swear the android app I was testing was built with the latest/greatest google-services.json file and I could send from the FCM console to the app.
I rebuilt the app after doing a Clean Project and now I can send to the app with the FCM token it registers. So, maybe try a clean rebuild before beating your head against the wall for too long.
THIS WORKED FOR ME:
CHECK WHICH FIREBASE PROJECT YOUR CURRENT ANDROID PROJECT IS LINKED TO AND USE
Server Key FROM THAT PROJECT.
I was using Server key from a different firebase project(say Project 2). I used the same server key as linked with my android project(Say Project 1) and it worked. In my experience, using one firebase app for one entire android project seemed to fix the problem.
In my case, I just was mistaking about project credentials: using google-services.json from one project, and server key from another.
We spent hours and hours to resolve this issue. What we found that token was being generated using different FCM account settings (google-services.json) and notification was being set using different FCM account.
When we synced both it worked like a charm.
My problem was solved by entering the correct key as shown below.
Copy Server key like this picture
And then copy this code in post man like this picture
and Body is row like this picture
NOTE:
You must add key= before Server key in header
Make sure you enter the header keys correctly (Authorization =
"key=..." and ContentType = "application/json")
You have sent the request to the correct address
(https://fcm.googleapis.com/fcm/send and Post method)
I cross verified all the credentials
Added new server key and deleted old one
Downloaded google-services.json again
Nothing solved the issue
Then I just clean the project and then rebuild the project.After that it's working fine
I found that the senderId is different from the project number in the FCM console
so I re-downloaded google-services.json and everything works fine
Make sure you remove all the Parse code from your app (libraries, receivers in AndroidManifest, etc). I found that intermittently push notifications were not working. From my observations, it would work on fresh installs but not app updates from the play store.
I believe something internal to Parse was conflicting with FCM (Parse uses GCM, so I'm guessing it had to do with using GCM and FCM simultaneously). As soon as I ripped out all the Parse receivers and libraries, things worked great.
Actually there are many reasons for this issue.
Mine was because of Invalid token passed.
I was passing same token generated from one app and using same token in another app.
Once token updated , it work for me.
I spent hours on this and finally figured it out. This problem happens if service account your sender application is using differs from the service account your receiver is using.
You can find out your receiver service account via Firebase -> Project Overview -> Project Settings -> Service Accounts and generate a new key and use that key when you are initializing your FirebaseApp in the sender:
FileInputStream serviceAccount = new FileInputStream("YOUR_PATH_TO_GENERATED_KEY.json");
GoogleCredentials googleCredentials = GoogleCredentials.fromStream(serviceAccount);
FirebaseOptions options = new FirebaseOptions.Builder().setCredentials(googleCredentials).build();
firebaseApp = FirebaseApp.initializeApp(options);
This initialization need to be done once before you send push notifications.
In my case, I had done everything correctly and I still had this problem. I had made some changes in the "google-services.json" used in the receiver app and I noticed AndroidStudio was not using my new file. The solution was so simple:
AndroidStudio -> Build -> Clean Project and Build -> Rebuild Project
For eliminate this error for mismatch sender Id In fcm
{"multicast_id":7751536172966571167,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"MismatchSenderId"}]}
there is error occured,
So we required first, In FCM, register Application e.g MyDemoApplication
After that FCM will generate server key i.e AIzaSyB9krC8mLHzO_TtECb5qg7NDZPxeG03jHU and sender Id i.e 346252831806 these format like these.
After In Android Studio, our project is connect to these FCM created project i.e MyDemoApplication
And most important step is there required to device token or registration id. These token must created in Android Studio..
After that using Sender Id and API key in web API project u will be definitely getting notification
I had the same issue in my react-native - node.js project.
I wanted to send notifications in android.
Everything was set-up and working fine (i.e. I was able to send notifications from node.js and receive notifications on android device).
After a few days, I had to use a different firebase account, so I changed the google-services.json file in my project's android/app folder and rebuilt the project. But, when I tried sending notification from my server once again, I got an error -
{.............
errorInfo: {
code: 'messaging/mismatched-credential',
message: 'SenderId mismatch'
},
codePrefix: 'messaging'
}
Solution:
the XML file at the location -
app/build/generated/res/google-services/{build_type}/values/values.xml was not getting automatically updated according to new google-services.json.
It still consisted of old values from my previous google-services.json file. I had to change values.xml file manually.
This is how app/build/generated/res/google-services/{build_type}/values/values.xml file look (You need to change it manually if it does not get updated automatically according to google-services.json)-
<?xml version="1.0" encoding="utf-8"?>
<resources>
<! -- Present in all applications -->
<string name="google_app_id" translatable="false">1:1035469437089:android:73a4fb8297b2cd4f</string>
<! -- Present in applications with the appropriate services configured -->
<string name="gcm_defaultSenderId" translatable="false">1035469437089</string>
<string name="default_web_client_id" translatable="false">337894902146-e4uksm38sne0bqrj6uvkbo4oiu4hvigl.apps.googleusercontent.com</string>
<string name="ga_trackingId" translatable="false">UA-65557217-3</string>
<string name="firebase_database_url" translatable="false">https://example-url.firebaseio.com</string>
<string name="google_api_key" translatable="false">AIzbSyCILMsOuUKwN3qhtxrPq7FFemDJUAXTyZ8</string>
<string name="google_crash_reporting_api_key" translatable="false">AIzbSyCILMsOuUKwN3qhtxrPq7FFemDJUAXTyZ8</string>
<string name="project_id" translatable="false">mydemoapp</string>
</resources>
You can refer to these links:
https://firebase.google.com/docs/projects/multiprojects
Processing the JSON file: https://developers.google.com/android/guides/google-services-plugin#processing_the_json_file.
I got this Error after I added more Product Flavours to my current App,
it seems like it uses the package name to generate the FCM Token, so to reslove the issue I have to add an app for eatch Flavour with it's corresponding PackageName, and update the google-services.json file with the latest generated one wich include Client info for each Flavour(Package Name).
check gcm_sender_id in manifest.json
If mismatch - correct, but you need make new subscribers for new sender Id.
Exists subscribers break.
As per response "MismatchSenderId", this is a mismatch between FireBase and your local "google-services.json", you have to make sure that both manifests matches
In FireBase console you go to "YourProject > Project OverView > Cloud Messaging" you'll see the "SenderID" which MUST match with the "google-services.json" in your Android project.
Best thing you can do is download the final json file from "General" tab and place it in your project.
I had the same error while trying to send push notificaion. Get the updated google-services.json file and replaced with it. Worked for me.
I was trying to send notification using the fcm api "https://fcm.googleapis.com/fcm/send" from postman. Server key was ok and token was pasted fine but was still getting error "MismatchSenderId".
Then introduced the follwoing dependency in the gradle file on android side.
implementation platform('com.google.firebase:firebase-bom:25.12.0')
and it started receiving notifications on the device
Make sure that your "package_name" in the google-service.json file is your applicationId from build.gradle(app) file.
"client_info": {
"mobilesdk_app_id": "",
"android_client_info": {
"package_name": "" // -> your applicationId
}
}
I found the reason for this problem after 5 years of using Firebase.
Of course, this happened to me when I tried to get a new google-services.json file for old projects.
When you receive the new file, you must use the latest version of google-services.
classpath 'com.google.gms:google-services:$.$.$'
Updating firebase initialization works for me..
<script src="https://www.gstatic.com/firebasejs/4.6.2/firebase.js"></script>
<script>
// Initialize Firebase
// TODO: Replace with your project's customized code snippet
var config = {
apiKey: "<API_KEY>",
authDomain: "<PROJECT_ID>.firebaseapp.com",
databaseURL: "https://<DATABASE_NAME>.firebaseio.com",
storageBucket: "<BUCKET>.appspot.com",
messagingSenderId: "<SENDER_ID>",
};
firebase.initializeApp(config);
</script>
For me the problem was that I was using the phonegap-plugin-push in cordova, testing the app with the phonegap app.
The problem with this is that for some reason with this plugin, the phonegap app intercepts it and returns a dummy registration key, no matter what sender ID you have.
So to make it work (as long as you have all your keys right) is to test your program some other way, with an emulator, or android emulation via usb. And your keys will match.
Hopefully this saves someone some time.
your probably doing whwat i was doing. GCM token is not the same as FCM anymore. check if your pulling the GCM token ID instead of FCM. just override FirebaseInstanceIdService & ensure your getting the right ID. i thought at one point they were the same but now there not. i logged a test and saw. after you do that also update google-services.json like others say as a safety.
I wasted days on this.
In my case, I followed this blog to use Postman and get the mismatchsenderid error. Previously, I was getting messaging/mismatched-credential, and people reported that their client app used multiple projects.
However, for me, I have one-to-one, a web app and a project. BUT, I host all apps via ng serve on default port 4200. So regardless of what project / app pair I was developing on, I would always get the same token from my client web app using AngularFireMessaging.requestToken - this token was always the token created on my first ever run of a firebase app in development.
As a work around, I start different apps / projects on different ports and get proper tokens for respective projects.
Note: I had truncated the first part of the FCM token (before :) and the error was MismatchSenderId
Fixed my script and now everything works fine.
In my case someone had deleted APN SSL Certificates for my app in Apple development portal.
I needed to create new certificates: Identifiers / My AppId / Push Notifications / Edit.
Then I uploaded them to the Firebase project from console: Project Settings / Cloud Messaging / iOS app configuration / APNs Certificates.
This solved the problem.
In my case it was very simple. I was pulling the wrong registrationId from the database. After I pulled the correct Id, it worked.
I had big trouble figuring out why I was getting a "MismatchSenderId" status.
I added the gms dependency in the root build.gradle but my error was actually not applying the gms plugin in the app build.gradle.
If you have don't have this line in the app build.gradle, this could be the reason why the notification are not working:
apply plugin: 'com.google.gms.google-services'
I have noticed that when a device id is created in GCM you cannot send push messages through FCM using the new server key, you have to use the old API-key.

iOS Push Notifications error after sending wrong token

I've been implementing a small PHP script which will send Push Notifications to Apple's APN servers.
I've got a database with all device tokens that registered for push notifications within my App.
My script will query the database for the tokens and send them to Apple.
Everything works great with a big but... I manually inserted a wrong token into my table and executed the script again.
I've realized that after that wrong token is sent to Apple, all notifications after that won't reach the devices.
Apple seems not to return any OK if everything worked fine but it does send a KO code if something went wrong. I get an error from Apple for that wrong token but I don't get any response afterwards for all the other notifications.
I open only one connection for all the tokens inside the database.
I prepare a socket context with this line:
$streamContext = stream_context_create(array(
  'ssl' => array(
   'local_cert' => $this->signFile
  )
 ));
Then start a communication channel with the APN servers:
$this->hFile = stream_socket_client(self::$serverUrl[$environment], $err, $errstr, 60, STREAM_CLIENT_CONNECT, $streamContext);
Finally I send the message:
fwrite($this->hFile, $command);
I thought of setting a new connection for every notification but I wanted to ask SO opinion first...
By the way, I know that PHP isn't the best choice for this but it came as a requirement from somewhere else and we are forced to set the system this way.
Thank you and have a nice day,
Alex.
I finally found a solution:
As soon as I detect that a token is invalid, I close the connection and open it again to start sending tokens right after the wrong one.

Resources