I have multiple devices (both Android and IOS) subscribed to same topic (e.g. “all”/”unregistered”)
I am sending FCM notification through postman,
I am able to successfully able to receive notification on all devices, but when I add image url to notification all devices receive notification but some devices are showing notification image.
This is my Json for sending notification from postman
{
"to": "/topics/all",
"notification": {
"body": "Notification Body Content",
"title": "Notification title content",
"image": "https://images.pexels.com/photos/1108099/pexels-photo-1108099.jpeg",
"sound": "default"
},
"apns": {
"headers": {
"apns-priority": "10"
},
"payload": {
"aps": {
"content-available": 1,
"sound": "default",
"alert": {
"body": "Notification body",
"title": "Notification title"
}
}
}
},
"android": {
"priority": "high",
"notification": {
"sound": "default"
}
},
"data": {
"goToScreen":"",
"redirectionUrl":"",
"redirection": "1",
"redirectToExternalApp": "0",
"redirectInsideApp": "1"
}
}
below is the code that I used to handle notification
filename : notification_utils.dart
class FCMServices {
static RemoteMessage? initialMessage;
static String? messageId;
static FirebaseMessaging messaging = FirebaseMessaging.instance;
static Future<void> requestNotificationPermission() async {
// Request notification permission on IOS
if (GetPlatform.isIOS) {
NotificationSettings settings = await messaging.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);
if (settings.authorizationStatus == AuthorizationStatus.authorized) {
// print('User granted permission');
} else if (settings.authorizationStatus ==
AuthorizationStatus.provisional) {
// print('User granted provisional permission');
} else {
// print('User declined or has not accepted permission');
}
// print("### ${settings.authorizationStatus.toString()}");
} else {
// Request notification permission on Android
PermissionStatus notificationStatus =
await Permission.notification.request();
if (notificationStatus.isDenied) {
// Generating demo notification because to register notification channelID
// to display notification as a heads up notificaiton
LocalNotificationService.demoNotification();
}
}
}
static Future<void> getInitialMessage() async {
initialMessage = await messaging.getInitialMessage();
}
static void setupAppOpenListener() {
FirebaseMessaging.onMessageOpenedApp.listen(_handleMessage);
}
static void handleNotificationInForground() async {
FirebaseMessaging.onMessage.listen((message) {
if (message.notification != null) {
requestNotificationPermission();
LocalNotificationService.display(message);
}
});
}
static Future<void> setupInteractedMessage() async {
// If the message also contains a data property with a "goToScreen"
if (initialMessage != null) {
if (initialMessage!.messageId != messageId) {
messageId = initialMessage!.messageId;
await _handleMessage(initialMessage!);
}
initialMessage = null;
}
FirebaseMessaging.onMessageOpenedApp.listen(_handleMessage);
}
static void handleBackgroundMessages() {
FirebaseMessaging.onBackgroundMessage(_handleMessage);
}
// DESCRIPTION: Handeling remote message
static Future<void> _handleMessage(RemoteMessage message) async {
// This will handle rest of stuff
}
static void setupAppOpenListener() {
FirebaseMessaging.onMessageOpenedApp.listen(_handleMessage);
}
}
And here is the code I wrote in main.dart file
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
await FCMServices.getInitialMessage();
FCMServices.setupAppOpenListener();
//this is to setup device orientation of the app
await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
runApp(const Root());
}
this is code from backend
public function sendNotificationToAllUsers($notificationData){
$title = $notificationData['title'];
$message = $notificationData['message'];
$type = $notificationData['type'];
$process_type = $notificationData['process_type'];
$process_date = $notificationData['process_date'];
$process_time = $notificationData['process_time'];
$user_id = $notificationData['user_id'];
$product_id = $notificationData['product_id'];
$images = $notificationData['images'];
$notification = ['body' => $message, 'title' => $title, 'image' => $images, 'sound' => 'default'];
$notification1 = ['body' => $message, 'title' => $title, 'image' => $images];
$data = ['screen' => 2, 'product_id' => $product_id];
$apns = [
'headers' => ['apns-priority' => '10'],
'payload' => [
'aps' => [
'content-available' => 1,
'sound' => 'default',
'alert' => $notification1
]
]
];
$android = ['priority' => 'high', 'notification' => ['sound' => 'default']];
$fcmNotification = [
'to' => '/topics/all',
'notification' => $notification,
'apns'=>$apns,
'android'=>$android,
'data'=>$data
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->fcmUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fcmNotification));
$result = curl_exec($ch);
}
I am using
-
flutter version 3.13.0
-
dart version 3.1.0
-
firebase_messaging: ^14.7.10
-
firebase_core: ^2.24.2
-
Followed official firebase documentation
NOTE : I have code that subscribes to topic called “all” in my code which is not provided here, also I have all the required permissions to display notification on both android and IOS device
what Now is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.