I am creating a framework that can receive custom (a certain data model) messages from firebase. The framework is going to be implemented to receive notifications that are not related with the app but with other stuff.
So the framework is going to handle all the display issue by translating the data received and create a notification as it is indicated (I have some flags in data receive that indicated if I should use an image or attach an icon .. etc).
So I did some research on how to receive messages from FCM.
In first instance, I found direct channel that allows to bypass APNS, the problem is that, this only works with the app in foreground.
I indeed create a test project in firebase, a single view app, register my app in firebase project, set info.plist, configure , and send a notification to my app and It worked (just when the app was open).
Then I looked for another choice, and I found APNS. Skiping all the process for validation between firebase and APNS. I found that when you have all set up (and you put all initialization in didAplicationFinishLaunching) your app is able to receive notifications from firebase (Through APNS) when applicaton is in background.
But, notification received (the one that gets displayed) is just for you to tap over it and then it will fire up the app again an only then you will receive the whole data in aplication:didReceiveRemoteMessage method.
My question is, is there a way I can get this custom messages even in background and when received I can display a notification with the content of the whole message?
Yes,
Fortunately you can do that but for that you need to send Silent Notification which will let you process the notification in background and schedule local notification to trigger it with you desired data.
I'm using OneSignal Swift in a native iOS application. So far i'm able to handle all kinds of push notification cases (foreground/background) except one. When user has force closed the application from "task bar switcher".
I'm using 'content-available':'true' in all my notifications through OneSignal in order to wake up my app and start running code the exact moment the notification is received. So far so good everything is working as expected.
When the app is closed and push notification arrives, if user taps it, then handling with OneSignal works with OSHandleNotificationActionBlock callback.
The problem though is if user doesn't tap and just opens through application icon, then notification data are completely lost. Online reference dictates that the correct way of handling this (what most apps like Facebook do), is query tour APNS (OneSignal in my case) for unhandled notifications and re-fetch them on application start. So the question is how to do that from OneSignal iOS native SDK? Is it possible? There is ofc the REST Service -which might be the case- but users are clearly discouraged to call the API from inside client application. An app key is necessary which shouldn't be saved in device's keychain apparently for security reasons(??)...
I am working on a chat application and I have successfully integrated Firebase in my iOS app. And now, Firebase Notifications is not clear to me. I have setup everything properly as per the documentation, and now I am getting notification to my device when I send notification from the Firebase Console. I want to know how Firebase will handle or send notification to a particular user. Do we need to send device info to Firebase? Or how does it work?
I want to know how Firebase will handle or send notification to a particular user.
The Firebase Server acts as a middle-man that handles the queuing of your messages.
For example, for Downstream Messaging, you send a push notification from your App Server or using the Firebase Console, the message is delivered to the FCM Server, storing it until such time that the device it has to send it to is available to receive it (or until such time you want it to be delivered, see Lifetime of a Message).
You can check out this Lifecycle Flow of GCM (since I think for this part, it's still the same for FCM):
Lifecycle Flow
Send and receive downstream messages.
Send a message. The app server sends messages to the client app:
The app server sends a message to GCM connection servers.
The GCM connection server enqueues and stores the message if the device is offline.
When the device is online, the GCM connection server sends the message to the device.
On the device, the client app receives the message according to the platform-specific implementation. See your platform-specific documentation for details.
Receive a message. A client app receives a message from a GCM connection server.
Note that this the one I mentioned is only for Downstream Messaging. You can check out the docs if you're also interested to learn about the flow for Upstream Messaging.
Do we need to send device info to Firebase?
It depends on your use-case. Although I think it is highly advisable for you to store all the registration tokens for future use.
Or how does it work?
Besides from what I already mentioned above, I'm just gonna go ahead and quote the How does it work? from the official docs:
How does it work?
An FCM implementation includes an app server that interacts with FCM via HTTP or XMPP protocol, and a client app. You can compose and send messages using the app server or the Notifications console.
Firebase Notifications is built on Firebase Cloud Messaging and shares the same FCM SDK for client development. For testing or for sending marketing or engagement messages with powerful built-in targeting and analytics, you can use Notifications. For deployments with more complex messaging requirements, FCM is the right choice.
I recently ran into the same problem when dealing with FCM.
You need to set both data and notification objects in your message payload like it is described here.
https://firebase.google.com/docs/cloud-messaging/concept-options#notifications
Messages with both notification and data payloads
App behavior when receiving messages that include both notification
and data payloads depends on whether the app is in the background or
the foreground—essentially, whether or not it is active at the time of
receipt.
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
message object with both payloads available. Here is a JSON-formatted
message containing both the notification key and the data key:
{
"to" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
},
"data" : {
"Nick" : "Mario",
"Room" : "PortugalVSDenmark"
}
}
Also, you need to set high priority if you want to deliver push notifications on the device.
https://firebase.google.com/docs/cloud-messaging/concept-options#setting-the-priority-of-a-message
Setting the priority of a message
You have two options for assigning delivery priority to downstream
messages: normal and high priority. Delivery of normal and high
priority messages works like this:
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 the battery. For
less time-sensitive messages, such as notifications of new email or
other data to sync, choose normal delivery priority.
High priority
FCM attempts to deliver high priority messages immediately, allowing
the FCM service to wake a sleeping device when possible and open a
network connection to your app server. Apps with instant messaging,
chat, or voice call alerts, for example, generally need to open a
network connection and make sure FCM delivers the message to the
device without delay. Set high priority only if the message is
time-critical and requires the user's immediate interaction, but
beware that setting your messages to high priority contributes more to
battery drain compared with normal priority messages. Valid values are
normal and high. For more details, see the server reference for HTTP
or XMPP.
For iOS client apps, normal and high priority are analogous to APNs
priority levels 5 and 10. For full details on iOS-specific behavior,
see the APNs documentation. For details on Android-specific behavior,
see Optimizing for Doze and App Standby.
Here is an example of a normal priority message to notify a magazine
subscriber that new content is available to download:
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"priority" : "high",
"notification" : {
"body" : "This week's edition is now available.",
"title" : "NewsMagazine.com",
"icon" : "new"
},
"data" : {
"volume" : "3.21.15",
"contents" : "http://www.news-magazine.com/world-week/21659772"
}
}
You don't need to send anything to Firebase if you have successfully configured your project for Firebase.
https://firebase.google.com/docs/cloud-messaging/ios/client
On Android, to send a notification all you need to know is GCM id of a device you want to notify. Assuming my user knows the id of another user, he sends a message directly to GCM server, what decreases the load on my server. I don't have experience building iOS apps, so I want to know if the same approach is possible for iOS devices. If my user knows id of another apple device, can he send a request directly to APNS?
Sending a push is just a matter of posting data to apple servers.
You can do it from your server or the device. It's up to you.
Try NWPusher. It contains an iOS demo application that shows how to send push notifications from iOS to iOS.
I am create a firebase based chat application for iOS. One of the features is to send push notification to the users in the same chat room (if they are offline).
I can't find a firebase function (for iOS) that can be used to send push notifications to the user.
Is it possible?
Displaying alert badges and notifications on iPhone applications is accomplished through Apple's Push Notification system. Since the application is not running on the user's phone when they receive notifications, the APN will have to be triggered from your server-side code.
You'll probably want to create a server-side module that listens for changes to your chat Firebase. When a message appears for a user that is offline, you'll have to schedule a remote notification with the APN. That latter part has nothing to do with Firebase, but has extensive documentation on Apple's developer web site.
I'm not sure if a web application can display alerts or badges. Otherwise this approach will only work if you create a native wrapper for your Firebase chat application.