Firebase custom notifications with FlutterFire - firebase

Thanks to Firebase and FlutterFire, it's easy to send regular notifications from the servers to the users' devices. Those notifications include a title, a body, and an image url. But what about creating a no-that-simple notification, like Telegram's or WhatsApp's?
The simple question is to avoid sending a Notification from the server, and instead set the data field to the push message. But according to the FlutterFire documentation:
Data only messages are considered low priority by devices when your application is in the background or terminated, and will be ignored
So, it sounds like if we want to have a reliable delivery system, we should add a Notification to our push messages. But that notification is so simple. And, again, according to the documentation:
If your message is a notification one (includes a notification property), the Firebase SDKs will intercept this and display a visible notification to your users (assuming you have requested permission & the user has notifications enabled)
So: If I want a reliable system, I have to send Notifications, but I do it, I can't tell FlutterFire to use my custom notifications.
So the question is: how to show custom notifications with FlutterFire?
What I want to achieve is something like this:

I'm going to try setting the priority of the push notification.
You can however explicitly increase the priority by sending additional properties on the FCM payload:
On Android, set the priority field to high.
On Apple (iOS & macOS), set the content-available field to true.
On the server side code, it looks like:
message.setAndroidConfig(AndroidConfig.builder().setPriority(AndroidConfig.Priority.HIGH).build());
message.setApnsConfig(ApnsConfig.builder().setAps(Aps.builder().setContentAvailable(true).build()).build());

Related

How to send an actionable push notification through Ionic?

I am currently using Airnotifier and Firebase Cloud Messaging. From some other questions and after trying by myself, it seems Firebase does not support actionable notifications. Is there a way to create one somehow, or can we somehow send a request to the user's device and the application on that device can create an actionable local notification?
UPDATE: Just checked with the FCM API and SDK, title and body fields are accepted but actions and click_action are not seen anywhere. Is there a way to verify if those data have been given or if those are the correct name for the fields?
UPDATE2: gcm.notification.actions has shown in the notification received in iOS, but not click_action which I included in the notification field.
UPDATE3: It seems the "actions" field magically disappears after the "schedule" function is called in ionic's local notification plug in.

How to send a push notification from the Firebase console to a specific GCM/FCM device token ID

In Urban Airship, when I am composing a notification, I can target specific users by searching for a Urban Airship channel_id (device ID):
I sent from Urban Airship the push notification that I am showing above, and I received it successfully. Now I want to do the same thing, send a push notification to a specific device, but now using the Firebase console. The problem is that in Firebase, in the step where I need to specify the target, it only allows me to choose User segment or topic. I was expecting to see a third option: Target specific users (for sending notifications to one or many specific GCM/FCM device token IDs. So my alternative is to add a GCM/FCM device token ID to a topic and then send the push notification to that topic, which is something that I have successfully done before. Nonetheless, that would be a workaround and not the way I would prefer to do this. Is it possible to send push notifications to specific users (by defining the target GCM/FCM device token IDs) from the Firebase console? Thank you.
UPDATE 1: See how the Firebase console (https://console.firebase.google.com/) only shows User segment and Topic as the Target:
First Step:
Second Step:
Third Step:
By design, the notification feature in the console is for sending out broadcast-type events. Sending user-specific alerts would be more of a programmatic operation done through the API.
For sending test messages, there is a console tool for this, explained here.
Note that it is possible to send a notification to one device by subscribing a device to any topic and sending the notification to that topic in the console (keep in mind topics are public and you can't prevent users from signing up to them; fine for testing most likely).
Another alternative is to send a message via HTTP or curl. Perhaps the best resource for this is the quickstart/messaging example.

Permanent Notification From Firebase Cloud Function?

Is it possible to send permanent notification from firebase cloud functions? User can't dismiss that notification?
For iOS you cannot, but Android you could setup a local notification using remote config.
iOS Remote Notifications
While you could send the same notification over and over again, control of the notifications is at the OS level of the client device. iOS would never let you create a local or remote notification that cannot be dismissed because it would be a poor user experience. In addition, I think you would find that your users would not be happy about it as well.
An alternative, would be to create a custom UI Header in your app that includes a label. You could then use Firebase Remote Config to set a value for that label, that way you can deliver the same message to all users of your app.
Android Push Notifications
In Android, it looks like you can have permanent notifications. However, it looks like this is controlled at the client level. So I still recommend the remote config option here as well. Set the value of the remote config, and then generate a local notification and set the notification to not clear.
notification.flags |= Notification.FLAG_NO_CLEAR;
Perhaps, a key/value pair could be sent from your Firebase Cloud Message call, but you will run into other issues as well. When should the function be called? Will you generate a new notification for every new user added to Firebase? This could become more expensive versus the remote config route.
Google Chrome Notifications
The remote config option should scale to this as well. A quick Google search didn't show anything that would suggest you can do a permanent notification like Android. And even if you could then your solution wouldn't work in Safari and other browsers.

Does Ionic 2 support private & public Firebase notifications?

I'm thinking to work on firebase notifications in the last few days but I want to some answers for this few questions:
Does Ionic2 support firebase notifications ?
Will notification show in the top of screen when my application closed or it should be work in the background ?
Is firebase notification sent on time or it may take long time ?
If I assumed Ionic 2 can send private notifications (to my device only), so can I send public notification for all application users ?
If the answer of all questions "yes", wish you give me any useful links that may help me (if you have a time)
In addition to Gabriel's answer:
The default behavior for notifications is for it to show on the top part of the screen. I haven't worked on Ionic before, but AFAIK, the behavior should compared to Android.
With that said, you should refer to the Handling Messages part of the FCM docs.
There are factors to consider that may affect the time it takes for the device to receive a message, but as FCM's behavior, it would try it's best to send the message, as soon as feasible (see my answer here).
Gonna itemize this further:
Specific device only - Yup. When sending the message, just specify the registration token(s) that should recieve that message.
All devices - Yup. You could use the Firebase Notifications Console. But if you intend to send the message by using the REST API, you have to make sure that the devices are subscribed accordingly to your custom global topic.
About the device-to-device Gabriel mentioned, I think it should be worded as server to specific device(s).
Yes it does, and it works fine with FCM (Firebase Cloud Messaging, aka GCM - Google Cloud Messaging). It supports subscribing to topics and device-to-device notification via HTTP client. The topics subscriptions and device-to-device both require FCM plugin.
Yes, they'll appear on top of your device screen as any push notification, you'll just need to code how it'll behave after receiving it, like execute something if the notification is tapped, or do something if it's received and the app is already open (foreground), it's all up to you.
It's sent on time, i've never had delay problems, the longer it took to send the notification and i receive in my device was 10 seconds. But as far as i know it "stores" your notification for 24 hours, so if the servers go down or something happen the notification has a life time of 24 hours to be sent.
You can, as said in first answer, subscribe your users to topics. Let's say you want a topic to sent message so all users can receive, you can have an all topic, or have one only for logged users, one only for users who bought products, you can even get all users tokens and send one by one, but it's much better having them subscribe to topics.
Topics notifications can be sent via the Firebase project console. Device-to-device is sent by your app code.
To send device-to-device you'll need a token, it's provided by the FCM plugin (link above) and you'll need to store this token somewhere, like in your user node on Firebase. I usualy use the user device ID provided by the Device plugin to have his token, since the user can have multiple devices and log in all then, this'll prevent token overriding.
I don't know good tutorials about configuring FCM with Firebase, if i find something i'll edit the question and add it for you, but stick to the ionic native link and the FCM plugin github page.
Hope this helps.

sending ios push message without user action

I want to collect device attributes and send them to the mdm server using push notification.
Steps involved are:
create a configuration profile with mdm payload
get device token
use apple push notification service to send a notification to the device.
get back device attributes
do same with multiple devices which the company manages
Questions:
Will the user always need to act on a message or I can send a message onto the device without user action and get the work done?
Do I need an app on the device to send back the token?
It sounds like you're trying to use push notifications to poll iOS devices and return information.
Push notifications do not provide a mechanism to execute code on a device without user interaction. You would have to have an application loaded on the device, and the user would have to click through from the notification to your app.
In iOS, applications generally don't run unless the user is actively interacting with them. There are a few exceptions (e.g. background audio.)
You do need to have an app running to get the device token. You call registerForRemoteNotificationTypes. See APNs docs for more information.
Edited to add more information:
Looks like the MDM protocol uses push notifications to do just what you describe. However, there's nothing about it in Apple's "Local and Push Notification Programming Guide" (linked above).
Apparently Apple keeps tight control over the MDM documentation (see here and here).
However, I did find this paper from blackhat describing how the system works.
My earlier point about not executing arbitrary code on a device without user interaction still applies. There's a finite set of commands that can be executed (see Appendix A).
I came across this question when searching for iOS push notification access payload without user interaction - Just want to add that, in iOS 7 and above, it is possible to send silent push notifications to app, and app can process them without user interaction.
However, I've also found some discussions saying that the silent push notifications are not delivered reliably, in this SO post. That's why I'm still looking for alternate ways to retrieve payloads of the normal remote push notifications. Doesn't look like there's a way.

Resources