xcode iOS 推送通知 - 单击应用程序图标而不是通知时如何获取通知数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12084015/
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
iOS Push Notification - How to get the notification data when you click on the app icon instead of notification
提问by Howard
Similar to this question: How do I access remote push notification data on applicationDidBecomeActive?
类似于这个问题:如何访问 applicationDidBecomeActive 上的远程推送通知数据?
But the different is how can you access the notification data when you are inapplicationDidBecomeActive
and if you have clicked on the app icon instead of the push notification.
但不同的是,当您进入时applicationDidBecomeActive
,如果您点击了应用程序图标而不是推送通知,您如何访问通知数据。
The flow is: If you click on the push notification
then didReceiveRemoteNotification
will be triggered, but if you click on the original app icon, only applicationDidBecomeActive
will be triggered and didReceiveRemoteNotification
will not be called.
流程是:如果点击push notification
thendidReceiveRemoteNotification
会被触发,但是如果点击原来的app图标,只会applicationDidBecomeActive
被触发,didReceiveRemoteNotification
不会被调用。
I am looking for the later case so how can I access the push notification data.
我正在寻找后一种情况,所以我如何访问推送通知数据。
(Both case assuming the app is in background and not killed yet.)
(这两种情况都假设应用程序在后台并且尚未终止。)
回答by fannheyward
You can't get remote push payload by launching app from homescreen.
您无法通过从主屏幕启动应用程序来获取远程推送有效负载。
If the push data is important for app use, load it from your server after app launched.
如果推送数据对应用程序使用很重要,请在应用程序启动后从您的服务器加载它。
回答by msk
@fannheyward answer is absolutely correct. You cannot get payload when application is launched by tapping app icon.
@fannheyward 答案是绝对正确的。通过点击应用程序图标启动应用程序时,您无法获得有效负载。
I have an idea, what if you get to know that some notification is pending when app is launched by tapping app icon. With this knowledge your app can fetch payload from your server.
我有一个想法,如果您通过点击应用程序图标来知道当应用程序启动时某些通知处于待处理状态,该怎么办。有了这些知识,您的应用程序可以从您的服务器获取有效负载。
You can set "Badge" in every such notification and on applicationDidBecomeActive you can check [application applicationIconBadgeNumber] > 0 to know that some notification is active. After fetching payload from your server you can set it to 0 like below
您可以在每个此类通知中设置“徽章”,并且在 applicationDidBecomeActive 上,您可以检查 [application applicationIconBadgeNumber] > 0 以了解某些通知处于活动状态。从您的服务器获取有效负载后,您可以将其设置为 0,如下所示
[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
Please note: This means your app will have badge displayed over it when notification is received. I am not sure of the behaviour when badge is disabled by user from settings.
请注意:这意味着您的应用程序在收到通知时会显示徽章。我不确定用户从设置中禁用徽章时的行为。
回答by kasajei
If your application target is over iOS7, you can do only if application is alive in backgroud.
如果您的应用程序目标超过 iOS7,则只有当应用程序在后台处于活动状态时才能执行此操作。
In the capabilities settings at Xcode, you should enable Background Modes>Remote notifications, and write below code.
在 Xcode 的功能设置中,您应该启用后台模式>远程通知,并编写以下代码。
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
// save userInfo in NSUserDefaults
completionHandler( UIBackgroundFetchResultNoData );
}
If you want to test it, it will be helpful to use https://github.com/acoomans/SimulatorRemoteNotifications
如果你想测试它,使用https://github.com/acoomans/SimulatorRemoteNotifications会很有帮助
- From the server side, make sure to set content-available property with a value of 1
- 在服务器端,确保将 content-available 属性设置为 1
For this to work I also had to check the background fetch box.
为此,我还必须检查后台提取框。
回答by Rafael Jimeno
You should get the notification in the launchWithOptions method in your appDelegate something like this:
您应该在 appDelegate 的 launchWithOptions 方法中收到通知,如下所示:
NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
if(remoteNotif != nil){
//Handle your notification
}