0

I have an application that I made with Flutter. I am trying to write a method for users to add each other as friends in my application, but before I do this, I want to send push notifications from one device to the other. Actually, I think if I start, I can solve the rest with my own algorithm.

Ways I've tried:

  • installed node.js
  • from project terminal: firebase login
  • firebase init
  • functions file created and exists in my project
  • i have index.ts file
  • I get unique token for each device when app opened.

I want to put a simple notification sending code in the index.ts file, but I couldn't. And this notification should work from one device to another.

1 Answers1

1

Here is simple solution to send device to device notification. First create json formatted parameters like below

    var params = {
  "to": "device token",
  "notification": {
  "title": "Notification Title",
  "body": "Notification Message",
  "sound": "default",
  },
  "data": {
    "customId": "01",
    "badge": 0,
    "alert": "Alert"
  }
};

Then var url = 'https://fcm.googleapis.com/fcm/send'; call as api. Get response like below code

    var response = await http.post(url,
  headers: {
    "Authorization": "key= Web server Key over here",
    "Content-Type": "application/json"
  },body: json.encode(params)
);

if (response.statusCode == 200) {
  Map<String, dynamic> map = json.decode(response.body);
  print("fcm.google: "+map.toString());
}
else {
  Map<String, dynamic> error = jsonDecode(response.body);
  print("fcm.google: "+error.toString());
}