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.
Related
I am using APNS push with content-type = 1. I receive the payload and fire using local notification.
this works fine in background and foreground mode
but when app is killed I get nothing.
what is the solution? I have seen people saying something about VOIP apps
but mine is not a VOIP app..
Some said to check Pushkin framework?
Any guidance?
Update
with this Json format I received notification when app was in killed state.. I checked on lower version 9.3 iOS.. have to check on iOS 11..
{
"aps": {
"content-available": 1,
"alert": "custom message ",
"badge": 1,
"sound": "solemn.wav"
},
"id": "55",
"data": "your data"
}
I don't think it can be done if your app is not a VOIP app. But, if you want to change the appearance of a push notification, you may want to look at the Notification Service Extension.
You have to include mutable-content flag as 1 in your payload inside aps. You cannot use content-type flag with this notification. Also, you have to show user something or the other after you have received the payload. Also, it is available in iOS 10 and above only.
Even with all these constraints, you can do some amazing things with this extension which were not possible earlier.
You can now show media attachment in the notification.
You can download small content or hit an API.
You can modify earlier sent push notification.
You can create a local notifcation center in your app.
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 have a (Nativescript) iOS client app that should be able to receive a push notification sent by Firebase. It actually does receive data when the app is in the frontend, but as the documentation states, iOS should detect an incoming notification (when the app is in the background) and display the notification.
This last part just doesn't seem to work. I tried a lot, but can't figure out what's going on here.
I'm sending the notification by using the Firebase console. There's just nothing happening on my iPhone here, no sound, no badge, nothing!
While Firebase claims everything just works fine...?!
Can anybody help me?
Okay, so I guess I wasn't very specific, so let me add some code below.
I am posting this code to Firebase (https://fcm.googleapis.com/fcm/send) to trigger the push notification:
Content-Type:application/json
Authorization:key=[MY_KEY_HERE]
{
"to" : "[FIREBASE_DEVICE_ID_HERE]",
"priority": "high",
"notification" : {
"body" : "Howto write a great body?",
"title" : "Some title...",
"sound" : "default"
}
}
As you can see, this is a notification message, not a data message, so it should trigger a popup, badge, sound, or whatever on iOS...right?
As I mentioned before, I am able to receive this json data, but only if my app is in the foreground... So the Firebase-device-id is absolutely right and also is the authorization key.
What else should I try?
FCM Data-Messages are not displayed while the app is in background :
On iOS, FCM stores the message and delivers it only when the app is in the foreground and has established a FCM connection.
Source here : FCM Documentation
Be sure to sens Notification and not a Data message
Maybe you are disconnecting from Firebase with such command
[[FIRMessaging messaging] disconnect];
Could you tell if you can send message via APNS from your computer (without Firebase)? Example file to send such message can be found somewhere in this tutorial https://www.raywenderlich.com/123862/push-notifications-tutorial .
I have an application in iOS that receives push notifications through GCM (Google Cloud Messaging) and APNS . These notifications contain some data that has to be processed before showing anything to the user.
After data processing, I generate a local notification with proper information to the user.
I see this behaviour:
With the app in foreground I only see the local notification.
With the app in background I see both notifications, remote and local.
With the app not even running, no notification is shown.
Can I show only my local notification after processing some data? (at least when the app is in background)
I have read about using content-available property as documented here, but the behaviour is almost the same.
Finally I get the solution (thanks to #DmytroShvecov for the pointer).
It is necessary to follow the official documentation here and follow these steps in the server:
Include inside aps keys alert, badge and sound but with blank value.
Include content-available key with 1 as value (if you want your notifications to be processed in background without interaction of the user.
Include any acme key to be treated as custom payload with your data.
This is an example of everything working together:
{
"aps": {
"alert": "",
"badge" : "",
"sound":"",
"content-available": 1
},
"acme": {
"what": "ever",
"you": "want"
}
}
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.