I have a question.For iOS,Can we send both "data" and "notification" in the payload?I was getting the notification when only "notification" was sent to the GCM.When I added both,I am not getting any notification.My question is,What should be the payload if I want to get update on both iOS and android devices?
current payload :
{
"to":"/topics/testTopic",
"content_available":true,
"priority":"high",
"notification": {
"body":"Test Message",
"title":"test"
},
"data": {
"title": "Title.D Mixed JSON",
"body": "Content.D Mixed JSON",
"productId" : "1"
}
}
Your payload is fine, there is no reason that you should not be getting it. But there are some differences between those two.
Difference between "notification" and "data" is documented in official documentation: https://developers.google.com/cloud-messaging/concept-options
Use notifications when you want GCM to handle displaying a notification on your client app’s behalf. Use data messages when you want your app to handle the display or process the messages on your Android client app, or if you want to send messages to iOS devices when there is a direct GCM connection
So, for you hybrid messages that you want to send it is important to know if your app is in the background or in the foreground:
When in the background, apps receive the notification payload in the notification tray, and only handle the data payload when the user taps on the notification.
When in the foreground, your app receives a bundle with both payloads available.
Another reason for not receiving push notifications is that you have not assigned delivery priority. There are two options normal and high.
High
High priority. GCM attempts to deliver high priority messages immediately, allowing the GCM service to wake a sleeping device when possible and open a network connection to your app server
Normal
Normal priority. This is the default priority for message delivery. Normal priority messages won't open network connections on a sleeping device, and their delivery may be delayed to conserve battery
Try to set "priority" : "high" right before "notification" in your payload.
It's not Allowed to duplicate the content of "notification" and "data", you can read the data inside the "notification" when the user tap on the notification, the following JSON should work with you.
{
"to":"/topics/testTopic",
"content_available":true,
"priority":"high",
"notification": {
"body":"Test Message",
"title":"test"
},
"data": {
"productId" : "1"
}
}
Related
I am sending notifications and data messages (non-notification) over APNS from FCM to my iOS App, and everything works fine in the foreground.
I would like to be able to trigger code when the App is in the background.
I already have "Enable Background Modes" "Remote Notifications" set in info.plist
However the following message does not trigger until I bring the App into the foreground
message = new Message()
{
Apns = new ApnsConfig()
{
Headers = new Dictionary<string, string>()
{
{ "content_available", "true" },
},
},
Data = new Dictionary<string, string>()
{
{"temperature", "warm"},
},
Topic = "thetopic",
};
1) can anyone tell me why this does not run immediately. I tried adding
{
"apns-priority", "10"
},
but this makes no difference, and indeed
Local and Remote Notification Programming Guide
states that
It is an error to use this priority for a push notification that
contains only the content-available key.
What am I missing here?
Is there any way at all in iOS that it is possible to cause an non-running App to run up in response to an APNS notification or message, without involving the user?
Is it possible under iOS to have an App permanently running (i.e. autostart) so that it can always respond to a notification or message?
You are sending "data message" - this will not work for apps that are not running. This is very poorly documented but you have to send "notification message" to awake your app. Just add
"notification":{
"title" : "title",
"body" : "body"
},
to your message to get it to work
You should consider creating a UNNotificationServiceExtension for that purpose.
See: https://developer.apple.com/documentation/usernotifications/unnotificationserviceextension
This is in fact a mini-app that is launched each time a notification is received, in which you can put your code.
We have a Django backend server using pyfcm to deliver Notifications throgh Firebase Cloud Messaging (FCM). I just updated to the today released Version 1.3.0. I want to send a notification using the loc-key and loc-args parameters so it can be displayed in the language the user is using on his phone. The notification reaches the device, it vibrates and makes the default sound for a new notification, but it will not displayed. It just happens nothing except for the sound and vibration.
This is the payload generated by the server which is sent to the fcm endpoint:
{
"notification": {
"loc-args": ["Demo (#demo)"],
"loc-key": "notification-follow",
"sound": "Default"
},
"priority": "high",
"to": "..."
}
On the client side, this is what is received by the phone:
[
AnyHashable("gcm.message_id"):0:1496257581534217 %f910cc44f910cc44,
AnyHashable("aps"):{
category = profile;
sound = Default;
},
AnyHashable("gcm.notification.alert"):{
"title-loc-key":" notification-follow",
"title-loc-args":[
"Demo (#demo)"
]
}
]
Is there anything I have to do before the message is displaying? Sending the message without the loc-key and loc-args but with message-body presents the notificaion on the device. Maybe the payload is wrong? How should it look like to do what I want?
The key, notification-follow in this case, is listed and translated in the Localizable.strings files in any available language.
Ok, problem solved. It was pyFCM. I thought it would be an error with this library, so the developer pseudo-fixed it. But it worked properly, and now it doesn't work anymore. The trick is to give body_loc_args an array, otherwise it will give you an InvalidParameters error.
I'm using this plugin with cordova: cordova-plugin-fcm to get notifications working.
It works good on Android.
Problem is with iOS, when the app is in foreground the notifications arrives. But when the app is closed or in background, the notification doesn't show in notification bar, but when I open the application I can see the notification arriving and the popup I generate, gets opened.
But I really need to notification to show in lock screen and in the notification bar.
This is what I'm sending to Firebase API:
/ POST to https://fcm.googleapis.com/fcm/send
And in the body I'm sending this:
{
"to" : <USER_TOKEN>,
"alert":"Test",
"notification": {
"alert":"Test test",
"title": "Notification test",
"text": "Testing notification text"
},
"priority": 10,
"content_available": true
}
I've also tried with "priority": "high" and get the same results.
The notification arrives, but it only shows when I open the app. I don't even get it in the notification bar or lock screen.
Also I tried adding the "aps" property in the body, with all the information inside.. doesn't work.
I hope someone can throw some light into this..
PS: iOS v10.1.1
PS2: Works good on all android devices.
I've already read some answers from the community but doesn't seem to work:
Firebase API is not sending push notifications when using the API
iOS not receiving Firebase Push Notification sent through the API
Thanks for your time.
have you Upload your Development APNs certificate on console.firebase.google.com,
Upload your APNs certificate to Firebase. If you don't already have an APNs certificate, see Provisioning APNs SSL Certificates.
Inside your project in the Firebase console, select the gear icon, select Project Settings, and then select the Cloud Messaging tab.
Select the Upload Certificate button for your development certificate, your production certificate, or both. At least one is required.
For each certificate, select the .p12 file, and provide the password, if any. Make sure the bundle ID for this certificate matches the bundle ID of your app. Select Save.
you can refer link https://firebase.google.com/docs/cloud-messaging/ios/client
I had the same problem,
First of all, you need to use "body" instead of "text";
For priority you should always use "high" or "normal", for pushnotifications the default value should be high.
If you forget to use the "title" and "body" key in the notification object of your Json string, iOS wont add the notification to the notificatios list apparently.
If you want some custom values, then add a data object with custom values. like this:
"data":{
"data1":"value1",
"data2":"value2"
}
So try something like this:
{
"to" : <USER_TOKEN>, //or /topics/<topicname> or /topics/all"
"notification": {
"title": "Notification test",
"body": "Testing notification text"
},
"priority": high,
"sound":"default", //not using this one wont make your iOS device use sound
"click_action":"FCM_PLUGIN_ACTIVITY",
"icon":"fcm_push_icon"
}
Combined with data object:
{
"to" : <USER_TOKEN>, //or /topics/<topicname> or /topics/all"
"notification": {
"title": "Notification test",
"body": "Testing notification text"
},
"data":{
"data1":"value1",
"data2":"value2"
},
"priority": high,
"sound":"default", //not using this one wont make your iOS device use sound
"click_action":"FCM_PLUGIN_ACTIVITY",
"icon":"fcm_push_icon"
}
I hope this helps, it did for me
I have an app running on iOS 9.3.5. I need to be able to send a push notification to my app when it's in the foreground or background. I don't actually need to include any data, I just need to ping the app so it will "phone home". So I don't need/want the user to see any notifications.
Since the company is already using Firebase for their Android app, I've set that up in the iOS app. If I send a message to https://fcm.googleapis.com/fcm/send with the notification key in the payload, it's received on the iPhone. When I try it with the data key instead, I get nothing. In both cases I get a success response from the POST. I've implemented the follow callbacks:
application:didReceiveRemoteNotification:
userNotificationCenter:willPresentNotification:withCompletionHandler:
applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage
None of those are called when sending a message with the data key.
Edit:
This is the payload I'm using.
{ "data": {
"message": "phonehome",
},
"to" : "xxxxx"
}
After doing some more testing it looks like I get that message when the app is in the foreground but not the background. When I switch to the foreground then application:didReceiveRemoteNotification: gets called.
Edit 2: Added content_available and that did the trick. Thanks!
{ "to" : "xxxx", "content_available" : true }
It's a little bit hard to suggest without a sample payload, However, do try to use the priority parameter as high or content_available to true.
I have implemented in my IOS app the new Google Cloud Messaging framework for handle push notifications. After implementation I'm able to receive push notifications only when App is active and in foreground. If App is closed or in background I didn't get the notification alert in my device. In the iOS notifications settings I see my app enabled to receive them.
I was having a similar problem where the app would receive a notification only if the app was running (foreground/background) and wouldn't receive anything if app was killed manually (terminated)
All I had to do to fix that was to add priority & content_available to the notification body when sending it (as #KayAnn pointed out)
Here's how it should look like :
{
"to" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...",
"content_available" : true,
"priority": "high",
"notification" : {
"body" : "Winter is coming!",
"title" : "John Snow"
}
}
Keep in mind that I also have UIBackgroundModes: remote-notification in Info.plist.
Google documentations (in step 4) quotes:
If the GCMExample application is running in the foreground, you can
see the content of the notification printed to the Xcode debug
console; if the GCMExample app is in the background, you will receive
an APNS notification.
So in order to receive messages when the app is in background you have to register APNS as options as below as described here.
_registrationOptions = # {
kGGLInstanceIDRegisterAPNSOption: deviceToken,
kGGLInstanceIDAPNSServerTypeSandboxOption: #YES
};
EDIT:
There are few other things you need to do:
1) While publishing the app move to production certificate
In the JSON Payload:
2) Use "content_available": "true" OR
3) Set the "priority": "high"
EDIT:2
Using content_available and priority high in the same payload conflicts with Apple's recommendation (Apple docs). I have come across this during my testing. In such a scenario the message may be throttled.
Use either / or of these two parameters instead.
A Tip on working use-cases:
- content_available: use when you are sending data only and do not have other notification specific parameters like alert, badge, sound etc. This is because by default a content_available message is a silent push.
- Priority: high: Use it when you are sending a notification which should reach the users immediately, i.e time critical notifications such as game scores.