如何以编程方式检测ios推送通知权限/设置

时间:2018-02-05 20:00:45

标签: ios permissions notifications settings

在通知设置(设置 - >通知 - > AnyAppName)中,有5个项目,每个项目都有一个切换按钮,SoundsBadge App IconShow on Lock Screen,{{1 },Show in History

我正在使用Show as Banners来获取用户的设置并宣传相应的提醒。 它可能返回值[[[UIApplication sharedApplication] currentUserNotificationSettings] types]表示0~7SoundBadge的任意组合。问题是,我们是否能够检测到BannersShow on Lock Screen

的状态

此外,在设置页面的底部,theres是一个名为Show in History的{​​{1}}选项,它有三个选项:OPTIONSShow Previews和{{1} }。我们能够以编程方式为此设置用户吗?

1 个答案:

答案 0 :(得分:0)

您应该使用从iOS 10开始支持的UserNotifications框架。 这样您就可以通过getNotificationSettingsWithCompletionHandler: UNUserNotificationCenter函数检索UNNotificationSettings。 在Show in History中,您可以检查一些值:

  • notificationCenterSetting(Show on Lock Screen
  • lockScreenSetting(Show as Banners
  • alertSetting([[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { if(settings.authorizationStatus == UNAuthorizationStatusAuthorized) { //notifications are enabled for this app if(settings.notificationCenterSetting == UNNotificationSettingEnabled || settings.lockScreenSetting == UNNotificationSettingEnabled || (settings.alertSetting == UNNotificationSettingEnabled && settings.alertStyle != UNAlertStyleNone)) { //the user will be able to see the notifications (on the lock screen, in history and/or via banners) dispatch_async(dispatch_get_main_queue(), ^(){ //now for instance, register for remote notifications //execute this from the main queue [[UIApplication sharedApplication] registerForRemoteNotifications]; }); } else { //the user must change notification settings in order te receive notifications } } else { //request authorization [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) { if(granted) { dispatch_async(dispatch_get_main_queue(), ^(){ //now for instance, register for remote notifications //execute this from the main queue [[UIApplication sharedApplication] registerForRemoteNotifications]; }); } else { //user denied the authorization request //the user must change notification settings in order te receive notifications } } } }];
  • alertStyle(横幅的类型)

例如:

import tensorflow as tf
input_tensor = tf.placeholder(dtype=tf.float32, shape=[None, 16, 16, 3])
print(input_tensor.shape)

conv_filter = tf.get_variable('conv_filter', shape=[2, 2, 3, 6], dtype=tf.float32)
conv1 = tf.nn.conv2d(input_tensor, conv_filter, strides=[1, 2, 2, 1], padding='SAME')
print(conv1.shape)

deconv_filter = tf.get_variable('deconv_filter', shape=[2, 2, 6, 3], dtype=tf.float32)

deconv = tf.nn.conv2d_transpose(input_tensor, filter=deconv_filter,
    output_shape=tf.shape(input_tensor),
    strides=[1, 2, 2, 1],
    padding='SAME')
print(deconv.shape)

t = tf.reduce_mean(deconv)
g = tf.train.AdamOptimizer(0.01).minimize(t)