Ahmed Majid
Ahmed Majid

Follow

Ahmed Majid

Follow
Using onesignal push notifications without backend server

Using onesignal push notifications without backend server

Ahmed Majid's photo
Ahmed Majid
·Feb 12, 2023·

1 min read

Hello,

You can use onesignal service to send push notifications without using an external server or backend server directly from the mobile app for example, I know it's not the best solution but you can manage to make it work in small apps.

the code below is an example of flutter code:

sendAdNotification() async {
     OneSignal.shared.getPermissionSubscriptionState();
     OneSignal.shared.getDeviceState().then((deviceState) {
       print("OneSignal: device state: ${deviceState?.userId}");
     });

     var sendData = await post(
       Uri.parse('https://onesignal.com/api/v1/notifications'),
       headers: <String, String>{
         'Content-Type': 'application/json; charset=UTF-8',
         'Authorization': 'Basic RESTAPIKEY',
       },
       body: jsonEncode(<String, dynamic>
       {
         "app_id": "APPID",//kAppId is the App Id that one get from the OneSignal When the application is registered.

         "filters": [
           {"field": "tag", "key": "KeyNAme", "relation": "=", "value": Id},

         ],
         "android_accent_color":"FF9976D2",

         "small_icon":"ic_stat_onesignal_default",

         "large_icon":"biglogo.png",

         "headings": {"en": "New Ad"},

         "contents": {"en": titleTextEditingController.text},


       }),
     );

     debugPrint(sendData.body.toString());
   }
 
Share this