[英]iOS 10.0 * Handle Push notification in foreground
If i implement the method to present push notification in ios 10.0 如果我实现了在ios 10.0中显示推送通知的方法
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
completionHandler(UNNotificationPresentationOptions.alert)
}
then it present all the notification , So, my question is how can i prevent to show particular push notification like (login at another deveice ) i just handle code for that particular push only 然后它会显示所有通知,所以,我的问题是如何防止显示特定的推送通知(如另一个deveice登录)我只处理该特定推送的代码
My push data is 我的推送数据是
{
"aps" : {
"alert" : {
"id" : 2091
},
"sound" : "chime.aiff"
},
"acme" : "foo"
}
I have used id because i can separate each push by its id, and based on id i can decide weather to show notification in foreground or not.. 我已经使用了id,因为我可以通过它的id分隔每个推送,并且基于id我可以决定天气在前景中显示通知。
Thanks @Anbu.Karthik For reference as per my question we can handle push even user did't tap on notification in 谢谢@ 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)
}
}
And When user tap on notification Following method is called... 当用户点击通知时调用以下方法...
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
print(userInfo)
}
As per the official documentation: 根据官方文件:
// The method will be called on the delegate only if the application is in the foreground.
//仅当应用程序位于前台时,才会在委托上调用该方法。 If the method is not implemented or the handler is not called in a timely manner then the notification will not be presented.
如果未实现该方法或未及时调用处理程序,则不会显示通知。 The application can choose to have the notification presented as a sound, badge, alert and/or in the notification list.
应用程序可以选择将通知显示为声音,徽章,警报和/或通知列表。 This decision should be based on whether the information in the notification is otherwise visible to the user.
该决定应基于通知中的信息是否对用户可见。
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0);
So to answer your question, if you want to prevent the notification from being shown, either do not implement this method or do not call the handler. 因此,要回答您的问题,如果您想阻止显示通知,请不要实现此方法或不要调用处理程序。
Hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.