send notification from the client - Firebase Cloud Messaging - swift programmatically [duplicate] - swift

This question already has answers here:
How to send one to one message using Firebase Messaging
(5 answers)
Closed 1 year ago.
Is it possible to find a push notification from the client using swift?
I searched online and the only examples, documentation I found were strictly related to the use of Firebase Cloud Functions and or using the Firebase Console.
I wanted to know if it's possible to create and send a notification using Firebase Cloud Messaging from the client.

It's actually possible using the firebase rest api, the following endpoint could be used to send a notification:
https://fcm.googleapis.com/fcm/send
The HTTP request must be a POST request with these headers:
Authorization: key=<server_key>
Content-Type: application/json
and the following body:
{
"to" : "<FCM TOKEN>",
"collapse_key" : "type_a",
"priority": 10,
"data" : {
"body" : "Notification",
"title": "Notification title"
}
}
Of course you need to know to the FCM token of the target device that should receive the notification or at least the topic where the device/user is subscribed to. If you want to send notification to a specific topic use the following body:
{
"to" : "/topics/<YOUR_TOPIC>",
"collapse_key" : "type_a",
"priority": 10,
"data" : {
"body" : "Notification",
"title": "Notification title"
}
}

Related

Remove duplicate notification in Flutter

I use Firebase Cloud Messaging to send notifications on Flutter.
Some notifications of my chat application are repeated, for example, when 3 messages are sent in a chat, 3 notifications are sent.
I want the previous notifications to be deleted and only the last notification to be displayed
send it with a data message, not a notification
{
"to":"_some_fcm_token_",
"type": "new_message",
"data": {
"model":{"id":"same_user_notification_id"},
"body": "subtitle",
"title": "title",
"click_action": "FLUTTER_NOTIFICATION_CLICK"
},
"priority": "normal"
}
this is different between Message types :
Notification message
Data message
About FCM messages

Firebase Cloud messaging push notification not showing after flutter web app terminated

I am implementing the firebase cloud messaging to make my push notification service for a project but I am at a block, if I close down the tab which means I close my flutter web application I do not get the notifications that were sent whilst the time my app was closed in my browser.
I have looked through different threads here suggesting i remove the notification key from the message I am sending to FCM.
Please refer to the payload sent:
> {
"to" :[ "xxxxxxxxxx"]
"data" : {
"body" : "Notification Body",
"title": "Notification Title",
"key_1" : "Value for key_1f",
"key_2" : "Value for key_2"
}
}
The problem though still remains when I close the tab running my flutter web app and send a notification, I do not receive it when I open the app in a new tab again.
Essentially I have to provoke the onMessageReceived() function but I guess this would not be in my case. Is it different because the thread(link) are based on mobile apps and not web applications.
`

Foirebase no server key found

My project in FCM has no server key shown in cloud messaging .. only sender ID as shown .. and I am trying to send notification from post man .. so I must have the server key
I tried to get the key from the google-services.json file api_key .. current key but it's not working.
the code sent in postman is :
header:
Authorization key="AIzaSyBENvif3XkufbKkjRRE92ECj7RBA0LlWj0"
body:
"to":"c_OHgeJnQmi_F3i3D7wivZ:APA91bHnvHMI_mR9IWoesWeBOLE1aEXGa-1v-1UVCtt1MriXRo03VcaxxTiBAztnbUOrXCf_AVAqAgyOGBcvK4IsO0cW1o1UNXJQqVpkLKveH0aAPkd7kHvBhUGcGkY--IfBctUvgrR7",
"notification" : {
"body" : "New announcement assigned",
"OrganizationId":"2",
"content_available" : true,
"priority" : "high",
"subtitle":"Elementary School",
"Title":"hello"
},
"data" : {
"priority" : "high",
"sound":"app_sound.wav",
"content_available" : true,
"bodyText" : "New Announcement assigned",
"organization" :"Elementary school"
can any one help please ?
I found the solution ... FCM has two ways to send notification
1 - Firebase Cloud Messaging API (V1)
2 - Cloud Messaging API (Legacy)
The server key is available on the second way which is disabled by default and must be enabled to get the server key and send notification via API using post man
Firebase Generate Server-Key 2023
I ran with this issue so this is what i've done
The server key for testing the Cloud messaging is disabled by default
First navigate to the menu of firebase and click Project Settings -> Cloud messaging tab
enter image description here
You will find Cloud messaging API disabled enter image description here
click on the dots -> manage API in Google enter image description here
Click on enable 4
Return and refresh the page, you should now be able to copy & test with your server key.

APNs works fine separate from Firebase, but cannot get notifications to work in FCM

Sorry if this is a dumb question, I am setting up Firebase with APNs for the first time. I am working in Swift with an iOS app and I have apns messages working separately without Firebase -- so remote push notifications are received on the device in background and while running app. I can use this tool https://github.com/noodlewerk/NWPusher to send notifications on Sandbox and Production apns certs successfully by sending this json blob as an example:
{"aps":{"alert":"Testing","badge":1,"sound":"default"}}
However, now I am trying to incorporate Firebase and not getting the same behavior on the iOS device.
I have Firebase messaging setup and the app prints remote messages with "shouldEstablishDirectChannel = true" but APNs is never received (with this set to false then device doesn't print anything). No push notifications are received on device. I don't believe APNs is being mapped correctly for Firebase messages as I cannot get the device to display push notifications in the same way that I can with NWPusher on strictly APNs Sandbox and Production certs. Firebase app console has the app's .p12 apns cert (which is working fine for apns without firebase involved)
Here's a sample request I try to send to https://fcm.googleapis.com/fcm/send
{
"to" : "eqkeyhereinifnbe",
"collapse_key" : "type_a",
"data" : {
"body" : "Notification",
"title": "testing",
"key_1" : "Data for key one",
"key_2" : "Hello Meowww",
}
}
API returns a success response:
{
"multicast_id": 634054476369,
"success": 1,
"failure": 0,
"canonical_ids": 0,
"results": [
{
"message_id": "0:15329261441bfd36cccfb49c"
}
]
}
In iOS app's AppDelegate:
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print("Received data message: \(remoteMessage.appData)!")
}
^prints the message successfully from the api on device:
Received data message: [AnyHashable("collapse_key"): type_a,
AnyHashable("key_1"): Data for key one, AnyHashable("from"): 9861340,
AnyHashable("key_2"): Hello Meowww, AnyHashable("body"): Notification,
AnyHashable("title"): testing]!
However this received message doesn't do any of the APNs notification magic to an iOS device. I tried to send the same json blob to Firebase API that works fine in NWPusher: {"aps":{"alert":"Testing","badge":1,"sound":"default"}}, but device does not display the notification. Why? How troubleshoot this to make Firebase send the same APNs notification to show notifications on device?
I just needed to write the request with "message" and "data" objects like this:
{
"to" : "aTokenHerefonsfondfodnfdofndf",
"collapse_key" : "type_a",
"notification":{
"title":"Notification title test",
"body":"It worked body"
},
"data" : {
"body" : "Notification",
"title": "testing",
"key_1" : "Data for key 1",
"key_2" : "Hello Meowww"
}
}

Multiple Responses with Dialogflow (aka Api.ai)

I'm trying to create a chatbot which once "greetings" process is done goes on and initiate a new topic without any user query. It has to be something like the following:
bot : hello
user : hello
bot : how old are you?
user : 35
bot : Great.
bot : Let's talk about politics. Are you american?
Until the "great" line everything works but then I cannot trigger the event that will prompt the line "Let's talk about politics...."
The doc is vague, can I do this without webhooks? And if not, how would a webhook like this look like?
You can define multiple responses in Dialogflow's console as seen in the screenshots below by clicking the Add Message Content button in the response section of the intent you'd like to add the response to. You can also send multiple messages for some platforms (depending on platform feature availability) with webhook fulfillment using rich messaging responses documented here: https://dialogflow.com/docs/rich-messages
Go to the response section of the intent you'd like to add a 2nd response to:
Click ADD MESSAGE CONTENT and select Text response:
Enter you second message in the second text box provided:
Yes, you can define multiple responses. If you are planning to use Facebook Messenger platform to show the responses you can use the code below. Change "Response 1" and "Response 2" to your desired text and dump the my_result object as json and return it back. You need to change the "platform" if you want to use any other platforms than messenger.
my_result = {
"fulfillmentMessages": [
{
"text": {
"text": [
"Response 1"
]
},
"platform": "FACEBOOK"
},
{
"text": {
"text": [
"Response 2"
]
},
"platform": "FACEBOOK"
}
]
}