send_push_notification.dart 940 B

12345678910111213141516171819202122232425262728293031323334353637
  1. /// Packages used:
  2. /// firebase_messaging
  3. /// http
  4. ///
  5. /// Send push notification to device by [token]
  6. ///
  7. /// Get your token:
  8. /// await FirebaseMessaging.instance.getToken();
  9. Future<void> testFirebaseMessaging(String token) async {
  10. log('Testing cloud messaging');
  11. Map<String, String> headers = {
  12. HttpHeaders.authorizationHeader:
  13. 'key = YOUR SERVER KEY',
  14. HttpHeaders.contentTypeHeader: "application/json"
  15. };
  16. Map<String, dynamic> msg = {'mesgType': 'ACTION', 'action_id': '4664'};
  17. Map<String, dynamic> notification = {
  18. 'title': 'Title',
  19. 'body': 'body text',
  20. 'sound': 'default'
  21. };
  22. Map<String, dynamic> args = {
  23. 'registration_ids': [token],
  24. 'data': msg,
  25. 'notification': notification
  26. };
  27. final res = await post(
  28. Uri.parse('https://fcm.googleapis.com/fcm/send'),
  29. headers: headers,
  30. body: json.encode(args),
  31. );
  32. log('Code: ${res.statusCode} Body: ${res.body}');
  33. }