iOS 10.0 *处理前台推送通知

问题描述 投票:3回答:2

如果我实现了在ios 10.0中显示推送通知的方法

@available(iOS 10.0, *)

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
    {
        completionHandler(UNNotificationPresentationOptions.alert)

    }

然后它会显示所有通知,所以,我的问题是如何防止显示特定的推送通知(如另一个deveice登录)我只处理该特定推送的代码

ios swift3 push-notification notifications apple-push-notifications
2个回答
1
投票

我的推送数据是

{
    "aps" : {
        "alert" : {
            "id" : 2091
        },
        "sound" : "chime.aiff"
    },
    "acme" : "foo"
}

我已经使用了id,因为我可以通过它的id分隔每个推送,并且基于id我可以决定天气在前景中显示通知。

谢谢@ Anbu.Karthik根据我的问题参考我们可以处理推送甚至用户没有点击通知

   @available(iOS 10.0, *)

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
    {
        print("Handle push from foreground")
        let userInfo:[AnyHashable:Any] =  notification.request.content.userInfo
        let aps:NSDictionary = (userInfo[AnyHashable("aps")] as? NSDictionary)!
        let alert:NSDictionary = (aps["alert"] as? NSDictionary)!
        let id = Int(alert["id"] as! Int)
        if id == your id
        {    
         //Handle push without prompting alert 
        }

        else
        {
          //Display alert
         completionHandler(UNNotificationPresentationOptions.alert)
        }
    }

当用户点击通知时调用以下方法...

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {

        print(userInfo)


    }

0
投票

根据官方文件:

//仅当应用程序位于前台时,才会在委托上调用该方法。如果未实现该方法或未及时调用处理程序,则不会显示通知。应用程序可以选择将通知显示为声音,徽章,警报和/或通知列表。该决定应基于通知中的信息是否对用户可见。

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0);

因此,要回答您的问题,如果您想阻止显示通知,请不要实现此方法或不要调用处理程序。

希望能帮助到你。

© www.soinside.com 2019 - 2024. All rights reserved.