Google Cloud Messaging with Xcode 7 and iOS 9 - ios

I've managed to use GCM with Android devices, but now I'm trying to implement on an iOS app.
I think I followed all the necessary steps to be able to use the GCM service. I've followed the https://developers.google.com/cloud-messaging/ios/client?configured=true to add the service to an existing app.
I think that everything is configured correct but I'm not able to receive any notification. I have the APN certificates and the GoogleService-info.plist file, on the console this is the output:
2016-01-05 13:47:53.864: GCM | Invalid key in checkin plist: GMSInstanceIDDeviceDataVersion
Connected to GCM
2016-01-05 13:47:57.501: GGLInstanceID | Invalid last checkin timestamp in future.
2016-01-05 13:47:57.501 TerneraGallega[1014:444977] Checkin successful with authId: 4958686017822257121, digest: KSJNg+Aj82uavBXrFeOAfA==, lastCheckinTimestamp: 1451998077000
Registration Token: nC-dd9fCvFs:APA91bHMfbvLczxSPLQsLeEJASU_RfV4wvVuPs2u4VJDtpC8oB2cm1AKr6cH_LFeLsQzb94Kk9iwez8fsOvgzNns_9DU3i8Ema1oCKIwcNFzenNpAhViyGHah4E7-RkQlg1durYSEQRD
Already subscribed to /topics/global
As soon as I receive this if I made a POST request with:
{
"to" : "nC-dd9fCvFs:APA91bHMfbvLczxSPLQsLeEJASU_RfV4wvVuPs2u4VJDtpC8oB2cm1AKr6cH_LFeLsQzb94Kk9iwez8fsOvgzNns_9DU3i8Ema1oCKIwcNFzenNpAhViyGHah4E7-RkQlg1durYSEQRD",
"content_available" : true,
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark"
}
}
The first attempt it returns a success message but no log nor notification is shown in the device (with the application on the foreground)
After a few minutes, on another try of the POST request now it returns a failure with
{
"multicast_id": 7256343774952522277,
"success": 0,
"failure": 1,
"canonical_ids": 0,
"results": [
{
"error": "NotRegistered"
}
]
}
Now, I downloaded the GCM Example from the Google page, configured the APN and also got the GoogleService-info.plist, when launching the GCM Example Swift file as soon as it opens on the device it says:
Registration Succesful! Check the xcode debug console fot the registration token that you can use...
After this, if you set a POST Request with
{
"to" : "mVEtHyTXEOg:APA91bGvK_Uf2ZKgpguWUOto3CXQzIT1z22uJ446mYkNqMwL9VLDYdGtdm_4vS8rcl3T9OeqEC1UWbdKAOyuoweW1GiU0mv0cDSPW03y4XGx19JcR6rxsiWRNUjtADX6iNAW8wM8UBJl",
"content_available" : true,
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark"
}
}
it again returns a
{
"multicast_id": 7435981433811133310,
"success": 0,
"failure": 1,
"canonical_ids": 0,
"results": [
{
"error": "NotRegistered"
}
]
}
Can someone who has implemented GCM on an app could point me what I'm missing?
-------EDIT-------
Finally I've made it work by using a distribution certificate instead of a development certificate... it seems that I have all good configured but I'm missing something with the development certificate.
PS: In the APN Profile page under ID I have enabled both Push notification certificates (that's why I thought that everything was good configured)
But I'll leave this question open because I'm unable to use this under development circumstances

I was able to do effectively the same thing as the fix in your edit without messing with the certificates. Obviously this won't work if you already have everything setup, and are using both the development and production environments, but if you're just in the development stage it's easy enough to change.
In the Gcm example project (for objective-c) in application: didRegisterForRemoteNotificationsWithDeviceToken I simply changed the code from:
_registrationOptions = #{kGGLInstanceIDRegisterAPNSOption:deviceToken,
kGGLInstanceIDAPNSServerTypeSandboxOption:#YES};
To
_registrationOptions = #{kGGLInstanceIDRegisterAPNSOption:deviceToken,
kGGLInstanceIDAPNSServerTypeSandboxOption:#NO};
I understand this isn't exactly a real fix, but it at least made it work without changing any certificates (as long as the production certificate is setup.

Related

Sending data message to iOS device results in FirebaseMessagingException: Request contains an invalid argument

When we are sending a FCM data message with the Firebase Admin SDK to an iOS device we are getting the following exception.
com.google.firebase.messaging.FirebaseMessagingException: Request contains an invalid argument.
We are using com.google.firebase:firebase-admin:6.7.0 and the message is build like this.
Message.builder()
.setApnsConfig(ApnsConfig.builder()
.putHeader("apns-priority", "10")
.setAps(Aps.builder()
.setContentAvailable(true)
.build())
.build())
.putAllData(data)
.setToken(token)
.build();
I don't think that there is something wrong with the token itself, because sending a push notification to this token works.
Also the message size shouldn't be an issue, because it is basically just an id in it.
I'm not sure if it happens on all iOS devices.
Am i missing something on building the data message?
Update
I checked the details included at the exception and there it looks like something is wrong with the token.
{
"error": {
"code": 400,
"message": "Request contains an invalid argument.",
"status": "INVALID_ARGUMENT",
"details": [
{
"#type": "type.googleapis.com/google.firebase.fcm.v1.FcmError",
"errorCode": "INVALID_ARGUMENT"
},
{
"#type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"field": "message.token",
"description": "Invalid registration token"
}
]
}
]
}
}
But when i send a data message to the same token using postman, there is no error and the response looks like this.
{
"multicast_id": 7455611938954436367,
"success": 1,
"failure": 0,
"canonical_ids": 0,
"results": [
{
"message_id": "0:1614145427166557%c5757de0f9fd7ecd"
}
]
}
I think i figured it out.
My problem was that i was setting the apns-priority to "10". According to the documentation, notifications with a priority of "10" have to be delivered immediatly.
If i put my device into flight mode for example, and sending my data push it failes with the mentioned firebase exception about an invalid token. Which is very missleading in my opinion.
If i set the apns-priority to "5" everything works fine.
It may happen if you try to use a push notification with only data (silent push notification) which do not required the user to have granted permission to receive on iOS, but you set some iOS specific parameters in the request.
In this case, the error does not occur if the user has granted permission to receive push notifications or you don't specify any iOS specific parameter.

Test FCM notification on iOS Simulator

Since Xcode 11, iOS simulators can now receive push notifications. This can be accomplished by executing a specific command or providing a JSON file to the simulator. I got this information from this blog: Test Notification on Simulator.
However, in my use case, I use Firebase Cloud Messaging to deliver notifications to the user of my app (Android and iOS both). I am unable to test the notification generated by Firebase on a simulator as the payload is different for FCM.
Is there a way to achieve this?
I found a similar question on the topic but there's no answer on it so far.
For Testing push notifiction in simulator just creat a new file and paste following json object and save file in .apns , and then just drag and drop this file in simulator then push will work.
{
"aps" : {
"alert" : {
"title" : "sarunw.com",
"body" : "A asdfsadfsadf"
},
"badge" : 5
},
"Simulator Target Bundle": "bundleId"
}
For differenct payload just use following JSON object in Postman
{
"to" : "ewebWDSSwYc:APA91bGOARO4eq9LlOfgfXPQZMK3IiRFanhzSSgkD6cVqJiPbnyuEgsvQMarDEl6Hg7_UMtFM6Hn90jyTsSENNqLNNLVjLSypR0voAO3j6orIsgabX-t0cpfJ0qpB2SzhZiXvor0r__d",
"notification":
{
"title":"ABC",
"body":"123",
"mutable_content" : true,
"category":"CustomSamplePush"
},
"data":
{
"sender_contact_code" : "+92",
"sender_contact" : "(555) 564-8583",
"group_name":"Testing Group",
"type":"NewMessage",
"type_id":"1213",
"notification_type_id":"123213"
}
}
with this url -> https://fcm.googleapis.com/fcm/send
(Type will be POST)and in authorization you need to put Server key in Key Value, in that way you will be able to send push notification with your own formatted object.
also you can use Pusher for testing push notifiction & I have uploaded screenshot for reference.
Since Xcode 12 (not 11), it's possible to registrer to push and receive via the APNS payload. If the drag & drop it not working, you can use Terminal :
xcrun simctl push booted YOUR_BUNDLE payload.json
With :
"booted" = the current simulator
"payload.json" = the APNS file on
your Mac.
It may look like:
{
"Simulator Target Bundle": "xxx.xxx.xx ",
"aps":{
"alert":{
"title":"XXX",
"body":"YYY YYYY"
},
"badge" : 5,
"data":{
// stuff
},
"mutable-content":1
}
}
maybe you found the solution a long time ago but here is how i made it work:
to allow FCM notification to be catch in your app you need to add that in your .apns file
"gcm.message_id":"20021"
here is the apns file i use to test FCM notifs on my Iphone simulator:
{
"Simulator Target Bundle": "XXX.XXXXXX.XXX",
"aps": {
"alert" : {
"title" : "Test notif",
"body" : "Should update widget && refresh feed"
},
},
"gcm.message_id":"20021",
"data": {}
}

iOS Notification Service extension causes Program ended with exit code: 0

I'm having a trouble to use rich notification.
When I send a apns payload, my notification service extension print Program ended with exit code: 0 error message EXC_RESOURCE and show like normal notification.
The error message seems like attached picture.
my apns payload is
{
"aps": {
"sound": "default",
"mutable-content": 1,
"alert" : {
"title" : "tt",
"subtitle" : "st",
"body" : "bd"
},
"badge": 2
},
"imgUrl": "http://someimagelink.jpeg"
}
I don't know why, but even it everything works fine if I try in a new project. Help me please.
I guess you had imported some heavy library, which causes the issue because all extension is registered with iOS when app installs so there is the size limit on 20 MB (around) you can't exceed. If you exceed extension will be terminated by OS.
P.S Try to look service extension memory consumption in debug navigator. Hope it helps. I had faced same issue.

Firebase cloud messaging for iOS: don't receive notifications from POST request (only from composer tool)

I setup Firebase cloud messaging for iOS using their quick start tutorial and API docs: https://firebase.google.com/docs/cloud-messaging/ios/first-message
I sent message to my iPhone from Firebase composer tool and it was successful. But the next step I need is to receive a notification sent with regular POST request. I found that it's possible to do with legacy protocols: https://firebase.google.com/docs/cloud-messaging/auth-server#authorize_legacy_protocol_send_requests
I tried to send the following:
{ "data": {
"message": "This is a Firebase Cloud Messaging Topic Message!"
},
"to" : "myIphoneTokenHere"
}
via postman with authorization key for my Firebase project and received success in response:
{
"multicast_id": 5644065455367933439,
"success": 1,
"failure": 0,
"canonical_ids": 0,
"results": [
{
"message_id": "0:1513338642327024%5529b926f9fd7ecd"
}
]
}
But I there was no notification on my iPhone unlike with the composer tool.
Wondering what can I try to solve this problem, how can I debug what goes wrong?
Solved the issue by changing request body to:
{
"content_available": true,
"notification" : {
"body" : "This is a Firebase Cloud Messaging Topic Message!",
"title" : "FCM Message"}, "to" : "myIphoneTokenHere" }

Can't send push notifications using the server API

Im using the new Firebase platform. Am trying to get a push notification sent by my app server and delivered to my iPhone.
I have the setup working where I manually send the message with the Firebase notifications area on the website, but when I try and send the message with a POST to https://fcm.googleapis.com/fcm/send I get no message being delivered to the device.
I'm sending the following (with auth headers)
{ "notification": {
"title": "Portugal vs. Denmark",
"text": "5 to 1"
},
"to" : "<registration token>"
}
I'm getting a 200 response from the POST with the following body:
{
"multicast_id": 5511974093763495964,
"success": 1,
"failure": 0,
"canonical_ids": 0,
"results": [
{
"message_id": "0:1463685441784359%3ad254b53ad254b5"
}
]
}
If I try and send to this device directly through the Firebase website it works, but the above form post doesn't. No idea where to go from here!
On iOS the priority field seems mandatory.
{
"to": "cHPpZ_s14EA:APA91bG56znW...",
"priority": "high",
"notification" : {
"body" : "hello!",
"title": "afruz",
"sound": "default"
}
}
If the API returns you a message_id it means that your message has been correctly accepted and it will eventually be delivered to the device.
On Android, messages are delivered as soon as possible (provided that the device is connected of course).
On Apple devices, IF the application is closed or in background, the notification is sent through Apple infrastructure and can be delayed accordingly with Apple documentation.
To reduce the delay of the prioritymessages sent to Apple device you can use the priority parameter.
More details: https://firebase.google.com/docs/cloud-messaging/concept-options#setting-the-priority-of-a-message
I found the message field to be required, as well as the priority field to deliver a message with a POST.
message is not required (and labeled as optional) in the Firebase Console.
I resolved adding the notification tag in request body without priority.
Here is a recap:

Resources