0

I have a question about push notifications.

I have a database with all tokens devices. This is the structure of the table.

ID (number)

APP_NAME (varchar)

APP_TOKEN (varchar)

ENABLE (bool)

If the users go to the device settings and switch off my app notifications, there is anyway to change the field enable to false? I mean, I have a plugin in wordpress witch send notifications to all devices registered in my database and I want to know if an user switch off the notifications to put the field "enable" to false and then no send the notification to him.

Pablo Garcia
  • 361
  • 6
  • 23

3 Answers3

1

Unfortunately, there is no delegate callback from iOS that inform the app when push notification is enabled or disabled from Settings app. Application need to query the UIApplication property enabledRemoteNotificationTypes for example in applicationDidBecomeActive: and then make a server call to save the setting like in your case ENABLE = NO.

However, even if server sends the notification when user choose to not listen to it, iOS ignores the notification.

Abhinav
  • 37,684
  • 43
  • 191
  • 309
1

You have to check the enabled notification flags on application launch. Then set your enabled flag accordingly with a server call

UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types == UIRemoteNotificationTypeNone) {
     //set your flag to disabled
}
cekisakurek
  • 2,474
  • 2
  • 17
  • 28
1

You can check all types of notifications cf this question:

BOOL remoteNotificationsEnabled, noneEnabled,alertsEnabled, badgesEnabled, soundsEnabled = NO;

// iOS8+
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {

     remoteNotificationsEnabled = [UIApplication sharedApplication].isRegisteredForRemoteNotifications;

     UIUserNotificationSettings *userNotificationSettings = [UIApplication sharedApplication].currentUserNotificationSettings;

     noneEnabled = userNotificationSettings.types == UIUserNotificationTypeNone;
     alertsEnabled = userNotificationSettings.types & UIUserNotificationTypeAlert;
     badgesEnabled = userNotificationSettings.types & UIUserNotificationTypeBadge;
     soundsEnabled = userNotificationSettings.types & UIUserNotificationTypeSound;
}
else {  // iOS 7 and below
      UIRemoteNotificationType enabledRemoteNotificationTypes = [UIApplication sharedApplication].enabledRemoteNotificationTypes;

      noneEnabled = enabledRemoteNotificationTypes == UIRemoteNotificationTypeNone;
      alertsEnabled = enabledRemoteNotificationTypes & UIRemoteNotificationTypeAlert;
      badgesEnabled = enabledRemoteNotificationTypes & UIRemoteNotificationTypeBadge;
      soundsEnabled = enabledRemoteNotificationTypes & UIRemoteNotificationTypeSound;
}

Then depending of the results, update your boolean on your backend by making a call to your API.

Community
  • 1
  • 1
BoilingLime
  • 2,207
  • 3
  • 22
  • 37
  • There is anyway to do for iOS 7 or earlier? – Pablo Garcia Sep 23 '15 at 06:50
  • The problem now is that when the user disables the push notification i can't get the device token so i can't go to my table and update the value "enable" – Pablo Garcia Sep 23 '15 at 08:46
  • You should not identify your users by their device token. Don't you have another way to identify them ? id, email, anything ? – BoilingLime Sep 23 '15 at 08:55
  • Have a look here : http://stackoverflow.com/a/6993440/2086502 there is a way to have a unique identifier for a device. Just have a relationship in your database to link a unique id to the device token. – BoilingLime Sep 23 '15 at 08:58