Error while sending custom push notifications with onesignal in flutter

54 Views Asked by At

I am trying to send push notifications and for this I am using the Onesignal Rest API.

This is how my code looks

import 'dart:convert';
import 'package:http/http.dart' as http;

Future<void> sendPushNotifications(String message) async {
  final url = Uri.parse('https://api.onesignal.com/notifications');
  final headers = {
    'Content-Type': 'application/json',
    'Authorization': 'MY_API_KEY',
  };

  final body = {
    'app-id': 'MY_APP_ID',
    'contents': {'en': message},
  };

  final response =
      await http.post(url, headers: headers, body: jsonEncode(body));

  if (response.statusCode == 200) {
    print('Notification sent successfully');
  } else {
    print('Failed to send notification: ${response.body}');
  }
}

And then I have invoke this function when a button is pressed. Once the button is pressed, I get this error:

Failed to send notification: {"errors":["app_id not found. You may be missing a Content-Type: application/json header."]}

I want that a push notification should be sent as soon as the button gets clicked. Please help in understanding what am I missing.

These are the ones that I am using.

0

There are 0 best solutions below