Example: Ask Unsubscribed Devices to Resubscribe to Push

10471047

This example will explain how to ask unsubscribed users to resubscribe to push. If you want to ask iOS and Android devices to subscribe for the first time (recommended) see our Prompting for Push Permissions with an In-App Message 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.

10441044

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 segment, there is a button Click Action called Push Permission Prompt. This will show the native push permission prompt if it was not shown before, or it will present an alert to take the user to the App Settings to enable push notifications.

670670

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

505505

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)) {
        OneSignal.promptForPushNotifications(true);
     }
  }
}
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?