简体   繁体   English

apns-如何提示用户仅在相关时启用推送通知?

[英]apns - how to prompt the user to enable push notification only when it becomes relevant?

Is there a way to prompt the user to enable push notifications when it actually makes sense? 有没有一种方法可以提示用户在实际有意义的情况下启用推送通知? Put another way, instead of prompting the user to enable push notifications as soon as the app is launched for the first time (with code in app delegate), delay the prompt to after a specific point in the app (post walkthrough for example). 换句话说,不是在第一次启动应用程序时提示用户启用推送通知(应用程序委托中带有代码),而是将提示延迟到应用程序中的特定位置之后(例如,演练)。

I have seen several apps that do that including Google Photos and Uber but I am not clear which piece of code actually triggers the on screen dialog and if I can place it anywhere in the app. 我已经看到了一些执行此操作的应用程序,包括Google Photos和Uber,但我不清楚是哪段代码真正触发了屏幕对话框,以及是否可以将其放置在应用程序中的任何位置。 I assume it is the following (in Swift): 我假设它是以下内容(在Swift中):

UIApplication.sharedApplication().registerForRemoteNotifications()
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)   
application.registerUserNotificationSettings(settings)

Put the following code anywhere in your app you think its right (ie other than in app delegate) to prompt the user to enable notifications: 将以下代码放在您认为合适的应用程序中的任何位置(即,不在应用程序委托中)以提示用户启用通知:

UIApplication.sharedApplication().registerForRemoteNotifications()
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)   
application.registerUserNotificationSettings(settings)

To handle the various scenarios you can follow this answer/thread. 要处理各种情况,您可以遵循以下答案/主题。

Actually this is the right code, which you can fire from wherever you want 实际上,这是正确的代码,您可以从任意位置触发

let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
UIApplication.sharedApplication().registerForRemoteNotifications()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM