Flutter / Firebase push notifications not fired when app is closed - android

I am using firebase messaging to send notifications to users in flutter application, and everything is working fine when I am sending notification from postman (notification always shows), but then when I want to send the same notification from my backend server, notification not showing on device. On server logs I can see that notification was send with success like this:
{
"multicast_id": XXXXXXXXXXXXX,
"success": 1,
"failure": 0,
"canonical_ids": 0,
"results": [
{
"message_id": "XXXXXXXXXXXXX"
}
]
}
So it means it was correctly send to messaging services, but push notification not firing on device.
Sometimes when I open app (going to foreground of application) then notification somehow shows, but I need users to see notification always, especially when their app is closed.
I am sending notification with high priority so this is not an issue.
Any help, what could be potential issue with this?
My request to FCM:
POST: https://fcm.googleapis.com/fcm/send
Authorization: key=xxxx_MY_KEY_XXXXX
Content-Type: application/json
{
"to": "xxxxxMY_FCM_TOKEN_xxxxxx",
"mutable_content": true,
"content_available": true,
"priority": "high",
"data": {
"content": {
"id": 106,
"channelKey": "remote_chann",
"title": "Hello",
"body": "test",
"notificationLayout": "Messaging"
}
}
}

As per your data send with fcm is sending only data object not notification object as per the fcm to wakeup it self you need to send notification object along with data object.
https://firebase.google.com/docs/cloud-messaging/concept-options

Related

android read FCM data from notificationManager.getActiveNotifications

I'm using Capacitor for my App, I have an use case similar to whatsapp, when users receive a new message if your app is in background, you receive FCM notification in the notification tray, later if user open that particular chat directly by opening the app(without clicking on the notification), i'd like to clear that notification from the notification tray.
so here's what I thought
get active notification
find the active chat notifications by filtering extra data sent by FCM
clear those notifications programatically
however android's notificationManager.getActiveNotifications doesn't seem to provide access to FCM data.
below is the payload i'm posting to FCM(i want to use chatId and type in data section to clear active notifications).
{
"android": {
"priority": "high"
},
"notification": {
"title": "you have a new message from XYZ",
"body": "hello"
},
"data": {
"messageId": "62971793-0111-4c9b-bbb2-bb78262a0543",
"chatId": "631e162f5f286c23d13e06d5",
"type": "CHAT_NEW_MESSAGE"
},
"token": "fcmToken"
}
Here's the capacitor plugins Android Logic to get the delivered/active notification.
how can I achieve above use case?

Flutter Huawei push by http

im trying to send push by http to Huawei Devices with Huawei consolé integration, but the documentation is confuse, and i'm not really sure how to build my body to send
I have this, but only works for foreground, if the app is in background or terminated, the push isn't working
This is the portar i'm following https://developer.huawei.com/consumer/es/doc/development/quickApp-Guides/quickapp-access-push-kit#dataDesc
{
"validate_only": false,
"message": {
"data": "{title: Hello! 1, description: Hello, shirley!, params: { key1: test1, key2: test2}, ringtone: {vibration: true, breathLight: true}}",
"android": {
"title": "Hello! 1",
"body": "Hello, shirley!",
"params": {
"key1": "test1",
"key2": "test2"
},
"click_action": {
"type": 3
}
},
"token": [
"IAAAAACy0irbAADqfnlY9pfEgoWWoIzgCBXQzPol8CTNzV6i3V5byP7cIhuU74vovAwysjkI_XMwowYj1sv5SrhXRi-sYXB5OrplLHEB02LO0pKHcA"
]
}
}
The conditions received by the Push notification do not need to be run in the foreground.
According to the Docs,You can send push messages in either of the following ways:
Select target users in AppGallery Connect. In this case, messages can be pushed to both quick apps on the live network and those running on a quick app loader.
Send push messages to a large number of users in batches by invoking server APIs. In this case, messages can be pushed to both quick apps on the live network and those running on a quick app loader.
Conditions for a quick app to receive push messages are as follows.

FCM how to display the push notification preview when receiving a message

I implemented the push notification on Android through Firebase, I have a problem that I haven't found an answer anywhere, I'm getting my push perfectly, but when I receive a notification with the application closed, the default sound is played, but the message does not slide on screen, if I go to the notifications my message is there, but it does not slide as soon as it arrives on the device is this a parameter of the firebase API or is it an implementation in the code? In the app's permissions, I'm allowed to slide messages. Has anyone come across this?
This is the body of the request for the legacy firebase api.
POST - https://fcm.googleapis.com/fcm/send
{
"notification": {
"body": "my body",
"title":"my title",
"sound": "default"
},
"data":{
"click_action": "",
"type": "",
"action":"",
"content":""
},
"priority": "high",
"to": "token device"
}

Custom data push message with Firebase with platform-specific notification issue

I have some issues with sending custom push message using Firebase Cloud Messaging with my data.
On iOS I have created Notification Service Provider that controlls every incoming notification but I had to add "notification" field with title and body in push request to Firebase. If there was no "notification" field then if app was turned off/killed then I couldn't receive message.
On Android I have my FirebaseMessagingService where I receive messages (and I can decide what to do with it) but if "notification" field is present in Firebase request then FirebaseMessagingService is not receiving message but notification shows automatically with title and body from "notification".
I've found some docs from Firebase about platform-specific messages
(When to use platform-specific keys) and I though to add notification only for iOS platform. I have tried few approches described in link above and here Creating the Remote Notification Payload / Payload Key Reference
Below some examples of JSON I've tried to send but I always get error InvalidParameters: Invalid notification payload (I've tried all possibilities with fields apns, payload, aps, alert)
{
"to": "xyz................", //single device with android or ios
"mutable_content": true,
"data": { "field1": "value1" [.......] },
"apns": {
"payload": {
"title": "Some message title",
"body": "Some message body",
"sound": "default"
}
}
or
{
"to": "xyz................", //single device with android or ios
"mutable_content": true,
"data": { "field1": "value1" [.......] },
"apns": {
"payload": {
"aps": {
"title": "Some message title",
"body": "Some message body",
"sound": "default"
}
}
}
etc.
The purpose is to receive data in my app and decide how to handle it - depending on incoming data I want to show my own notification or make some custom action in app.
Is it even possible to send platform-specific message to iOS with notification/alert and without it to Android? If so - how?

FCM notification with topic server side not received on client

I tried a sample code to test the push notification system with Firebase and it's working well except one thing.
If I try to send a notification from Firebase console, using Device Token, the notification shows in device.
If I try to send a notification from Firebase console, using my topic topik, all notifications show in all devices.
If I try to send a notification from my web page or from postman, using Device Token, the notification shows in device.
If I try to send a notification from my web page or from postman, using my topic topik, NOTHING HAPPENS.
This is a example call:
link: https://fcm.googleapis.com/fcm/send
POST method
Header field:
Content-Type : application/json
Authorization : key=MY_SERVER_KEY (the new one)
Body:
{
"to": "/topics/topik",
"data": {
"title": "This is a Firebase Cloud Messaging Topic Message!",
"content-text": "This is a Firebase Cloud Messaging Topic Message!"
}
}
or
Body:
{
"to": "/topics/topik",
"data": {
"message": "This is a Firebase Cloud Messaging Topic Message!"
}
}
the result on send action is something like this
{
"message_id": 7150560334538835864 (SUCCESS!)
}
but no notification arrives in any device. I tried to debug the onReceive method, but nothing happens.
Any idea?
Are you trying to send data-messages or notification-messages?
see: https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages
If you want to send notification-messages (the type of messages sent by the notification console)
the payload you wrote in the question is wrong. Try replacing data with notification:
Body:
{
"to": "/topics/topik",
"notification": {
"title": "Hello",
"body": "This is a Firebase Cloud Messaging Topic Message!"
}
}

Categories

Resources