Flutter iOS notification send click_action - ios

We are developing with flutter for both android and iOS in a mobile application. We have been on a problem for about 2 months and we could not solve it. The problem is;
When we click on the notification we sent, no action is taken on the iOS side and a black screen appears. This system, which works smoothly on the Android side, does not work on the iOS side. After some research we got here: https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages
We made an edit according to this link, but the notifications still do not work properly on the iOS side.
This is the message code we sent.
const message = {
token: token,
notification: {
title: username,
body: lastMessage,
click_action: "FLUTTER_NOTIFICATION_CLICK",
},
data: {
specificPageRouteId: lastSender.toString(),
chatId: chatId.toString(),
notificationType: "chat",
messageType: messageType,
click_action: "FLUTTER_NOTIFICATION_CLICK",
},
};
There are not working, the above is also not sending the notification correctly. But the main problem is the click problem in the incoming notification.
const message = {
token: token,
notification: {
title: username,
body: lastMessage,
},
data: {
specificPageRouteId: lastSender.toString(),
chatId: chatId.toString(),
notificationType: "chat",
messageType: messageType,
click_action: "FLUTTER_NOTIFICATION_CLICK",
},
};
Is there anyone who can help us with this?

Yes we have resolve this issue. The problem was on the firebase side. It got fixed when we used the new API. I think the new one is not very stable.
admin.messaging().sendToDevice( tokens,
{
notification: {
title: title,
body: body,
},
data: {
click_action: "FLUTTER_NOTIFICATION_CLICK",
notificationType: "TYPE",
},
},
{
content_available: true,
priority: "high",
}
);
This code works.

Related

Flutter with Firebase Messaging. iOS device doesn't receive data only messages

I'm sending data-only message from Cloud functions
I tried this solution: https://github.com/FirebaseExtended/flutterfire/issues/3395#issuecomment-683973108
Here are the option sendings from Cloud Functions:
{
android: {
priority: "high",
},
apns: {
payload: {
aps: {
contentAvailable: true,
},
},
headers: {
"apns-push-type": "background",
"apns-priority": "5",
"apns-topic": "io.flutter.plugins.firebase.messaging", // bundle identifier
},
},
}
And here the code which handle messages. (I want to create a notification from the client not from the server)
const AndroidNotificationChannel channel = AndroidNotificationChannel(
'high_importance_channel',
'High Importance Notifications',
'This channel is used for important notifications.',
importance: Importance.max,
);
class BackgroundHandler {
static const IOSNotificationDetails iosNotificationDetails =
IOSNotificationDetails(subtitle: 'notification');
static final AndroidNotificationDetails androidNotificationDetails =
AndroidNotificationDetails(channel.id, channel.name, channel.description);
}
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print('It works');
print(message);
FlutterLocalNotificationsPlugin().show(0, 'notification', 'new notification', NotificationDetails(android: BackgroundHandler.androidNotificationDetails,
iOS: BackgroundHandler.iosNotificationDetails));
NotificationManager.handleNotificationData(message.data);
});
On Android it works perfectly and it works even with notification, but nothing happens with data only messages.

How to use apns-collapse-id with FCM?

I know this question have been asked many times but none of the answers work for me. Some of the questions dont even have an answer.
I am try to bundle or group similar notifications on ios. I am using FCM Cloud Functions to trigger notifications.
Following are the methods i tried by
const payload = {
notification: {
title: "Your medical history is updated" + Math.random(),
tag: "MedicalHistory",
},
data: {
click_action: "FLUTTER_NOTIFICATION_CLICK",
sound: "default",
status: "done",
},
};
const patchedPayload = Object.assign({}, payload, {
apns: {
headers: {
"apns-collapse-id": "MedicalHistory",
},
},
});
const options = {
priority: "high",
collapseKey: "MedicalHistory",
};
await admin
.messaging()
.sendToDevice("MY_TOKEN", patchedPayload, options);
The above code does not work
const payload = {
notification: {
title: "Your medical history is updated" + Math.random(),
tag: "MedicalHistory",
},
data: {
click_action: "FLUTTER_NOTIFICATION_CLICK",
sound: "default",
status: "done",
},
apns: {
headers: {
"apns-collapse-id": "MedicalHistory",
},
},
};
const options = {
priority: "high",
collapseKey: "MedicalHistory",
};
await admin
.messaging()
.sendToDevice("MY_TOKEN", payload, options);
This does not work as well.
I dont understand where to put the apns-collapse-id. The cloud functions sample also does not show this. Neither i could find doc with code on this
I recommend using the latest send function from the firebase-admin lib, usage described here.
It seems to work fine and somewhat easier to apply.
An example of a message with android/ios specific headers:
// common data for both platforms
const notification = {
title: 'You liked!',
body: 'You really liked something...',
}
const fcmToken = '...'
// android specific headers
const android = {
collapseKey: '0'
}
// ios specific headers
const apns = {
headers: {
"apns-collapse-id": '0'
}
}
// final message
const message = {
token: fcmToken,
notification: notification,
android: android,
apns: apns,
}
// send message
admin.messaging().send(message).catch(err => console.error(err));

cannot set the Sync document's ttl from a twilio function runtime client

I need my twilio sync documents to be deleted automatically after few minutes.
I followed the documentation for the Runtime.getSync(),
which by the way seems to be introduced here
https://www.twilio.com/docs/runtime/client and extended by the
"Documents" section here
https://www.twilio.com/docs/sync/api/documents using node.js examples,
it was so hard to piece it all toghether.
but I cannot get the ttl parameter to work
exports.handler = function(context, event, callback) {
let sync = Runtime.getSync();
let payload = {
'greeting': "bonbon!"
};
let uniqueName = "test4";
sync.documents.create({
uniqueName: uniqueName,
ttl: 5, //////////////////DOESN'T WORK!!!
data: payload
}).then(function(response) {
console.log(response);
callback(null, response);
});
}
the ttl parameter doesn't cause any effect on the expiration of the document and doesn't even cause the document's parameter "date_expires" (as seen in the https://www.twilio.com/docs/sync/api/documents ecample) to reflect the right value, in fact the date_expires parameter doesn't even appear in the returned JSON object:
{
_version: {
_domain: {
twilio: {
username: "xxxxxxxxxxxxxxxxxxxxxx",
password: "[Redacted]",
accountSid: "xxxxxxxxxxxxxxxxxxxxxx",
httpClient: { },
_sync: {
$ref: "$["_version"]["_domain"]"
}
},
baseUrl: "https://sync.twilio.com",
_v1: {
$ref: "$["_version"]"
}
},
_version: "v1"
},
sid: "xxxxxxxxxxxxxxxxxxxxxx",
uniqueName: "test4",
accountSid: "xxxxxxxxxxxxxxxxxxxxxx",
serviceSid: "xxxxxxxxxxxxxxxxxxxxxx",
url: "https://sync.twilio.com/v1/Services/xxxxxxxxxxxxxxxxxxxxxx/Documents/xxxxxxxxxxxxxxxxxxxxxx",
links: {
permissions: "https://sync.twilio.com/v1/Services/xxxxxxxxxxxxxxxxxxxxxx/Documents/xxxxxxxxxxxxxxxxxxxxxx/Permissions"
},
revision: "0",
data: {
greeting: "bonbon!"
},
dateCreated: "2018-09-19T03:30:24.000Z",
dateUpdated: "2018-09-19T03:30:24.000Z",
createdBy: "system",
_solution: {
serviceSid: "default",
sid: "xxxxxxxxxxxxxxxxxxxxxx"
}
}
is there anyone who has an explanation or a workaround?
Thank you very much in advance
Which version of twilio do you use?
https://www.twilio.com/console/runtime/functions/configure
If it is less than 3.6.10, ttl is not supported.
https://github.com/twilio/twilio-node/blob/master/CHANGES.md#2017-11-17-version-3100
[2017-11-17] Version 3.10.0
Sync
Add TTL support for Sync objects (breaking change)
The newest version is 3.21.0. Try it.

iOS background notifications using Cloud Functions

I have an issue getting data when app goes in background. I'm using Firebase Cloud functions and I think something I didn't code correctly in NodeJS. I have tried many examples and solutions like putting:
content_available: true and priority: 'high' but nothing seems to work. I have to note that:
Notifications are arriving in background and foreground but I can read data from notification in foreground but data is not fetched in background. So there is NO notifications data in background even it is displayed with sound and alert.
I think I'm doing something wrong in data payload so here is my NodeJS method:
var message = {
token: tokenID,
notification: {
title: 'ChatApplication',
body: `${myUsername} send you a message`,
},
android: {
ttl: 3600 * 1000,
notification: {
icon: 'default',
sound: 'default',
click_action: "com.yupi.chatapplication.messaging"
},
data: {
from_user: user_id,
title: 'ChatApplication',
body: `${myUsername} send you a message`
},
},
apns: {
payload: {
aps: {
alert: {
title: 'ChatApplication',
body: `${myUsername} send you a message`
},
badge: 1,
sound: 'default',
content_available: true,
mutable_content: true,
priority: 'high'
},
},
},
};
return admin.messaging().send(message)
.then((response) => {
// Response is a message ID string.
return console.log('Successfully sent message:', response);
})
.catch((error) => {
return console.log('Error sending message:', error);
});
The right field names for the Node.js SDK should be contentAvailable and mutableContent: https://firebase.google.com/docs/reference/admin/node/admin.messaging.Aps

FCM Notification in iOS doesn't play sound when received

I am using Firebase push notifications in my iOS Application. Although I am able to send the notification by sending below payload, it doesn't play a sound when received.
{
"to": "myToken",
"notification": {
"body": "test",
"title": "test"
},
"priority": "high"
"sound": "default"
}
If I send the test message from console, it works well and plays notification sound. Note:
My Authorization code is correct
I am sending http request to https://fcm.googleapis.com/fcm/send
I have tested it on IPhone 4 , IPhone 6 and IPhone 6S, All recieve
notifications without sound
your JSON "sound" : "default" should be inside the "notification" key not in the root of the JSON. This JSON should work.
{
"to": "myToken",
"notification": {
"body": "test",
"title": "test",
"sound": "default"
},
"priority": "high"
}
When using the FCM admin SDK, you have to specify sounds separately for Android and Apple devices:
let message = {
notification: {
'body': 'This is the message the user sees',
},
data: {
'param1': 'specify some extra data here',
},
// Apple specific settings
apns: {
headers: {
'apns-priority': '10',
},
payload: {
aps: {
sound: 'default',
}
},
},
android: {
priority: 'high',
notification: {
sound: 'default',
}
},
token: 'target FCM token goes here',
};
(Note: I've only tested the Apple settings thus far)
payload = {
notification:{
title: 'SOLO has been changed by an administrator',
body: 'Administrator changed your SOLO schedule',
},
android: {
},
apns: {
headers:{
"apns-collapse-id": "solo_changed_administrator",
"content-available": "1",
"apns-priority": "10",
},
payload:{
aps:{
sound: 'default',
badge: 12213123223
}
}
},
data:{
type: 'type'
}
}
https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages?authuser=0#ApnsConfig
I had the same problem. When notifications come through FCM to iOS not working sounds or vibrations. Here I followed this link: https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/PayloadKeyReference.html#//apple_ref/doc/uid/TP40008194-CH17-SW1
And finally went successful. I created my apple notification as an alert in my payload and it works for me. Here is my JSON below to get some idea about my solution.
"apns": {
"payload": {
"aps" : {
"alert" : {
"body": "Notification body",
"title": "Notification title"
},
"badge" : 2,
"sound" : "default"
}
}
}
Note: Please put this "apns" key in the relevant place of your message request. I was using REST call for requesting message.
Please refer the following link to get a good idea of how to send a notification message with platform-specific delivery options.
Link: https://firebase.google.com/docs/cloud-messaging/concept-options#example-notification-message-with-platform-specific-delivery-options

Resources