Navigate back to the menu at Window > OneSignal SDK Setup to see if there are any remaining steps to run
Namespace
You will notice that previous uses of OneSignal no longer can be found. In any file you are using the OneSignal Unity SDK please add to the top of the file:
We also now include a prefab for codeless initialization!
Located in the com.onesignal.unity.core package we've include a simple prefab which initializes OneSignal. You can easily find it using the Asset search bar to find OneSignalController.prefab and making sure to select All or In Packages for your search option. Drag the prefab into your very first scene, fill in the App Id, and you are immediately ready to go!
Set whether user consent is required to start the SDK
2.x.x
3.x.x
OneSignal.SetRequiresUserPrivacyConsent(true); // before init
// or
OneSignal.StartInit("your_app_id")
.SetRequiresUserPrivacyConsent(true)
.EndInit();
OneSignal.Default.RequiresPrivacyConsent = true; // before init
Set the status of the user's consent
2.x.x
3.x.x
OneSignal.UserDidProvideConsent(true);
OneSignal.Default.PrivacyConsent = true;
Get the status of the user's consent
2.x.x
3.x.x
if (OneSignal.UserProvidedConsent()) {
// user provided consent
}
if (OneSignal.Default.PrivacyConsent) {
// user provided consent
}
Set the external user id and get the result of the call
2.x.x
3.x.x
OneSignal.SetExternalUserId("3983ad1b-e31d-4df8-b063-85785ee34aa4",
result => Debug.Log("success")
);
var result = await OneSignal.Default.SetExternalUserId("3983ad1b-e31d-4df8-b063-85785ee34aa4");
if (result) {
Debug.Log("success");
}
Remove the external user id
2.x.x
3.x.x
OneSignal.RemoveExternalUserId();
OneSignal.Default.RemoveExternalUserId();
Remove the external user id and get the result of the call
2.x.x
3.x.x
OneSignal.RemoveExternalUserId(
result => Debug.Log("success")
);
var result = await OneSignal.Default.RemoveExternalUserId();
if (result) {
Debug.Log("success");
}
Push Notifications
Prompt the user for permission to send push notifications
2.x.x
3.x.x
OneSignal.PromptForPushNotificationsWithUserResponse(response => {
if (response) {
// user accepted
}
});
var response = await OneSignal.Default.PromptForPushNotificationsWithUserResponse();
if (response == NotificationPermission.Authorized) {
// user accepted
}
Getting the current push notification permission status
2.x.x
3.x.x
var currentDeviceState = OneSignal.GetPermissionSubscriptionState();
var currentStatus = currentDeviceState.permissionStatus.status;
if (currentDeviceState.permissionStatus.hasPrompted == false) {
// do if user was not prompted
}
var currentStatus = OneSignal.Default.NotificationPermission;
if (currentStatus == NotificationPermission.NotDetermined) {
// do if user was not prompted
}
Listen for push notification permission status changes
2.x.x
3.x.x
OneSignal.permissionObserver += changes => {
var previousStatus = changes.from.status;
var currentStatus = changes.to.status;
if (changes.to.hasPrompted) {
// do if user was prompted
}
};
OneSignal.Default.NotificationPermissionChanged += (current, previous) => {
if (current != NotificationPermission.NotDetermined) {
// do if user was prompted
}
};
Get the current push notification subscription status
2.x.x
3.x.x
var currentDeviceState = OneSignal.GetPermissionSubscriptionState();
var pushUserId = currentDeviceState.subscriptionStatus.userId;
var pushIsSubscribed = currentDeviceState.subscriptionStatus.subscribed;
var pushState = OneSignal.Default.PushSubscriptionState;
var pushUserId = pushState.userId;
var pushIsSubscribed = pushState.isSubscribed;
Listen for push notification subscription status changes
2.x.x
3.x.x
OneSignal.subscriptionObserver += changes => {
var prevPushState = changes.from;
var currPushState = changes.to;
var pushToken = currPushState.pushToken;
var pushEnabled = currPushState.userSubscriptionSetting;
};
OneSignal.Default.PushSubscriptionStateChanged += (current, previous) => {
var pushToken = current.pushToken;
var pushEnabled = !current.isPushDisabled;
};
Disabling push notifications without removing the push subscription
2.x.x
3.x.x
OneSignal.SetSubscription(false);
OneSignal.Default.PushEnabled = false;
Clear all OneSignal notifications from the notification shade
2.x.x
3.x.x
OneSignal.ClearOneSignalNotifications();
OneSignal.Default.ClearOneSignalNotifications();
Get the push notification subscription status' ids
2.x.x
3.x.x
OneSignal.IdsAvailable((pushUserId, pushToken) => {
// perform action with push info
});
var pushUserId = OneSignal.Default.PushSubscriptionState.userId;
var pushToken = OneSignal.Default.PushSubscriptionState.pushToken;
var result = await OneSignal.Default.LogoutEmail();
if (result) {
Debug.Log("success");
}
else {
Debug.Log("error");
}
Get the current Email subscription status
2.x.x
3.x.x
var currentDeviceState = OneSignal.GetPermissionSubscriptionState();
var emailUserId = currentDeviceState.emailSubscriptionStatus.emailUserId;
var emailAddress = currentDeviceState.emailSubscriptionStatus.emailAddress;
var emailState = OneSignal.Default.EmailSubscriptionState;
var emailUserId = emailState.emailUserId;
var emailAddress = emailState.emailAddress;
Listen for Email subscription status changes
2.x.x
3.x.x
OneSignal.emailSubscriptionObserver += changes => {
var prevEmailState = changes.from;
var currEmailState = changes.to;
var emailSubscribed = currEmailState.subscribed;
};
Previously setting up the callbacks for checking the 3 supported lifecycle methods had to be done exclusively during initialization. These are now available to be subscribed to at any time and several new lifecycle methods have been added.
Listen for when a push notification opened the app
2.x.x
3.x.x
OneSignal.StartInit("your_app_id")
.HandleNotificationOpened(onNotificationOpened)
.EndInit();
void onNotificationOpened(OSNotificationOpenedResult result) {
var notif = result.notification;
var action = result.action;
}
OneSignal.Default.NotificationOpened += onNotificationOpened;
void onNotificationOpened(NotificationOpenedResult result) {
var notif = result.notification;
var action = result.action;
}
Listen for when a push notification is about to display while the app is in focus
OneSignal.Default.NotificationWillShow += onNotificationWillShow;
Notification onNotificationWillShow(Notification notification) {
if (someCheck) {
return null; // don't show the notificaiton
}
// COMING SOON - make modifications to the notification before showing
return notification; // show the notification
}
Listen for when an action of an In-App Message was triggered by a button click