ios 推送通知 -didFinishLaunchingWithOptions
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20771312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Push Notification -didFinishLaunchingWithOptions
提问by cdsoft
When I send a push notification and my app is open or in the background and I click on the push notification, my application redirects to PushMessagesVc
viewController
(as intended)
当我发送推送通知并且我的应用程序处于打开状态或在后台并单击推送通知时,我的应用程序重定向到PushMessagesVc
viewController
(按预期)
I use the code as below for this:
为此,我使用以下代码:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
PushMessagesVc *pvc = [mainstoryboard instantiateViewControllerWithIdentifier:@"PushMessagesVc"];
[self.window.rootViewController presentViewController:pvc
animated:YES
completion:NULL];
}
There is no problem in the code/scenario above but if the application is closed and I click on a push notification, the application does not redirect my PushMessagesVc
viewController
in this case & the application stays on the main screen.
上面的代码/场景没有问题,但是如果应用程序关闭并且我单击推送通知,则PushMessagesVc
viewController
在这种情况下应用程序不会重定向我的应用程序并且应用程序停留在主屏幕上。
For the 2nd scenario, I use the following code:
对于第二个场景,我使用以下代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
sleep(1);
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeNone)];
[UIApplication sharedApplication].applicationIconBadgeNumber = 1;
NSDictionary *userInfo = [launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
if(apsInfo) {
UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
PushMessagesVc* pvc = [mainstoryboard instantiateViewControllerWithIdentifier:@"PushMessagesVc"];
[self.window.rootViewController presentViewController:pvc animated:YES completion:NULL];
return YES;
}
return YES;
}
But in this case, the PushMessagesVc
does not appear.
但在这种情况下,PushMessagesVc
不会出现。
回答by staticVoidMan
Since you only want to present a viewController
when you get a Push Notification, you may try utilizing NSNotificationCenter
for your purposes:
由于您只想viewController
在收到推送通知时显示 a ,因此您可以尝试使用NSNotificationCenter
:
Part 1: Set up a class (in your case, the rootViewController
) to listen/respond to a NSNotification
第 1 部分:设置一个类(在您的情况下是rootViewController
)来聆听/响应NSNotification
Suppose, MainMenuViewController
is the rootViewController
of your navigationController
.
Set up this class to listen to a NSNotification
:
假设,MainMenuViewController
是rootViewController
您的navigationController
.
设置这个类来听一个NSNotification
:
- (void)viewDidLoad {
//...
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(presentMyViewOnPushNotification)
name:@"HAS_PUSH_NOTIFICATION"
object:nil];
}
-(void)presentMyViewOnPushNotification {
//The following code is no longer in AppDelegate
//it should be in the rootViewController class (or wherever you want)
UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
PushMessagesVc *pvc = [mainstoryboard instantiateViewControllerWithIdentifier:@"PushMessagesVc"];
[self presentViewController:pvc animated:YES completion:nil];
//either presentViewController (above) or pushViewController (below)
//[self.navigationController pushViewController:pvc animated:YES];
}
Part 2: Post Notification (possible from anywhere in your code)
第 2 部分:发布通知(可以在代码中的任何位置)
In your case, AppDelegate.mmethods should look like:
在您的情况下,AppDelegate.m方法应如下所示:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//firstly, don't sleep the thread, it's pointless
//sleep(1); //remove this line
if (launchOptions) { //launchOptions is not nil
NSDictionary *userInfo = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
if (apsInfo) { //apsInfo is not nil
[self performSelector:@selector(postNotificationToPresentPushMessagesVC)
withObject:nil
afterDelay:1];
}
}
return YES;
}
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
//this method can be done using the notification as well
[self postNotificationToPresentPushMessagesVC];
}
-(void)postNotificationToPresentPushMessagesVC {
[[NSNotificationCenter defaultCenter] postNotificationName:@"HAS_PUSH_NOTIFICATION" object:nil];
}
PS: I haven't done this for my projects (yet) but it works and is the best way i could think of doing this kinda stuff (for the moment)
PS:我还没有为我的项目做过这个(还)但它有效,是我能想到的最好的方式来做这件事(目前)
回答by Hardik Thakkar
Swift 5
斯威夫特 5
To get push notification Dictionary in didFinishLaunchingWithOptionswhen app is kill and push notification receive and user click on that
当应用程序被终止并收到推送通知并用户单击它时,在didFinishLaunchingWithOptions 中获取推送通知字典
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if let userInfo = launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] as? [String: AnyObject] {
if let aps1 = userInfo["aps"] as? NSDictionary {
print(aps1)
}
}
return true
}
Push notification Dictionary will display in alert.
推送通知字典将显示在警报中。
回答by W?odzimierz Wo?niak
Swift 2.0 For 'Not Running' State (Local & Remote Notification)
Swift 2.0 用于“未运行”状态(本地和远程通知)
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Handle notification
if (launchOptions != nil) {
// For local Notification
if let localNotificationInfo = launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification {
if let something = localNotificationInfo.userInfo!["yourKey"] as? String {
self.window!.rootViewController = UINavigationController(rootViewController: YourController(yourMember: something))
}
} else
// For remote Notification
if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as! [NSObject : AnyObject]? {
if let something = remoteNotification["yourKey"] as? String {
self.window!.rootViewController = UINavigationController(rootViewController: YourController(yourMember: something))
}
}
}
return true
}
}
回答by Roman Barzyczak
Swift version:
迅捷版:
if let localNotification: UILocalNotification = launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification { //launchOptions is not nil
self.application(application, didReceiveLocalNotification: localNotification)
}
回答by Роман Мельник
Swift 5
斯威夫特 5
I write my notification to var "notification" in didFinishLaunchingWithOptions, and if "notification" != nil started my push in applicationDidBecomeActive, after that removed him ( = nil)
我将我的通知写入 didFinishLaunchingWithOptions 中的 var "notification",如果 "notification" != nil 开始在 applicationDidBecomeActive 中推送,然后删除他 (= nil)
class AppDelegate: UIResponder, UIApplicationDelegate {
var notification: [AnyHashable : Any]? = nil
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
registerForPushNotifications()
//write notification to var
if launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] != nil {
self.notification = launchOptions![UIApplication.LaunchOptionsKey.remoteNotification] as? [AnyHashable : Any]
}
return true
}
func applicationDidBecomeActive(_ application: UIApplication) {
if notification != nil {
//for launch notification after terminate app
NotificationCenter.default.post(name: Notification.Name(rawValue: "startPush"), object: nil)
notification = nil
}
}