【问题标题】:How to disable/enable local notification in iOS?如何在 iOS 中禁用/启用本地通知?
【发布时间】:2014-03-12 12:24:04
【问题描述】:

在我的应用程序中,我保存了某些坐标并将它们设置为地理围栏,因此每次用户位置进入某个地理围栏时,我都会在 didEnterRegion 委托方法中放入一些代码,通知用户他/她进入了某个地理围栏。我正在使用 UILocalNotification 通知用户。

我担心的是,在我的应用程序“设置”视图中。我有一个用于启用/禁用通知的切换按钮。我怎么做?

谢谢!

【问题讨论】:

  • 在 nsuserdefault 中存储布尔值,如果是,则安排本地通知,否则不

标签: ios iphone objective-c cocoa-touch uilocalnotification


【解决方案1】:

UILocalNotification 的启用/禁用没有任何特定属性,因此我们无法轻松处理它。对于启用和禁用UILocalNotification(在您的情况下),

我只是把我的逻辑

if(toggleButtonEnable)
{
  // First remove all LocalNotifications with set it's BadgeNumber = 0 
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;

  // Then write code of UILocalNotification with it's fireDate
}
else
{
   // remove all LocalNotifications with set it's BadgeNumber = 0
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}

通过使用上述代码,每当if condition 变为true 时,首先删除/取消所有通知并设置badge = 0; 并创建新的UILocalNotification 和当if condition 变为 false 时,还删除/取消所有通知并设置 badge = 0;。所以你可以轻松处理(启用/禁用)UILocalNotification

【讨论】:

  • 在我的情况下,我想关闭特定用户 ID 的 LocalNotification。我有很多用户列表。请帮助我..
【解决方案2】:

无法启用/禁用本地通知。但是有一种方法可以做到这一点。

删除通知(禁用通知)

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"NotifyName" object:nil];

之后添加通知(启用通知)

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(abc:) name:@"NotifyName" object:nil];

【讨论】:

  • 本地通知不是NSNotifications
猜你喜欢
  • 2014-05-16
  • 2018-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多