Xamarin - 4.0 API Reference
The namespace for OneSignal SDK has been changed from
Com.OneSignal
toOneSignalSDK.Xamarin
Please remove any
using Com.OneSignal
orusing Com.OneSignal.Abstractions
statements and addusing OneSignalSDK.Xamarin
andusing OneSignalSDK.Xamarin.Core
to use the OneSignal SDK
Added
SMS
A new feature of Xamarin version 4.x.x is the ability to manage an SMS subscription.
Set the user's SMS Number
OneSignal.Default.SetSMSNumber("+12345556789");
Set the user's SMS Number and get the result of the call
var result = await OneSignal.Default.SetSMSNumber("+12345556789");
if (result) {
Debug.Log("success");
}
else {
Debug.Log("error");
}
Unlink the SMS subscription from the device so that it will no longer be updated
OneSignal.Default.LogoutSMS();
Unlink the SMS subscription from the device so that it will no longer be updated and get the result of the call
var result = await OneSignal.Default.LogoutSMS();
if (result) {
Debug.Log("success");
}
else {
Debug.Log("error");
}
Get the current SMS subscription status
var smsState = OneSignal.Default.SMSSubscriptionState;
var smsUserId = smsState.smsUserId;
var smsNumber = smsState.smsNumber;
Listen for SMS subscription status changes
OneSignal.Default.SMSSubscriptionStateChanged += (current, previous) => {
var smsSubscribed = current.isSubscribed;
};
Set Language
This new method allows you to set the language override for the device by passing in the 2-character, lowercase ISO 639-1 language codes.
var result = OneSignal.Default.SetLanguage("en");
if (result) {
Debug.Log("success");
}
else {
Debug.Log("error");
}
Prompt For Push Notification
This method prompts the user for notification permissions and provides the response received by the user's action. (iOS only)
var result = OneSignal.Default.PromptForPushNotificationsWithUserResponse();
Updated
Initialization
In version 4, we simplified initialization. OneSignal initialization now only requires that you set the application ID.
The property to OneSignal.Current
has been changed to OneSignal.Default
.
You can call Initialize
at any point in your app’s flow. This allows full initialization to be delayed until say, a user logs in.
3+ | 4+ |
---|---|
OneSignal.Current.StartInit("YOUR_ONESIGNAL_APP_ID") .Settings(new Dictionary<string, bool>() { { IOSSettings.kOSSettingsKeyAutoPrompt, false }, {IOSSettings.kOSSettingsKeyInAppLaunchURL,false }}) .InFocusDisplaying(OSInFocusDisplayOption.Notification) .EndInit(); | OneSignal.Default.Initialize("YOUR_ONESIGNAL_APP_ID") |
Debugging
Set the log and alert levels
3.x.x | 4.x.x |
---|---|
OneSignal.Current.SetLogLevel(LOG_LEVEL.VERBOSE, LOG_LEVEL.NONE); | OneSignal.Default.LogLevel = LogLevel.INFO; OneSignal.Default.AlertLevel = LogLevel.ERROR; |
Privacy
Set whether user consent is required to start the SDK
3.x.x | 4.x.x |
---|---|
OneSignal.Current.SetRequiresUserPrivacyConsent(true); | OneSignal.Default.RequiresPrivacyConsent = true; |
Set the status of the user's consent
3.x.x | 4.x.x |
---|---|
OneSignal.Current.UserDidProvidePrivacyConsent(true); | OneSignal.Default.PrivacyConsent = true; |
Get the status of the user's consent
3.x.x | 4.x.x |
---|---|
if (OneSignal.Current.RequiresUserPrivacyConsent()) { // user provided consent } | OneSignal.Default.PrivacyConsent = true; |
User Id
Set and remove the external user id using a result callback.
3.x.x | 4.x.x |
---|---|
OneSignal.Current.SetExternalUserId("YOUR_ONESIGNAL_EXTERNAL_USER_ID") | OneSignal.Default.SetExternalUserId("YOUR_ONESIGNAL_EXTERNAL_USER_ID") |
OneSignal.Current.SetExternalUserId("YOUR_ONESIGNAL_EXTERNAL_USER_ID", result => Debug.Log("success") ); | var result = await OneSignal.Default.SetExternalUserId("YOUR_ONESIGNAL_EXTERNAL_USER_ID"); if (result) { Debug.Log("success"); } |
OneSignal.Current.RemoveExternalUserId() | OneSignal.Default.RemoveExternalUserId() |
OneSignal.Current.RemoveExternalUserId( result => Debug.Log("success") ); | var result = await OneSignal.Default.RemoveExternalUserId(); if (result) { Debug.Log("success"); } |
State change listeners
Reference | Description |
---|---|
PermissionStateChanged | Handle native push permission changes |
PushSubscriptionStateChanged | Handle OneSignal push subscription state changes |
EmailSubscriptionStateChanged | Handle OneSignal email subscription state changes |
SMSSubscriptionStateChanged | Handle OneSignal SMS subscription state changes |
void Start() {
...
OneSignal.Default.PermissionStateChanged += _permissionStateChanged;
OneSignal.Default.PushSubscriptionStateChanged += _pushStateChanged;
OneSignal.Default.EmailSubscriptionStateChanged += _emailStateChanged;
OneSignal.Default.SMSSubscriptionStateChanged += _smsStateChanged;
}
private void _permissionStateChanged(PermissionState current, PermissionState previous) {
//Response to Permission State Change
}
private void _pushStateChanged(PushSubscriptionState current, PushSubscriptionState previous) {
//Response to Push State Change
}
private void _emailStateChanged(EmailSubscriptionState current, EmailSubscriptionState previous) {
//Response to Email State Change
}
private void _smsStateChanged(SMSSubscriptionState current, SMSSubscriptionState previous) {
//Response to SMS State Change
}
Push Notifications
Disabling push notifications without removing the push subscription
3.x.x | 4.x.x |
---|---|
OneSignal.Current.SetSubscription(false); | OneSignal.Default.PushEnabled = false; |
Clear all OneSignal notifications from the notification shade (Android only)
3.x.x | 4.x.x |
---|---|
OneSignal.Current.ClearAndroidOneSignalNotifications() | OneSignal.Default.ClearOneSignalNotifications() |
Get the push notification subscription status' ids
3.x.x | 4.x.x |
---|---|
OneSignal.Current.IdsAvailable((pushUserId, pushToken) => { // perform action with push info }); | var pushUserId = OneSignal.Default.PushSubscriptionState.userId; var pushToken = OneSignal.Default.PushSubscriptionState.pushToken; |
Sending a push to a user
3.x.x | 4.x.x |
---|---|
OneSignal.Current.IdsAvailable((pushUserId, pushToken) => { var notification = new Dictionary<string, object> { ["contents"] = new Dictionary<string, string> { ["en"] = "Test Message" }, ["include_player_ids"] = new List<string> { pushUserId }, ["send_after"] = DateTime.Now.ToUniversalTime().AddSeconds(30).ToString("U") }; OneSignal.Current.PostNotification(notification, response => Debug.Log("success"), error => Debug.Log("error") ); }); | var pushUserId = OneSignal.Default.PushSubscriptionState.userId; var pushOptions = new Dictionary<string, object> { ["contents"] = new Dictionary<string, string> { ["en"] = "Test Message" }, ["include_player_ids"] = new List<string> { pushUserId }, ["send_after"] = DateTime.Now.ToUniversalTime().AddSeconds(30).ToString("U") }; var result = await OneSignal.Default.PostNotification(pushOptions); if (result != null) { Debug.Log("success"); } else { Debug.Log("error"); } |
In App Messages
Setting and Removing trigger values
3.x.x | 4.x.x |
---|---|
OneSignal.Current.AddTrigger("triggerKey", 123); | OneSignal.Default.SetTrigger("triggerKey", 123); |
OneSignal.Current.AddTriggers(new Dictionary<string, object> { ["trigger1"] = "abc", ["trigger2"] = 456 }); | OneSignal.Default.SetTriggers(new Dictionary<string, object> { ["trigger1"] = "abc", ["trigger2"] = 456 }); |
OneSignal.Current.RemoveTriggerForKey("trigger3"); | OneSignal.Default.RemoveTrigger("trigger3"); |
Pause In-App messages
3.x.x | 4.x.x |
---|---|
OneSignal.Current.PauseInAppMessages(true) | OneSignal.Default.InAppMessagesArePaused = true; |
Handle In-App message when the user has triggered an action
3.x.x | 4.x.x |
---|---|
OneSignal.Current.StartInit("YOUR_ONESIGNAL_APP_ID") .HandleInAppMessageClicked((action) => { // Example IAM click handling for IAM elements Debug.WriteLine("HandledInAppMessageClicked: {0}", action.clickName); }) .EndInit(); | OneSignal.Default.InAppMessageTriggeredAction |
void Start() {
...
OneSignal.Default.InAppMessageTriggeredAction += _iamTriggeredAction;
}
private void _iamTriggeredAction(InAppMessageAction inAppMessageAction) {
//Response to IAM Triggered Action
}
In-App Message Lifecycle events
Set event methods for In app messages lifecycle events or when an In-app message trigger action is executed.
Reference | Description |
---|---|
InAppMessageWillDisplay | Handle In-App message when it is about to display |
InAppMessageDidDisplay | Handle In-App message when it displays |
InAppMessageWillDismiss | Handle In-App message when it is about to dismiss |
InAppMessageDidDismiss | Handle In-App message when it dismisses |
void Start() {
...
OneSignal.Default.InAppMessageWillDisplay += _iamWillDisplay;
OneSignal.Default.InAppMessageDidDisplay += _iamDidDisplay;
OneSignal.Default.InAppMessageWillDismiss += _iamWillDismiss;
OneSignal.Default.InAppMessageDidDismiss += _iamDidDismiss;
}
private void _iamWillDisplay(InAppMessage inAppMessage) {
//Response to IAM Will Display Event
}
private void _iamDidDisplay(InAppMessage inAppMessage) {
//Response to IAM Did Display Event
}
private void _iamWillDismiss(InAppMessage inAppMessage) {
//Response to IAM Will Dismiss Event
}
private void _iamDidDismiss(InAppMessage inAppMessage) {
//Response to IAM Did Dismiss Event
}
Email
Set a user's Email or logout of a set email including returning results
3.x.x | 4.x.x |
---|---|
OneSignal.Current.SetEmail("[email protected]"); | OneSignal.Default.SetEmail("[email protected]"); |
OneSignal.Current.SetEmail("[email protected]", () => Debug.Log("success"), error => Debug.Log("error") ); | var result = await OneSignal.Default.SetEmail("[email protected]"); if (result) { Debug.Log("success"); } else { Debug.Log("error"); } |
OneSignal.Current.LogoutEmail(); | OneSignal.Default.LogoutEmail(); |
OneSignal.Current.LogoutEmail( () => Debug.Log("success"), error => Debug.Log("error") ); | var result = await OneSignal.Default.LogoutEmail(); if (result) { Debug.Log("success"); } else { Debug.Log("error"); } |
Location
Set whether location sharing is enabled
3.x.x | 4.x.x |
---|---|
OneSignal.Current.SetLocationShared(true); | OneSignal.Default.ShareLocation = true; |
Prompt the user if they would like to share their location
3.x.x | 4.x.x |
---|---|
OneSignal.Current.PromptLocation(); | OneSignal.Default.PromptLocation(); |
Tags
Send, Get and Delete a tag or multiple tags
3.x.x | 4.x.x |
---|---|
OneSignal.Current.SendTag("tagName", "tagValue"); | OneSignal.Default.SendTag("tagName", "tagValue"); |
OneSignal.Current.SendTags(new Dictionary<string, string> { ["tag1"] = "123", ["tag2"] = "abc" }); | OneSignal.Default.SendTags(new Dictionary<string, string> { ["tag1"] = "123", ["tag2"] = "abc" }); |
OneSignal.Current.GetTags(tags => { var tag3Value = tags["tag3"]; }); | var tags = await OneSignal.Default.GetTags(); var tag3Value = tags["tag3"]; |
OneSignal.Current.DeleteTag("tag4"); | OneSignal.Default.DeleteTag("tag4"); |
OneSignal.Current.DeleteTags(new[] { "tag5", "tag6" }); | OneSignal.Default.DeleteTags(new[] { "tag5", "tag6" }); |
Outcomes
Send outcome, unique outcome, or outcome with a float value with result callbacks
3.x.x | 4.x.x |
---|---|
OneSignal.Current.SendOutcome("outcomeName"); | OneSignal.Default.SendOutcome("outcomeName"); |
OneSignal.Current.SendOutcome("outcomeName", result => Debug.Log("success")); | var result = await OneSignal.Default.SendOutcome("outcomeName"); if (result) { Debug.Log("success"); } |
OneSignal.Current.SendUniqueOutcome("uniqueOutcomeName"); | OneSignal.Default.SendUniqueOutcome("uniqueOutcomeName"); |
OneSignal.Current.SendUniqueOutcome("uniqueOutcomeName", result => Debug.Log("success")); | var result = await OneSignal.Default.SendUniqueOutcome("uniqueOutcomeName"); if (result) { Debug.Log("success"); } |
OneSignal.Current.SendOutcomeWithValue("outcomeWithVal", 4.2f); | OneSignal.Default.SendOutcomeWithValue("outcomeWithVal", 4.2f); |
OneSignal.Current.SendOutcomeWithValue("outcomeWithVal", 4.2f, result => Debug.Log("success")); | var result = await OneSignal.Default.SendOutcomeWithValue("outcomeWithVal", 4.2f); if (result) { Debug.Log("success"); } |
Lifecycle
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.
Reference | Description |
---|---|
NotificationOpened | Handle notifications when notifications are opened |
NotificationWillShow | handle notifications when notifications are received in the foreground |
void Start() {
...
OneSignal.Default.NotificationOpened += _notificationOpened;
OneSignal.Default.NotificationWillShow += _notificationReceived;
}
private void _notificationOpened(NotificationOpenedResult result) {
//Response to Notification Opened Result
}
private Notification _notificationReceived(Notification notification) {
//Response to Notification Received Event
return notification; // show the notification
}
Launch URLs in App
Set whether URLs embedded in push notification open within the app or in a browser
3.x.x | 4.x.x |
---|---|
OneSignal.Current.StartInit("YOUR_ONESIGNAL_APP_ID") .Settings(new Dictionary<string, bool> { { OneSignal.kOSSettingsInAppLaunchURL, true } }) .EndInit(); | OneSignal.Default.SetLaunchURLsInApp(true); |
Removed
3.x.x | 4.x.x |
---|---|
OneSignal.Current.inFocusDisplayType = OneSignal.OSInFocusDisplayOption.InAppAlert; // or OneSignal.Current.StartInit("your_app_id") .InFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification) .EndInit(); | Replaced by the feature of the NotificationWillShow event to determine if a notification will show or not. |
OneSignal.EnableVibrate(true); | REMOVED - Stopped working in Android 8+ due to a breaking change. To customize going forward, use Notification Categories (Channels). |
OneSignal.EnableSound(true); | REMOVED - Stopped working in Android 8+ due to a breaking change. To customize going forward, use Notification Categories (Channels). |
Updated over 2 years ago