FF & OneSignal Push Notifications Integration - Easy way without Firebase or Supabase

Feedback

Hi Folks,
If you are looking to use Onesignal to send push notification using your backend calls only without referring to Auth through Firebase or Supabase, here are the steps:

0- Get your JSON from google (service account after creating your App), get your p8 file also (for IOS).

1- Create an App on OneSignal using the JSON & the p8. Get you APP ID Key from Onesignal and go to FF.

2- Create a custom action on your App on FF with the following code:

import 'package:onesignal_flutter/onesignal_flutter.dart';

Future onesignal() async {
  //Remove this method to stop OneSignal Debugging
  OneSignal.Debug.setLogLevel(OSLogLevel.verbose);

  OneSignal.initialize("<Your App ID Key>");

  // The promptForPushNotificationsWithUserResponse function will show the iOS or Android push notification prompt. We recommend removing the following code and instead using an In-App Message to prompt for notification permission
  OneSignal.Notifications.requestPermission(true);
}

Add this to the dependency:

3- Create another custom action to collect the player_id (or subscription_id) when the user connects to your app:

import 'package:onesignal_flutter/onesignal_flutter.dart';

Future<String> getPlayerId() async {
  // Retrieve the push subscription ID using the new User Model.
  final playerId = OneSignal.User.pushSubscription.id;
  // Return an empty string if playerId is null, or return the playerId.
  return playerId ?? "";
}

4- Create an action that calls you custom action (called getPlayerId here) to get the playerId (or subscription_id) and insert it in your database in front of each user_id, it should be mapped with that.

5- When you want to send notifications from your backend, you'll need to call OneSignal API and pass the player_ids.

2
1 reply