how to Send push notifications from Flutter app

I use firebase messaging to push notifications.

Now i push notifications from console to users on app, but I want the user to push notifications from app to another user when send message. How can I do that?

Knowing that I have saved the device ID of each user on Firestore and the ID is renewed every time the user logs into the application.

I tried using the following code to send notifications in case the user sends a message, but it didn't work

sendNotification(String body, String idToken) async {
    await http.post(
      (Uri.parse('https://fcm.googleapis.com/fcm/send')),
      headers: <String, String>{
        'Content-Type': 'application/json',
        'Authorization': 'key=$servitoken',
      },
      body: jsonEncode(
        <String, dynamic>{
          'notification': <String, dynamic>{
            'body': "${body.toString()}",
            'title': "!"
          },
          'priority': 'high',
          'data': <String, dynamic>{
            'click_action': 'FLUTTER_NOTIFICATION_CLICK',
            'id': '1',
            'status': 'done'
          },
          'to': await "${idToken}",
        },
      ),
    );
  }

,##

Upvotes: 0

Views: 648

Answers (1)

Zarif Sadman
Zarif Sadman

Reputation: 11

I will recommend you to use onesignal instead of just using firebase cloud messaging. Onesignal is easy to use and can make your development life more

Upvotes: 0

Related Questions