Answers (3)
Here are the steps for onesignal setup in our application:
1.Set-Up your OneSignal Account
- Click on onesignal.com, then Login or Create a new account.
- Click on New App/Website and give a name to our application, then for android devices, select Google Android, and for ios devices, select Apple iOS.
- Click Next: Configure Your Platform.
- Right now, we are working with android devices, so for that we need to enter Firebase Server Key and Server ID, and we will get it by creating a new project on Firebase Console.
- So log in to Firebase Console and click on Add Project, and Add the project name. Also turn off Enable Google Analytics for this project Click on Create Project and click Continue.
- We will now be redirected to the project dashboard.
- Now here, click on the Setting icon next to Project Overview and click on Project Settings from the menu. And Select the Cloud Messaging tab.
- In the Cloud Messaging tab, you will find Firebase Server Key and Server ID, So copy these values and paste them to your OneSignal Account under OneSignal Google Android(FCM) Configuration.
- Under Cloud Messaging tab, If Firebase Cloud Messaging API (V1) is disabled, then click the kebab menu icon on the top right corner and open the link and click enable.
- Generate a Private Key JSON file: In Project settings, go to the Service accounts tab, Click Generate new private key at the bottom of the page. We'll then see a warning window, Click the Generate key and Save the JSON file somewhere secure.
- In OneSignal, navigate to Settings > Platforms > Google Android > Activate.
- Click on Service Account JSON > Choose file and select the JSON file we downloaded from your service account.
- Click Save and Continue.
- Select the SDK we are using for our app development and click Save & Continue. (Select our target SDK section: Choose Flutter)
- Click Save and Continue and then click Done.
- Finally, check that we can copy our App ID and check for subscribed users before clicking Save & Continue to complete our setup.
- Now we will be able to see our app dashboard under Settings>Platforms tab on OneSignal.
- On this page, under Native App Platforms, we should see an Active tag next to Google Android which means that we are ready to send notifications to users using the Android version of our Flutter App.
- If we follow the iOS Setup, we will see the Active tag next to Apple iOS.
2. Creating a Flutter App
- Create a new flutter project and add “onesignal_flutter” dependency to pubspec.yaml.
onesignal_flutter: ^3.5.1
- Then Add SDK initialization code,
void main() async {
// OneSignal App Id
String oneSignalAppId = 'Put your One Signal App ID Here';;
OneSignal.shared.setAppId(oneSignalAppId);
OneSignal.shared
.promptUserForPushNotificationPermission()
.then((accepted) {});
}
- In our project level(android/build.gradle) build.gradle file, add the following line of code:
buildscript {
ext.kotlin_version = '1.7.10'
repositories {
maven { url 'https://plugins.gradle.org/m2/' } // Add this line
}
dependencies {
// OneSignal-Gradle-Plugin // Add this line
classpath 'gradle.plugin.com.onesignal:onesignal-gradle-plugin:[0.12.6, 0.99.99]'
}
}
- In our app level(android/app/build.gradle) build.gradle file, add the following line of code:
apply plugin: 'com.android.application' // this already exists
apply plugin: 'com.onesignal.androidsdk.onesignal-gradle-plugin' // Add this line
3. Send a New Push Message
- Next, on the Dashboard section of our application's OneSignal account, do the following:
- Click on the New Push button.
- Enter the title and message of our notification.
- Click send to test device.
- We should be redirected to a page of all users so we can add a test user.
- We should find our device there, if not, run the app again. Click on Options, and add our device as a test user.
- Go back and click on Send to test device. Select our device and send.
- We’ve successfully sent our first notification!
There is an official Flutter package!
Add it as a dependency to your pubspec.yaml
file:
dependencies:
onesignal_flutter: ^2.6.2 # Verify that this is the latest version
Full setup instructions can be found here.
EDIT:
There is an official plugin now for OneSignal
https://github.com/OneSignal/OneSignal-Flutter-SDK
Unfortunately, there is no plugin for flutter to use OneSignal. You can write one yourself, though.
Since OneSignal exists for iOS and Android, what you have to do is write a communication between flutter and respective platforms.
If you decide to just implement the features for yourself, even pure platform channels can be viable.
Flutter plugin is just a wrapper to extract usage of platform channel that are platform dependent into a library.
IF you are using IntelliJ, after starting with plugin template on new project, in Tools -> Flutter you have an option to open the native iOS/Android code in Xcode/Android Studio. That should help to start things up.
