关闭推送通知 ios
switch off push notifications ios
我对推送通知有疑问。
我有一个包含所有令牌设备的数据库。这是table.
的结构
ID (number)
APP_NAME (varchar)
APP_TOKEN (varchar)
ENABLE (bool)
如果用户进入设备设置并关闭我的应用程序通知,是否可以将字段启用更改为假?我的意思是,我在 wordpress 中有一个插件向我的数据库中注册的所有设备发送通知,我想知道用户是否 s 关闭通知以将字段 "enable" 到false 然后不发通知给他。
不幸的是,当从“设置”应用程序启用或禁用推送通知时,iOS 没有通知应用程序的委托回调。应用程序需要查询 UIApplication
属性 enabledRemoteNotificationTypes
例如在 applicationDidBecomeActive:
中,然后调用服务器以保存设置,就像您的情况 ENABLE = NO
.
然而,即使服务器在用户选择不收听时发送通知,iOS 也会忽略该通知。
您必须在应用程序启动时检查启用的通知标志。然后通过服务器调用相应地设置启用标志
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types == UIRemoteNotificationTypeNone) {
//set your flag to disabled
}
您可以查看所有类型的通知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.
更新后端的布尔值
我对推送通知有疑问。
我有一个包含所有令牌设备的数据库。这是table.
的结构ID (number)
APP_NAME (varchar)
APP_TOKEN (varchar)
ENABLE (bool)
如果用户进入设备设置并关闭我的应用程序通知,是否可以将字段启用更改为假?我的意思是,我在 wordpress 中有一个插件向我的数据库中注册的所有设备发送通知,我想知道用户是否 s 关闭通知以将字段 "enable" 到false 然后不发通知给他。
不幸的是,当从“设置”应用程序启用或禁用推送通知时,iOS 没有通知应用程序的委托回调。应用程序需要查询 UIApplication
属性 enabledRemoteNotificationTypes
例如在 applicationDidBecomeActive:
中,然后调用服务器以保存设置,就像您的情况 ENABLE = NO
.
然而,即使服务器在用户选择不收听时发送通知,iOS 也会忽略该通知。
您必须在应用程序启动时检查启用的通知标志。然后通过服务器调用相应地设置启用标志
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types == UIRemoteNotificationTypeNone) {
//set your flag to disabled
}
您可以查看所有类型的通知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.
更新后端的布尔值