I am doing fcm integration to flutter demo application.onMessage method is not called when data only message is sent through postman in any condition(forground,background,killed.) (ios physical device iphone5s).
I have tried notification +data message it is working properly. as described in firebase_messaging plugin.
{
"to" :"firebase-token",
"data" : {
"click_action": "FLUTTER_NOTIFICATION_CLICK",
"body" : {
"message":"this is data message"
},
"title" : "data title",
"content_available" : true,
"priority" : "high"
}
}
here is content of notification request of postman
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
Fimber.d("onMessage: $message");
Fluttertoast.showToast(msg: "onMessage: $message",
toastLength: Toast.LENGTH_LONG
);
},
onLaunch: (Map<String, dynamic> message) async {
Fimber.d("onLaunch: $message");
Fluttertoast.showToast(msg: "onLaunch: $message",
toastLength: Toast.LENGTH_LONG
);
},
onResume: (Map<String, dynamic> message) async {
Fimber.d("onResume: $message");
Fluttertoast.showToast(msg: "onResume: $message",
toastLength: Toast.LENGTH_LONG
);
},
);
_firebaseMessaging.requestNotificationPermissions(
const IosNotificationSettings(sound: true, badge: true, alert: true));
_firebaseMessaging.onIosSettingsRegistered
.listen((IosNotificationSettings settings) {
Fimber.d("Settings registered: $settings");
});
_firebaseMessaging.getToken().then((String token) {
Fimber.d("token: $token");
});
it is in init state of homepage.
i have permission notifications in ios and working well with notification+data.
-result should be as described in https://pub.dartlang.org/packages/firebase_messaging#-readme-tab-
-i have found work around from here
add notification and sound to json like this
{
"content_available" : true,
"priority" : "high",
"to" :"fcm-token",
"notification":{
"sound": ""
},
"data" : {
"click_action": "FLUTTER_NOTIFICATION_CLICK",
"body" : {
"message":"3:57pm"
},
"title" : "data title",
"content_available" : true,
"priority" : "normal"
}
}
Related
I used reactnative PushNotification/PushNotificationIOS modules , My issue is when notification received on foreground to my device IOS ,the function onNotification: function(notification) {} does not called directly is called by click only .
I want to fetch data notification paylaod sent via pububnub console directly without any user interaction ,this function is working fine in android but ios not all.
the console does not show anything can help me to solve this issue and thanks.
This the function and imports module:
import PushNotification from "react-native-push-notification";
import PushNotificationIOS from "#react-native-community/push-notification-ios";
onNotification: function(notification) {
if (Platform.OS=='ios')
{
console.log('notif',notification)
notification.finish(PushNotificationIOS.FetchResult.NoData);
},
onAction: function (notification) {
},
onRegistrationError: function(err) {
},
permissions: {
alert: true,
badge: true,
sound: true,
},
popInitialNotification: true,
requestPermissions: true,
senderID: FIREBASE_SENDER_ID,
})
}
and this the object pubnub to send :
{"pn_apns":{
"aps":{
"alert": {
"body": "Course disponible",
"title": "My course"
},
"sound": "beep.wav",
"data": { "reference": "ND1004332", "startstation": "" }
},
"pn_push":[
{
"push_type":"alert",
"auth_method":"token",
"targets":[
{
"environment":"development",
"topic":"com.test.fr"
}
],
"version":"v2"
}
]
}
}
import {Platform} from 'react-native';
import PushNotification from 'react-native-push-notification';
import PushNotificationIOS from "#react-native-community/push-notification-ios";
if (Platform.OS === 'ios') {
// Must be outside of any component LifeCycle (such as `componentDidMount`).
PushNotification.configure({
onNotification: function (notification) {
console.log("NOTIFICATION:", notification);
const { foreground, userInteraction, title, message } = notification;
if (foreground && (title || message) && !userInteraction) PushNotification.localNotification(notification);
notification.finish(PushNotificationIOS.FetchResult.NoData);
},
permissions: {
// alert: true,
// badge: true,
sound: true
},
});
}
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.
I am new to push notifications and I have this task to add a custom sound whenever the user receives a notification. I am not using flutter local notifications, just the normal firebase configurations and its working well. Can anyone help me add a custom sound for iOS. Any help would be greatly appreciated!
Here is the payload used and the firebase configurations.
options = {
"notification": {
"title": formatter.title,
"body": formatter.body,
"sound": "notification.caf",
"content_available": true,
},
"priority": "high",
"data": {
"badge": user.unopened_notification_count,
"body": formatter.body,
"title": formatter.title,
"id": notification.id,
"notifiable_type": notification.notifiable_type,
"notifiable_id": notification.notifiable_id,
"click_action": "FLUTTER_NOTIFICATION_CLICK"
}
}
also here is my firebase configuration:
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
if (Firebase.apps.isEmpty) await Firebase.initializeApp();
print('Handling a background message ${message.data['id']}');
}
handleNotifications() async {
FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(badge: false, alert: false, sound: true); //presentation options for Apple notifications when received in the foreground.
FirebaseMessaging.onMessage.listen((message) async {
print('Got a message whilst in the FOREGROUND!');
return;
}).onData((data) async {
print('Got a DATA message whilst in the FOREGROUND!');
print('data from stream: ${data.data['id']}');
});
FirebaseMessaging.onMessageOpenedApp.listen((message) async {
print('NOTIFICATION MESSAGE TAPPED');
return;
}).onData((data) async {
print('NOTIFICATION MESSAGE TAPPED');
print('data from stream: ${data.data}');
});
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
FirebaseMessaging.instance.getInitialMessage().then((value) => value != null ? _firebaseMessagingBackgroundHandler : false);
return;
}
attached image here is where I placed the sound file inside Xcode
I'm using fcm in my flutter project. I want to show an alertdialog when a notification is received from fcm, when the app is in foreground. It's working for android but not on iOS. Can anyone help me with that please?
here's my code snippet:
FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print("FCM: onMessage: " + message['notification']['body']);
//showNotification(message['notification']['title'], message['notification']['body']);
print("FCM: onMessage: $message");
setState(() {
return notification = true;
});
AwesomeDialog(
context: scaffoldKey.currentContext,
title: message['notification']['title'],
desc: message['notification']['body'],
btnOkOnPress: () {
Navigator.pop(scaffoldKey.currentState.context);
},
).show();
//notification sound
playSound();
},
onLaunch: (Map<String, dynamic> message) async {
print("FCM: onLaunch: $message");
},
onResume: (Map<String, dynamic> message) async {
print("FCM: onResume: $message");
},
);
_firebaseMessaging.requestNotificationPermissions(
const IosNotificationSettings(sound: true, badge: true, alert: true));
_firebaseMessaging.onIosSettingsRegistered
.listen((IosNotificationSettings settings) {
print("Settings registered: $settings");
});
FCM will not work well in foreground, you need to use others like, flutter_local_notifications
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