如何在点击通知中打开特定帖子 [英] How to open particular post on the click on push notification
问题描述
在我的应用程序中,我实现了推送通知.当我的应用程序处于运行状态并且如果推送通知到来时,我将使用此代码进行处理.
in my app i implemented push notification. when my app is in running state and if push notification come i am handle that with this code.
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
print(userInfo)
myid = (userInfo["id"] as? String)!
print(myid)
if let notification = userInfo["aps"] as? NSDictionary,
let alert = notification["alert"] as? String {
var alertCtrl = UIAlertController(title: "Notification", message: alert as String, preferredStyle: UIAlertControllerStyle.Alert)
alertCtrl.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
// Find the presented VC...
var presentedVC = self.window?.rootViewController
while (presentedVC!.presentedViewController != nil) {
presentedVC = presentedVC!.presentedViewController
}
presentedVC!.presentViewController(alertCtrl, animated: true, completion: nil)
}
我想要什么::当我的应用程序未运行并且如果推送通知到时,我想基于该通知帖子ID打开垂直帖子.那我该怎么办呢? 这是我的代码
What i want :: when my app is not running and if push notification come i want to open perticular post based on that notification post id. so how can i do this? Here is my code
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
SVProgressHUD.setDefaultMaskType(SVProgressHUDMaskType.Black)
UIApplication.sharedApplication().applicationIconBadgeNumber = 0
let dicTemp = launchOptions?["UIApplicationLaunchOptionsRemoteNotificationKey"]
if dicTemp != nil{
window = UIWindow(frame: UIScreen.mainScreen().bounds)
storyboard = UIStoryboard(name: "Main", bundle: nil)
myid = (dicTemp["id"] as? String)!
let controller:pushnotificationpostViewController = self.storyboard.instantiateViewControllerWithIdentifier("pushnotificationpostViewController") as! pushnotificationpostViewController
navigation = UINavigationController(rootViewController: controller)
window?.rootViewController = navigation
window?.makeKeyAndVisible()
}
else
{
window = UIWindow(frame: UIScreen.mainScreen().bounds)
storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller:MainViewController = self.storyboard.instantiateViewControllerWithIdentifier("MainViewController") as! MainViewController
navigation = UINavigationController(rootViewController: controller)
window?.rootViewController = navigation
window?.makeKeyAndVisible()
}
//print(NSUserDefaults.standardUserDefaults().valueForKey("pushnotify")as! Bool)
let notificationTypes: UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound]
let pushNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)
application.registerUserNotificationSettings(pushNotificationSettings)
if (NSUserDefaults.standardUserDefaults().valueForKey("pushnotify")) != nil
{
pushnotification = NSUserDefaults.standardUserDefaults().valueForKey("pushnotify") as! Bool
// let notificationcheck = NSUserDefaults.standardUserDefaults().valueForKey("pushnotify") as! Bool
if (pushnotification == true)
{
application.registerForRemoteNotifications()
}
else
{
application.unregisterForRemoteNotifications()
}
}
else
{
application.registerForRemoteNotifications()
}
return true
}
但是通过此代码,我没有获得id,意味着myid变为nil.那我该怎么办呢?
but by this code i am not getting id means myid is getting nil. so how can i do this?
推荐答案
我认为当您退出申请时, 在您单击时未调用didReceiveRemoteNotification方法 通知,而是使用didFinishLaunchingWithOptions方法 被叫到那里,您必须检查天气应用程序是否已启动 通知
i think when you quit your application the didReceiveRemoteNotification method not gets called when you tap on the notification, instead the method didFinishLaunchingWithOptions gets called there you have to check weather application is launched by notification
将以下代码放在didFinishLaunchingWithOptions中:
Put below code in your didFinishLaunchingWithOptions :
var notification: UILocalNotification = (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] as! UILocalNotification)
if notification != nil {
// handle your notification
}
如果您使用本地通知,则上面的代码用于处理推送通知,然后尝试:
above code is for handling the push notifications if your using local notification then try:
// if launched by the local notification
var notification: UILocalNotification = (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey] as! UILocalNotification)
if notification != nil {
}
这篇关于如何在点击通知中打开特定帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!