Notification Preferences

Notification Preferences allow your users to make choices about the kind of push notifications they want to receive. Push notifications show up even when a user isn’t interacting with your app, so if the messages aren’t relevant your users will opt-out. Instead of turning off all notifications, users can choose ones they value. For example, a user may choose to receive notifications about score updates for his or her favorite sports team and opt out of other teams' updates.

Before you begin: Work with your marketing team to define which Notification Preferences to offer in your app, as described in the Design Mobile Apps for Responsys topic. For example, the preference for a favorite team may contain a concise name or abbreviation for that team. Another preference could denote whether a user wishes to receive news updates. A third preference could be used to inform when ticket sale prices have dropped to a specified threshold.

You may have as many as 30 preferences. Notification Preferences have 3 declaration fields:

Declaration Field Validation Rules
Key Valid key names for preferences may only contain alphanumeric characters and underscores, and must be between 1 and 25 characters in length. Key names are case-sensitive. Use unique, uppercase names for the preference key names. Responsys treats the preference key names as all uppercase.
Label Human-readable labels for your application to display. All unicode characters are valid for these labels, but they must be between 1 and 1024 characters in length.
preferenceType BOOLEAN, STRING, or NUMBER. Coordinate with the Responsys Account Admin regarding preferenceType (BOOLEAN, STRING, or NUMBER), so that the Responsys Account Admin can set up the correct data mapping in Responsys for the preferences fields.

Coding the Notification Preferences

Once you have decided upon your Notification Preferences for your app, you'll first need to declare them, then set default values for them using the PushIO SDK, per the sections below. Later, you'll want to create a user interface in your app to let the user modify their preferences.

How and what you display is up to you and the preferences you've defined. For example, you can tie a Boolean type Notification Preference to a UISwitch:

- (IBAction)switchAction:(id)sender
{
	UISwitch *mySwitch = (UISwitch *)sender;
if (mySwitch.on == YES)
{
   // When UISwitch is On, set News notification preference to Yes
   [[PushIOManager sharedInstance] setBoolPreference:YES forKey:@"NEWS"];
}
else
{
   // When UISwitch is Off, set News notification preference to No
   [[PushIOManager sharedInstance] setBoolPreference:NO forKey:@"NEWS"];
}
}				
@IBAction func switchAction(sender:UISwitch){
if sender.isOn{
PushIOManager.sharedInstance().setBoolPreference(true, forKey: "NEWS")
}else{
PushIOManager.sharedInstance().setBoolPreference(false, forKey: "NEWS")
}
}

DeclarePreference

Declare the preferences you wish to use at the start of each app session, because declared preferences are not stored persistently until values are set for them (as described in the next section).

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
   // ... 
   NSError *error = nil;
   [[PushIOManager sharedInstance] declarePreference:@"NEWS" label:@"News" type:PIOPreferenceTypeBoolean error:&error];
   if (error != nil) {
	   // Examine error
	}

   error = nil;
   [[PushIOManager sharedInstance] declarePreference:@"FAVORITE_TEAM" label:@"Favorite Team" type:PIOPreferenceTypeString error:&error];
   if (error != nil) {
	   // Examine error
	}

   error = nil;
   [[PushIOManager sharedInstance] declarePreference:@"PRICE_LIMIT" label:@"Price Limit" type:PIOPreferenceTypeNumeric error:&error];
   if (error != nil) {
	   // Examine error
	}
// ...
}				
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
//...
var error : NSError?
//Numeric preference type declaration
PushIOManager.sharedInstance().declarePreference("PRICE_LIMIT", label: "Price Limit", type: PIOPreferenceType.numeric, error: &error)
if let theError = error {
    print(theError.localizedDescription)
}

//String type preference declaration 
PushIOManager.sharedInstance().declarePreference("FAVORITE_TEAM", label: "Favorite Team", type: PIOPreferenceType.string, error: &error)
if let theError = error {
    print(theError.localizedDescription)
}


//Boolean type preference declaration
PushIOManager.sharedInstance().declarePreference("NEWS", label: "News", type: PIOPreferenceType.boolean, error: &error)
if let theError = error {
    print(theError.localizedDescription)
}
//...
}

SetPreference

After declaring Notification Preferences, you'll want to set values for these preferences. You might do this to set default preference values after the user's device is registered for the first time or when the user wishes to make changes to his or her preferences.

Declare the preferences before setting them, because setting preferences without declaring them first is ignored.

[[PushIOManager sharedInstance] setBoolPreference:YES forKey:@"NEWS"];
[[PushIOManager sharedInstance] setStringPreference:@"Colorado Rapids" forKey:@"FAVORITE_TEAM"];
[[PushIOManager sharedInstance] setNumberPreference:@(30.50) forKey:@"PRICE_LIMIT"];						
//Setting numeric preference value
PushIOManager.sharedInstance().setNumberPreference(30.50, forKey: "PRICE_LIMIT")
//Setting string preference value
PushIOManager.sharedInstance().setStringPreference("Colorado Rapids", forKey: "FAVORITE_TEAM")
//Setting boolean preference value
PushIOManager.sharedInstance().setBoolPreference(true, forKey: "NEWS")

GetPreference

Use this API to retrieve preferences that have been declared and/or set.

- (void)viewWillAppear {
	   [super viewWillAppear];
  // Get single preference
  PIOPreference *newsPreference = [[PushIOManager sharedInstance] getPreference:@"NEWS"];

  // Set UI switch based upon retrieved preference
  _newsSwitch.on = newsPreference;
}					
//Retrieve Specific preference
let preference =  PushIOManager.sharedInstance().getPreference("PRICE_LIMIT")	

GetPreferences

Use this API to get a list of all the Notification preferences that have previously been declared and/or set.

// Get all stored preferences
NSArray *allPreferences = [[PushIOManager sharedInstance] getPreferences];					
//Retrieve all preferences
let preferences = PushIOManager.sharedInstance().getPreferences()

ClearAllPreferences

Use this API to delete the Notification preferences that were declared and set earlier.

//Remove individual preference
NSError *error = nil;
[[PushIOManager sharedInstance] removePreference:@"PRICE_LIMIT" error:&error];
if(nil != error){
    NSLog(@"Unable to remove preference, reason: %@",error);
}
//Clear all preferences
[[PushIOManager sharedInstance] clearAllPreferences];					
//Remove individual preference
var error : NSError?
PushIOManager.sharedInstance().removePreference("PRICE_LIMIT", error: &error)
if let theError = error {
print("Unable to remove preference, reason:(theError.localizedDescription)")
}
//Clear all preferences
PushIOManager.sharedInstance().clearAllPreferences()