Storekit - Configure a subscription status URL using Firebase functions - firebase

I'm trying to use Firebase functions as our server. Here's the function I'm using. It works fine when I trigger it over HTTP
exports.subscriptions = functions.https.onRequest((request, response) => {
// Send 200 code to the server indicate to that you are done!
response.send(200);
});
Apple Says:
The App Store will deliver JSON objects via an HTTP POST to your
server for the key subscription
But so far I didn't get any notification from Apple server.
Any suggestions would be great?
I checked the logs in the console and it seems that I have started receiving notifications from Apple server.

well you're only saying to apple that you're done, if you add console.log(req.body) to your function it should give you some data in the logs :)

Related

Use firebase functions with Apple Pay

I want to integrate with Apple Pay on my website using the Apple Pay JS API, but my call to Apple is failing.
We are using Firebase Functions to run our server, and Firebase Hosting to host our website.
To do this, I need to request an Apple Pay Payments Session as described in https://developer.apple.com/documentation/apple_pay_on_the_web/apple_pay_js_api/requesting_an_apple_pay_payment_session.
The way this seems to work is that I start a payments session client side on my website and configure this to call an endpoint on my server. This then calls Apple's servers with a merchant identifier certificate I've created, and I get the payments session back and return it to my website. I'm following these documentation pages:
https://developer.apple.com/documentation/apple_pay_on_the_web/apple_pay_js_api/providing_merchant_validation
https://developer.apple.com/documentation/apple_pay_on_the_web/apple_pay_js_api/requesting_an_apple_pay_payment_session
I am calling Apple like this:
const { validationUrl } = req.body
const axiosInstance = axios.create({
httpsAgent: new https.Agent({
cert: fs.readFileSync(`${__dirname}/merchantIdentity.crt.pem`),
key: fs.readFileSync(`${__dirname}/merchantIdentity.key.pem`),
rejectUnauthorized: false
})
})
const appleUrl = `https://${validationUrl}/paymentSession`
const validateMerchantData = {
merchantIdentifier: <my merchant id>,
displayName: "Mercado",
initiative: "web",
initiativeContext: process.env.CLIENT_URL.replace("https://", "")
}
const appleRes = await axiosInstance.post(appleUrl, validateMerchantData)
CLIENT_URL is the base URL of my client website
I am getting the following error at this point:
Error: getaddrinfo EAI_AGAIN https
at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:71:26)
at GetAddrInfoReqWrap.callbackTrampoline (node:internal/async_hooks:130:17)
In https://developer.apple.com/documentation/apple_pay_on_the_web/configuring_your_environment, the documentation says "You must register and verify all top-level domains and subdomains where you will display the Apple Pay button." It's not clear to me whether this means the website domain (the hosting one) or our functions domain. I have successfully registered and verified our hosting domain, but I couldn't manage to do this with the functions domain, which is the one I'm actually using to call Apple (obviously wouldn't do this from the client, and Apple specifically says not to do this.
This is because Apple calls https://<domain_name>/.well-known/some_file.txt to do the verification, but firebase functions are always of the form https://-<project_id>.cloudfunctions.net/<function_name>, so it's impossible to have an endpoint for /.well-known. My solution has been to use rewrites on my Firebase Hosting URL to call my applePay firebase function.
What could be causing the error, and how could I resolve?
PS: not interested in any answers suggesting to use something like Stripe for Apple Pay.

Firebase messaging failing on flutter after some time

I'm having an issue with FCM on flutter. I have implemented messaging from my server so I'm storing my phone token for each user.
The thing is that when a user logs in for the very first time everything works properly, messages are being sent and user gets notified.
If I do not use the app during the weekend, on Monday I try to send a message by doing some actions on my app but messages are not being sent. I can see my token stored properly in my database.
I'm using firebase_messaging 2.1.0 for flutter.
This is how I get my token
_fireBaseMessaging.getToken().then((token){
_myPhoneToken = token;
});
1-I know token may change when:
App deletes Instance ID
App is restored on a new device
User uninstalls/reinstall the app
User clears app data
But none of this happens.
Any advice on how to handle this scenario? thanks in advance.
UPDATE
Provided you have setup the FCM sdk the right way (but you said that it works the fist time you install the app, so I guess so).
Provided that you are sure that the device_token you are using is the one of the device on which you are expecting to receive the notification (check if it's still the same), you should get on this device your notification quite soon if you use "priority" : "high".
{
"to" : "device_token",
"priority" : "high",
"notification" : {
"sound": "default",
"body" : "Test Notification body",
"title": "Test Notification title"
}
}
This method call
_firebaseMessaging.getToken().then((String token)
return always the new token even if it has been updated. So if you print this out on your device and you send a notification on this token without error, there's no reason why you should not get the token if the device has a valid internet connection active.
It's true that the device token can change during time. If you uninstall and reinstall the app, you can see the token will change and if you try to send a notification on the old one, you will get an error.
If instead the token will change during application lifetime, you can be notify on your server side by listening:
_firebaseMessaging.onTokenRefresh.listen((newToken) {
_fcm_token = newToken;
// send the new fcm to your server
});
So first of all I suggest you to be able to send a notification to a device with Postman. Check if the token you are using is still the one on the device. Then you can try to uninstall and reinstall the application and try to use the old token. You will get an error. Then try to send to the new one, and you should get your notification.
Then wait for some days and try again, check if the token has changed or not and if it's not changed you should be able to send the notification without problems with the same token.
Also be aware that data message on Android if the app is terminated are still not supported.
Some networks/router/mobile can cut the connection between firebase library and firebase server due to inactivity (5min without message). This cut may be detected by the library up to 30min (FCM heatbeat interval).
These are some links discussing this issue:
https://github.com/firebase/quickstart-android/issues/307
Android: Delay in Receiving message in FCM(onMessageReceived)
I contacted firebase support but they told that since the issue is caused by external part they cannot fix it (I suggest decreasing heartbeat interval ...)
I fixed it in android using an interval job which apply these instructions:
context.sendBroadcast(new Intent("com.google.android.intent.action.GTALK_HEARTBEAT"));
context.sendBroadcast(new Intent("com.google.android.intent.action.MCS_HEARTBEAT"));
You may write this specific code for Android side and should find something similar for ios side.

iOS FCM data only message does not call messaging:didReceiveMessage

Firebase Messaging version 5.6.0. I am attempting to handle a data only message in the foreground via Firebase Messaging on iOS 9.0 (10 if needed), but it is not calling FIRMessagingDelegate's messaging:didReceiveMessage per the documentation. I see the message come in # FIRMessaging.m's appDidReceiveMessage:message, but never comes through to the delegate.
This is the snippet from the cloud function that sends data to the topic per sending to a topic:
const message = {
data: {
test: '123'
}
topic: 'example'
}
admin.messaging().send(message);
Did I miss something?
Update: I do receive the data if I implement application:didReceiveRemoteNotification:userInfo
fetchCompletionHandler:completionHandler.
Thanks to Kat at Firebase support, here is the answer.
Use legacy sendToTopic instead of send, as send quietly adds content_available=1 which gets treated as APNs silent notification. Here is the updated version:
admin.messaging().sendToTopic('example', {
data: {
test: '123'
}
});
// Always use strings for key/values in the data object.
Below verbatim from Kat at Firebase support:
How the FCM data message is handled would depend on your setting for content_available.
If you have content_available = 1, the message is sent via APNs and is treated similar as an APNs silent notification. This is handled in the application:didReceiveRemoteNotification: callback when the app is running in foreground or background (i.e. not killed). See this related StackOverflow post for more information.
Without content_available, the message is sent via FCM direct channel. This only handled in the messaging:didReceiveMessage: when app is in foreground.
Note that messages sent via the Admin SDK's send() method uses the FCM HTTP v1 API which have content_available=1 by default, so they are always sent via APNs. If you want to configure the content_available field, you'll need to use the Admin SDK's sendToDevice() method which uses the legacy protocols.
In addition, here is the list of legacy protocols.

send grid & parse 502 Bad Gateway with nginx

I am trying to migrate my parse application over to digital ocean and followed this guide :
https://www.digitalocean.com/community/tutorials/how-to-migrate-a-parse-app-to-parse-server-on-ubuntu-14-04
Everything works perfectly fine until I get to the very end Test Parse Server ( Executing Example Cloud Code ) section
I tested the cloud code for the sample cloud code that was provided in the tutorial :
Parse.Cloud.define('hello', function(req, res) {
res.success('Hi');
});
so I got a Hi back in my browser as well as in postman.
See image here : https://cloudup.com/cH2dbBx1KTo

Then I test the function that uses sendgrid's service to send emails (http://blog.parse.com/announcements/introducing-the-sendgrid-cloud-module/), my cloud code file looks like this :
see image : https://cloudup.com/cD6MNRP3Tft
and now I try to run my post request from postman and I get an error even on my hello function that was working before
See image : https://cloudup.com/cIkwJ6552_5
So I look around and figure out that its an issue with my sendgrid import
var sendgrid = require("sendgrid");
sendgrid.initialize(“xxxxxx”, “xxxxx.”);
in these lines.
does anyone have any experience with digital ocean cloud code and send grid emailing service please help me out I will be grateful as this is the last step left and I will be done with my migration :)
cheers
Tanzeel
you have to specify server URL in parse config file. It is required and could be the reason why you cant run cloud code.
"PARSE_SERVER_URL": "http://localhost:1337/parse"
The url has be the same what you are using. There is also error in Nginx config in that tutorial, I explained it here https://serverfault.com/questions/765627/cannot-post-get-over-ssl/766428#766428
So I looked up at pm2 and to see real-time logs the command is
pm2 logs
at first when I ran the command I saw some errors, maybe they were there from before :
Then I tried the hello cloud function from postman app to test for its output in pm2 logs and I got the following :
Next I try to run my sendMail sendgrid function and I find out the the api-key I had used in my sendgrid function was throwing an error
ReferenceError: XXXXXXXXXXXX is not defined
So I went back to my cloud code and used quotes around my api-key parameter and passed it as a string in my send grid initialize function. Then I retry and get
[Error: The provided authorization grant is invalid, expired, or revoked]
So I went back to my sendgrid account and made sure that the api-key I was using was the correct one and it seemed to be just fine. I tested again and got the same error again so I decided to generate a new api-key just in case.
So I realize that I was not using the api-key but instead API KEY ID :
When we create a new api-key on sendgrid they give us the actual api key once and they ask us to store it in some secure place :
We can only display the key above one time. Please store it somewhere safe because as soon as you navigate away from this page, we will not be able to retrieve or restore this generated token.
So after I used an actual api-key I was able to send emails 😃
But one small issue still remains and I am not sure if its because of postman that I am using to run cloud code or something in the parse server or nginx that is still returning me with a 502 Bad Gateway as a response
But when I look at the logs for my parse server I do see a
parse-wrapper-0 { message: 'success' }
but it never gets back to me in my postman and instead I am getting a 502 error not sure why but the emails are being sent succesfully :)

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