IOS 10如何处理用户点击推送通知
Posted
技术标签:
【中文标题】IOS 10如何处理用户点击推送通知【英文标题】:IOS 10 how to handle user clicking on push notification 【发布时间】:2017-08-10 20:33:37 【问题描述】:我已经设法让推送通知正常工作并使用 FireBase 转到正确的设备。但是,如果用户单击推送通知,我希望他们所有人现在都转到名为 "NotificationC" 的特定控制器,如果用户单击推送通知,则会转到名为 的控制器>HomeC 我不想要的。我已阅读Firebase push notification action,但仍然无法将其发送到该控制器,这是我的代码。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert], completionHandler: (granted, error) in
if (granted)
UIApplication.shared.registerForRemoteNotifications()
Messaging.messaging().delegate = self as? MessagingDelegate
else
//Do stuff if unsuccessful...
)
application.registerForRemoteNotifications()
NotificationCenter.default.addObserver(self, selector:
#selector(tokenRefreshNotification), name: NSNotification.Name.InstanceIDTokenRefresh, object: nil)
FirebaseApp.configure()
return true
func tokenRefreshNotification(notification: NSNotification)
// NOTE: It can be nil here
let login = LoginC()
login.getDeviceToken(id: MYID)
newDeviceToken = true
// The callback to handle data message received via FCM for devices running ios 10 or above.
func applicationReceivedRemoteMessage(_ remoteMessage: MessagingRemoteMessage)
let homeC = HomeC()
homeC.getMyNotifcations()
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
print("User Info = ",notification.request.content.userInfo)
completionHandler([.alert, .badge, .sound])
//Called to let your app know which action was selected by the user for a given notification.
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
let homeC = HomeC()
homeC.getMyNotifcations()
completionHandler()
上面的代码
let homeC = HomeC()
homeC.getMyNotifcations()
本质上是一个segue,它应该让用户访问我的通知控制器,这就是我完成它的方式
func getMyNotifcations()
let Popup = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NotificationC") as! NotificationC
Popup.notificationCount = Int(notificationCount!)
self.addChildViewController(Popup)
Popup.view.frame = self.view.frame
self.view.addSubview(Popup.view)
Popup.didMove(toParentViewController: self)
当我将 getMyNotifcations 放入 IBAction 时,它可以工作。同样,我只是希望用户在点击通知时转到 NotificationC 控制器,任何建议都会很棒。我还在 AppDelegate 中的所有这些函数上设置了一个断点,它们都没有在点击时触发。
【问题讨论】:
尝试延迟然后调用 homeC.getMyNotifcations() 像: DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.5, execute: homeC.getMyNotifcations() )跨度> 我只是这样做了,但得到了相同的结果 检查更新的答案 【参考方案1】:您的视图控制器似乎没有正确实例化,您只是在制作类的对象。
试试这个,也许还会增加延迟。
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let homeC = storyboard.instantiateViewController(withIdentifier: "YOUR_VIEWCONTROLLER_IDENTIFIER_IN_STORYBOARD") as? HomeC
if homeC != nil
homeC!.view.frame = (self.window!.frame)
self.window!.addSubview(homeC!.view)
self.window!.bringSubview(toFront: homeC!.view)
homeC.getMyNotifcations()
【讨论】:
完美,我刚刚输入了那个代码,它工作正常,非常感谢以上是关于IOS 10如何处理用户点击推送通知的主要内容,如果未能解决你的问题,请参考以下文章
分组推送通知再次调用 MainActivity 的 OnCreate()。如何处理?
如何处理 Xamarin Forms 中推送通知中的点击事件?