Brief steps to integrate onesignal to flutter apps
Step 01 - Add onesignal package to pubspec.yaml
onesignal_flutter: ^2.6.1
Step 02 - Add the following code to the Line 01 of app > build.gradle file
//Need to put this in the headline for onesignal plugin
buildscript {
repositories {
// ...
maven { url 'https://plugins.gradle.org/m2/' } // Gradle Plugin Portal
}
dependencies {
// ...
// OneSignal-Gradle-Plugin
classpath 'gradle.plugin.com.onesignal:onesignal-gradle-plugin:[0.12.6, 0.99.99]'
}
}
apply plugin: 'com.onesignal.androidsdk.onesignal-gradle-plugin'
Step 03 - Add the following lines to after the activity tag of app > src > main > AndroidManifest.xml file
<intent-filter android:label="@string/app_name" android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- Accepts URIs that begin with YOUR_SCHEME://YOUR_HOST -->
<data
android:scheme="thinkyar"
android:host="open" />
</intent-filter>
Step 04 - Call the following function inside the initialise code of your flutter script
Future<void> _initPlatformState() async {
OneSignal.shared.setLogLevel(OSLogLevel.verbose, OSLogLevel.none);
OneSignal.shared.setRequiresUserPrivacyConsent(_requireConsent);
var settings = {OSiOSSettings.autoPrompt: false, OSiOSSettings.promptBeforeOpeningPushUrl: true};
OneSignal.shared.setNotificationReceivedHandler((OSNotification notification) {
_debugLabelString = "Received notification: \n${notification.jsonRepresentation().replaceAll("\\n", "\n")}";
});
OneSignal.shared.setNotificationOpenedHandler((OSNotificationOpenedResult result) {
_debugLabelString = "Opened notification: \n${result.notification.jsonRepresentation().replaceAll("\\n", "\n")}";
});
OneSignal.shared.setInAppMessageClickedHandler((OSInAppMessageAction action) {
_debugLabelString = "In App Message Clicked: \n${action.jsonRepresentation().replaceAll("\\n", "\n")}";
});
OneSignal.shared.setSubscriptionObserver((OSSubscriptionStateChanges changes) {
print("SUBSCRIPTION STATE CHANGED: ${changes.jsonRepresentation()}");
});
OneSignal.shared.setPermissionObserver((OSPermissionStateChanges changes) {
print("PERMISSION STATE CHANGED: ${changes.jsonRepresentation()}");
});
OneSignal.shared.setEmailSubscriptionObserver((OSEmailSubscriptionStateChanges changes) {
print("EMAIL SUBSCRIPTION STATE CHANGED ${changes.jsonRepresentation()}");
});
await OneSignal.shared.init(_oneSignalAppID, iOSSettings: settings);
OneSignal.shared.setInFocusDisplayType(OSNotificationDisplayType.notification);
bool requiresConsent = await OneSignal.shared.requiresUserPrivacyConsent();
_enableConsentButton = requiresConsent;
if(GlobalVariables.player != null) {
await Future.delayed(Duration(seconds: 2));
sendTags('name', GlobalVariables.player.name);
OneSignal.shared.setExternalUserId(GlobalVariables.player.myId);
var status = await OneSignal.shared.getPermissionSubscriptionState();
var oneSignalId = status.subscriptionStatus.userId;
//Set one signal id to the player
if(GlobalVariables.player.oneSignalId != oneSignalId)
PRPlayer.updateOneSignalId(oneSignalId);
}
// OneSignal.shared.setSubscription((enable))
// OneSignal.shared.getTags()
}
void sendTags(String key, String value) {
print("Sending tags");
OneSignal.shared.sendTag(key, value).then((response) {
print("Successfully sent tags with response: $response");
}).catchError((error) {
print("Encountered an error sending tags: $error");
});
}
Publishing for iOS
1 - Create a new distribution profile for one signal extension.
2 - Make sure the version of one signal profile and main application is the same
Comments
Post a Comment