Do I need APNs certificates if I'm using APNs Auth Key? - ios

I'm writing an cross-platform app with Flutter and Firebase. I've been working on sending notifications, and it works perfectly on android. I send messages via the firebase admin functions sdk with no problems. The request looks like this:
const payload = {
notification: {
title: title,
body: body,
},
}
return admin.messaging().sendToDevice(tokens, payload, {
mutableContent: true,
contentAvailable: true,
apnsPushType: "background",
})
But when I try to send messages to iOS devices, I get the following error from the shell:
"error": {
"errorInfo": {
"code": "messaging/third-party-auth-error",
"message": "A message targeted to an iOS device could not be sent
the required APNs SSL certificate was not uploaded or
has expired. Check the validity of your development and
production certificates."
},
"codePrefix": "messaging"
}
I was under the impression that since I'm using a APNs key generated in the apple developer console, that I would not need any sort of SSL certificate. My key is uploaded to the Firebase console and iOS devices are successfully reporting their device tokens, so I'm not sure why I can't get a message through.
I've tried sending messages from the CLI and also from the FCM console online. Both have failed.
I've also enabled Push Notifications and Background Modes with background fetch and remote notifications.
Any help or suggestions would be appreciated.

This is really stupid, but the issue was an incorrect Bundle ID in firebase. When I first hooked up firebase it gave me a default bundle id (com.firebase.io or something dumb like that). Later when I actually put my app on testflight i called it something else entirely. So even though I followed all the directions on the FlutterFire website for setting up apple here it didn't work bc the bundle ids didn't match.
So I had to delete the firebase app, replace the googleservicesinfo.plist file (thought it might have been sufficient to just change the bundle id field in that file (idk for sure tho)) and then replaced it in the firbase console. Had to reupload the auth key too with the "new" app.
Hope that makes sense. Comment if you have any other questions!

Related

Not all subscribed iOS devices to topic receive data notification, most often none at all. Notification without data works as expected

My app is implemented in React Native and is using Firebase for notifications. We have webhooks on the web app that are triggered on certain events. Those webhooks then send a request to the respective Firebase Cloud Function. With information from the request's header they get the topic they must send the notification to; from the request's body it gathers the information to be sent as data to the device. They are sent as data notifications only, the handler (using the React Native FCM API) then shows a local notification already translated with i18n using the react-native-push-notifications package. Everything works fine until I hit the weird situation described below.
Everything was doing alright both on Android and iOS until I launched the app on Test Flight for internal testing and then it stopped working after some time on iOS. Eventually, I noticed that when more than one iPhone subscribed to the same topic it eventually lead to inconsistencies in the delivery of the data notifications. The first iPhone to subscribe to the topic usually worked, the others didn't, the first one most of the time eventually stopped working as well or sometimes it just kept working while the others still didn't. I used the Firebase Console to send some test notification to the subscribed devices and it they actually received it. I then changed my Cloud Functions' code to avoid sending any data and just send some example body and title and it turns out the problem was here. As soon as I send something through the data field in the admin.messaging.Message object to be sent as argument to the admin.messaging().send method, they aren't received by the iPhone devices subscribed to the topic (or it's received by one or two max, the first ones to currently subscribe to the topic. But they usually stop receiving them after a while as well).
This is really really weird and being so inconsistent makes it practically impossible to debug with my current knowledge. Some things to keep in mind:
All Android devices still receive the notifications without a problem
I've watched the iPhone's console through Xcode to see if there was some error when processing the notification, in case they were actually getting the notification but they it failed before it was shown to the user. But nothing is logged by the SpringBoard process, making me conclude the notifications aren't actually getting to the device
I've manually sent notifications with cURL to APNs (with this guide). They were received fine
All notifications without data, regardless of the iOS specific apn headers, payload, etc, are received
What can be the cause of the problem? Or there's something in my code causing this strange behavior (which I doubt, since it works fine on Android and works fine in iOS as well on specific scenarios), there's some type of bug on Firebase's side causing some notifications to not be sent or, finally, there's some error on Apple's APNs side causing this. Highly doubt the last one, if the fault lays on any exterior factor it probably should be on Firebase's handling of topics.
Really would appreciate some help. Thanks in advance. Sorry If I didn't gave enough information, I actually never had the need to do a question on Stack Overflow. I'll leave below an example of a cloud function as well.
exports.orderPending = functions
.region("europe-west2")
.https.onRequest((req, res) => {
if (req.method == "POST") {
res.status("200").send("Webhook successful");
const topic = req.headers["code"];
const message = {
data: {
id: JSON.stringify(req.body.order.id),
event: "orderPending",
order: JSON.stringify(req.body.order),
},
topic,
android: {
priority: "high",
},
apns: {
payload: {
aps: {
contentAvailable: true,
},
},
headers: {
"apns-push-type": "background",
"apns-priority": "5",
"apns-topic": "APP BUNDLE ID HERE",
},
},
};
functions.logger.log(req.body.order.id);
functions.logger.log(topic);
admin.messaging().send(message)
.then((response) => {
console.log("Successfully sent message: ", response);
})
.catch((error) => {
console.log("Error sending message: ", error);
});
}
});
Turns out the data payload was exceeding the 4KB APNs limit but since when you send by topic it doesn't show any errors at all I had no way of knowing. So yeah, add this to your checklist

FCM custom sound not working on iOS when in background mode

Did something change with FCM recently? Our apps use custom sound in push notifications. This has been working fine on both Android and iOS, whether in foreground, background, or closed. But recently something seems to have changed as the same PHP code we've been using suddenly stopped working for custom sounds when the app is closed or in background mode? (The notification is delivered, but it just uses the default device sound).
I was able to get it working again on Android by just including the sound files in /app/src/res/raw (we used to keep them there, but for a long time now it wasn't needed yet always worked... apparently it's needed again). Ok Android solved.
But for iOS it still has stopped working! I'm not sure if it's something I'm doing wrong in xCode or if I need to change the PHP code used to trigger the notifications. So here's both to review:
I am using:
xCode 11.6
iOS target 11.0
Testing on real device, iOS 13.6.1
Sound files are at {Project Name}/Resources/ and were added with "Copy items if needed" and "Create groups" both checked
Server side code to trigger notifications (which always worked fine before and has not been changed):
{
"notification": {
"title": "Test Title",
"body": "Test Body",
"sound": "sound_file.mp3",
},
"registration_ids": ["APA...g","fl...CS"]
}
Please help... thought I was past all of this years ago...ugggg!
Detail information:
https://medium.com/#ThatJenPerson/authenticating-firebase-cloud-messaging-http-v1-api-requests-e9af3e0827b8
Sample code:
{
"message":{
"token":"ewrTK.....",
"notification":{
"title":"Breaking News",
"body":"New news story available."
},
"data":{
"title":"Notification title",
"body":"Notfication body"
},
"apns":{
"payload":{
"aps":{
"sound":"notification.mp3"
}
}
}
}
}
We just touched base with the Firebase dev team and they were able to reproduce the issue. They have just created an internal ticket to fix this. Will follow up here on the resolution progress
After several additional rounds communication with Firebase, here is the current status update and their response:
I would like to let you know that this issue was just recently
identified as a bug with our Legacy API that’s not properly handling
the custom sound for iOS. Our FCM gurus have identified the fix for
this issue, and the fix is about to be released on our production, but
we can’t share any exact details or timeline for the release. The fix
should let you send notifications properly based on your custom sounds
configuration and there should be no code changes needed on your end.
Also, it should still properly handle the notification sounds if the
app is closed or in background state for your iOS application.
We are furthermore told that for the moment, migration from FCM HTTP to HTTP v1 is the only work around. Will post here again once we hear confirmation on the fix to the Legacy API (which most people reading this as using). Stay tuned...
We created a ticket with Google/Firebase support.
Their answer:
There's no ongoing issue on our end that may cause the custom sound to not work. However, by specifying a custom sound, the sound file must be on the user's device already, either in the app's bundle or in the library/Sounds folder of the app's container. You can refer to this documentation for more information.
For FCM, there isn't any extra sound processing, only passing the payload field, so as long as the payload on the client shows "sound" file is successfully passed down, there may be an issue with the setup. If there's no recent change for your application and the sound files, then this may be something caused on Apple's end. With that, it's best to check this with them.
We discovered that we apparently use the legacy HTTP API so we tested the newer HTTP v1 version. (described here: https://firebase.google.com/docs/cloud-messaging/migrate-v1 )
This solved the problem for us. Custom sound is back.
In the mean time Firebase support replied the following and now acknowledges there is a problem with the legacy HTTP API.
Thanks for sharing your observations, Mark.
We’ve received similar reports regarding this issue and that indicates
that this is not an unusual issue in relation to the increasing
reports on the Stack Overflow discussion that you’ve mentioned too.
With that, this issue was just recently identified as a bug with our
Legacy API that’s not properly handling the custom sound for iOS. Our
engineering team has identified the fix for this issue, and we
couldn’t provide a definite timeline at the moment for the release in
our production. I’ll let you know once I have an update for this
issue.
For now, you might consider using our HTTP v1 API to send your
messages. Apologies for the inconvenience this may have caused you.
On my side, no matter what sound name I set, I always receive on ios side "sound": "default"
Sending:
{
"notification": {
"title": "Alert",
"body": "Test Message",
"sound": "alert.aiff",
},
"registration_ids": ["APA...g","fl...CS"]
}
Receiving:
{
aps = {
alert = {
body = "Test Message";
title = Alert;
};
"content-available" = 1;
sound = default;
};
}
The only reason for this to happen is that FCM set sound to default while it redirect to APNS. Need ask google
Please check guys, Google has fixed this issue !!

Firebase Phone Authentication Error: Invalid Token (Even after replacing APNs Key)

Today may production app started having issues on Phone Authentication through Firebase on several devices (but not all).
When I make the call PhoneAuthProvider.provider().verifyPhoneNumber(phoneNumber: uiDelegate:) I get the following error:
Error Domain=FIRAuthErrorDomain Code=17048 "Invalid token." UserInfo={NSLocalizedDescription=Invalid token., error_name=INVALID_APP_CREDENTIAL
As per Firebase documentation, this code indicates that an invalid APNS device token was used in the verifyClient request.
Nothing in my app code has changed, yet this error is occurring. I uploaded a new APNs Key and .p8 file as other StackOverflow posts have suggested, with no luck.
I have tried the suggestions in this similar post.
I am also able to receive push notifications from my app to this device, so it isn't like my push notifications aren't working.
Could anyone suggest ways of debugging his?
Edit: This is the method I am calling:
func sendVerificationCode(to phoneNumber: String, _ completion: #escaping (Error?)->Void) {
PhoneAuthProvider.provider().verifyPhoneNumber(phoneNumber, uiDelegate: nil) { (verificationID, error) in
/* store verificationID locally if exists */
completion(error)
}
}
firebaser here
There was an issue where iOS clients were not receiving the text messages required for one-time password authentication. Its status is being tracked here: https://status.firebase.google.com/incident/Authentication/18006
From there:
We are investigating an issue with Firebase Phone authentication that affects iOS devices. Affected customers may see "invalid token" error during OTP verification.
This issue has been fixed, since 11:15 am pacific time. If you're still seeing similar problems, reach out to Firebase support for personalized help in troubleshooting.

OneSignal iOS SDK send notification fail Code 400

What I have done:
I have created a simple iOS native App which has nothing but OneSignal initializaion codes. I installed my App on my 2 devices, device A and device B. Both devices are subscribed to my OneSignal App Console. When I send a notification from OneSignal Web Console, the 2 devices can both recive the notification.
What I want to do:
When I click on a button on device A, device A will send a notification to device B.
What is the problem:
I followed the OneSignal iOS SDK document and created the following function
https://documentation.onesignal.com/docs/ios-native-sdk#section-sending-notifications
func onButtonClick() {
OneSignal.postNotification(
[
"contents": ["en": "Test Message"],
"include_player_ids": ["c00bb8a6-79da-419b-9999-e919831a5223"] // id of device B
],
onSuccess: {(result) in print("success") },
onFailure: {(error) in print("error : \(error)") }
)
}
however, when I clicked the button, an error shows up in the xCode debug console:
2017-01-20 11:15:52.702852 my.bundle.id[2263:1201122] ERROR: Create notification failed
error : Optional(Error Domain=OneSignalError Code=400 "(null)" UserInfo={returned={
errors = (
"Please include a case-sensitive header of Authorization: Basic <YOUR-REST-API-KEY-HERE> with a valid REST API key."
);
reference = (
"https://documentation.onesignal.com/docs/accounts-and-keys#section-keys-ids"
);
}})
I was confused, because according to the OneSignal Reference document, send notification with "include_player_ids" Does not require API Auth Key.
https://documentation.onesignal.com/reference#create-notification
And it also says: NEVER use your 'REST API key' in client code, it is intended for use on your system or server only.
So what does this error mean? And what should I do?
I even copied and run this "work without any problems" code from this discussion, but the same Authorization error still shows up. What am I doing wrong?
The Answer
Just upgrade the SDK and the problem will be gone.
This was a bug in OneSignal-iOS-SDK 2.3.1, this bug was fixed in 2.3.2.
Please see their Github Release Notes.
How to avoid this kind of problems
Always run pod repo update before pod install to ensure that the pod install the latest version of the frameworks.
Thanks Kevin, Jason and Josh from One Signal Conversations to help me solve this problem.

How do I use Parse.com's push notification feature for a change in PFObjects?

Can I use Parse.com’s push notification feature to send an array of PFObjects to the user when either a new one is added, or an existing one is deleted?? If so, how should I go about it? I’m writing an app for iOS. Any help would be much appreciated.
Edit:
After doing a bit of searching, I see I have to use the cloud code aftersave() method, and then send a push notification through JavaScript.
If anyone could provide examples of how to go about it, that would be great. In the meantime I'm going through the docs. Thanks again all.
The Parse Documentation really contains everything you need to get started. Writing the code that sends the actual notification is only a small part of the whole process that is required to send push notifications. Here are the steps required:
Create an SSL Certificate: create the cert via Apple Developer Center then export it with Keychain Application
Create Provisioning Profile: also done via Apple Dev Center.
Configure Parse App: Change Parse settings, then upload the cert you created in step 1.
Configure App in Xcode: Change app to use provisioning profile you created in step 2.
Add Code to Prompt user for Notifications: write the code in your app that asks a user if they want to receive Push notifications, then store their device ID in Parse Installation class.
Send the Notification: as mentioned earlier this is the easiest step of all. It can be done via the Parse Dashboard (Click on Push notifications when looking at the Data Browser), via the Parse API or via Cloud-code. I did it via Cloud-Code and it looks like this:
// Find the Installation (i.e. iOS Device) that belongs to my user*
var query = new Parse.Query(Parse.Installation);
query.equalTo('user', user);
Parse.Push.send({
where: query, // Set our Installation query
data: {
alert: "This is the notification message",
badge: "Increment"
}
}).then( function(){
console.log("Push notification successfully sent!");
});
*I added a field in my installation class that captures a user. That way I know which device belongs to which user and can send targeted Push Notifications.
Prepare for Release: When you release your app, you will need to do some additional configuration to get everything ready to release.
Again, I recommend the Parse Docs. They are comprehensive and very helpful. Let me know if you have any additional questions. Hope this helps!
** BTW: If you don't have any experience with cloud-code, then you can complete steps 1-5, then manually send the notifications via the Parse Dashboard.

Resources