Example: Ask Unsubscribed Devices to Resubscribe to Push

This example will explain how to ask unsubscribed users to resubscribe to push. If you want to ask iOS devices to subscribe for the first time (recommended) see iOS Push Opt-In Prompt guide.

Step 1. Select the Audience

For both Android and iOS, you would need to duplicate this template and create 2 segments:
Segment 1: Device Type is Android
Segment 2: Device Type is iOS

Step 2 Unsubscribed Device Trigger

Using the OneSignal SDK getDeviceState() method, you can check if the current user is subscribed or not. Then with the addTrigger method, you can then trigger the in-app message once the user is detected to be unsubscribed.

//OneSignal Android SDK 4.0.0+
OSDeviceState device = OneSignal.getDeviceState();
boolean subscribed = device.isSubscribed();
if (!subscribed) {
  OneSignal.addTrigger("unsubscribed", "true");
}

//OneSignal Android SDK 3.15.6-
OSPermissionSubscriptionState status = OneSignal.getPermissionSubscriptionState();

if (!status.getSubscriptionStatus().getSubscribed()) {
  OneSignal.addTrigger("unsubscribed", "true");
}
//OneSignal iOS SDK 3.0.0+
if let deviceState = OneSignal.getDeviceState() {
    let subscribed = deviceState.isSubscribed
    if subscribed == false {
      OneSignal.addTrigger("unsubscribed", withValue: "true")
    }
}

//OneSignal iOS SDK 2.16.2-
let status: OSPermissionSubscriptionState = OneSignal.getPermissionSubscriptionState()
let isSubscribed = status.subscriptionStatus.subscribed
print("isSubscribed = \(isSubscribed)")
if isSubscribed == false {
    OneSignal.addTrigger("unsubscribed", withValue: "true")
}

Set the trigger to be the unsubscribed is true.

Step 3 Create the message with custom action

OneSignal provides a default Push Permission Prompt template which you can customize or create your own.

iOS Click Action for your iOS segment, there is a button Click Action: Push Permission Prompt (iOS). This will show the native iOS prompt if not shown before or it will present an alert to take the user to the App Settings to enable push.

Android Click Action for your Android segment, you can add a Custom Action ID. This will be available within the InAppMessageClickHandler method when the user clicks the button.

The action button example here is prompt_user

Based on the handler name, you can then send the user to the App Settings to resubscribe.
This is handled automatically with the iOS Push Permission Prompt Action, but code available if you want to handle it programmatically.

class ExampleInAppMessageClickHandler implements OneSignal.InAppMessageClickHandler {
  // Example of an action id you could setup on the dashboard when creating the In App Message
  private static final String ACTION_ID_MY_CUSTOM_ID = "prompt_user";

  @Override
  public void inAppMessageClicked(OSInAppMessageAction result) {
     if (ACTION_ID_MY_CUSTOM_ID.equals(result.clickName)) {
        Log.i("OneSignalExample", "Custom Action took place! Starting YourActivity!");
        //example from https://stackoverflow.com/questions/32366649/any-way-to-link-to-the-android-notification-settings-for-my-app
        intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");

        //for Android 5-7
        intent.putExtra("app_package", getPackageName());
        intent.putExtra("app_uid", getApplicationInfo().uid);

        // for Android 8 and above
        intent.putExtra("android.provider.extra.APP_PACKAGE", getPackageName());

        startActivity(intent);
     }
  }
}
let handler: OSHandleInAppMessageActionClickBlock = { action in
    var message: String? = nil
    if let clickName = action?.clickName, let clickUrl = action?.clickUrl, let firstClick = action?.firstClick, let closesMessage = action?.closesMessage {
        message = String(format: "Click Action Occurred: clickName:%@ clickUrl:%@ firstClick:%i closesMessage:%i", clickName, clickUrl as CVarArg, firstClick, closesMessage)
        print(message ?? "no message")
    }
    // NOT REQUIRED IF USING iOS Push Permission Prompt Action
    if clickName === "prompt_user" {
      OneSignal.presentAppSettings()
    }
}

OneSignal.setInAppMessageClickHandler(handler)

Schedule the message to start showing immediately and confirm.


Did this page help you?