关闭推送通知ios

时间:2015-09-22 12:40:38

标签: ios notifications

我对推送通知有疑问。

我有一个包含所有令牌设备的数据库。这是表格的结构。

ID (number)

APP_NAME (varchar)

APP_TOKEN (varchar)

ENABLE (bool)

如果用户转到设备设置并关闭我的应用通知,则无论如何都要将字段启用更改为false?我的意思是,我在wordpress中有一个插件向我的数据库中注册的所有设备发送通知,我想知道用户是否关闭通知以将字段“启用”设置为false,然后不向他发送通知。 / p>

3 个答案:

答案 0 :(得分:1)

不幸的是,从设置应用启用或禁用推送通知时,iOS没有代理回调通知应用。应用程序需要在UIApplication中查询enabledRemoteNotificationTypes属性applicationDidBecomeActive:,然后进行服务器调用以保存设置ENABLE = NO

但是,即使服务器在用户选择不收听时发送通知,iOS也会忽略该通知。

答案 1 :(得分:1)

您必须在应用程序启动时检查已启用的通知标志。然后通过服务器调用

相应地设置启用标志
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types == UIRemoteNotificationTypeNone) {
     //set your flag to disabled
}

答案 2 :(得分:1)

您可以查看所有类型的通知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;
}

然后根据结果,通过调用API来更新后端的布尔值。