代码之家  ›  专栏  ›  技术社区  ›  Andrey Chernukha

是否可以检测远程通知添加到通知中心的时刻?

  •  0
  • Andrey Chernukha  · 技术社区  · 7 年前

    有两种方法 UNUserNotificationCenterDelegate 与通知传递相关的协议。但两者似乎都不能满足我的需要。

    -(void)userNotificationCenter:(UNUserNotificationCenter *)center
      willPresentNotification:(UNNotification *)notification
        withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
    

    在发送通知之前调用。如果您要求通知中心向您提供所有已送达的通知,则新的通知将丢失。

    - (void)userNotificationCenter:(UNUserNotificationCenter *)center
    didReceiveNotificationResponse:(UNNotificationResponse *)response
         withCompletionHandler:(void(^)(void))completionHandler
    

    仅当用户对通知提供某些响应时才调用。也有好老的 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 但这是不赞成的。

    那么,在通知中心添加通知之后,是否有可能立即得到通知呢?

    2 回复  |  直到 7 年前
        1
  •  2
  •   Cristik    7 年前

    您可以添加 Notification Content Extension ,当接收到推送通知时将调用此扩展,目的是允许该扩展在将通知呈现给用户之前增强通知。( wwdc session on advanced notifications )

    这样,您就有机会在通知到达时执行一些代码。您不必实际修改通知。

    请注意,代码将在应用程序扩展的上下文中执行,因此如果要在应用程序中执行某些内容,可能需要更多的工作。

        2
  •  1
  •   Glenn Posadas    7 年前

    你想要的是:

    从通知中心删除通知。但事实并非如此 那里

    如果您试图抑制或阻止推送通知的显示。那么这是不可能的,至于iOS11.0的原因,苹果没有提及。

    但是,如果你想移除一个推力, UNUserNotificationCenter 您可以使用以下方法:

    // Notification requests that are waiting for their trigger to fire
    open func getPendingNotificationRequests(completionHandler: @escaping ([UNNotificationRequest]) -> Void)
    
    open func removePendingNotificationRequests(withIdentifiers identifiers: [String])
    
    open func removeAllPendingNotificationRequests()
    
    
    // Notifications that have been delivered and remain in Notification Center. Notifications triggered by location cannot be retrieved, but can be removed.
    open func getDeliveredNotifications(completionHandler: @escaping ([UNNotification]) -> Void)
    
    open func removeDeliveredNotifications(withIdentifiers identifiers: [String])
    
    open func removeAllDeliveredNotifications()