简体   繁体   English

iOS推送通知-单击应用程序图标而不是通知时如何获取通知数据

[英]iOS Push Notification - How to get the notification data when you click on the app icon instead of notification

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 in applicationDidBecomeActive 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则将触发didReceiveRemoteNotification ,但如果单击原始应用程序图标,则仅触发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.) (两种情况都假设该应用程序处于后台并且尚未被杀死。)

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. 如果推送数据对于应用程序的使用很重要,请在应用程序启动后从服务器加载。

@fannheyward answer is absolutely correct. @fannheyward的答案是绝对正确的。 You cannot get payload when application is launched by tapping app icon. 点击应用程序图标启动应用程序时,您将无法获得有效负载。

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. 您可以在每个此类通知中设置“徽章”,在applicationDidBecomeActive上可以检查[application applicationIconBadgeNumber]> 0以了解某些通知处于活动状态。 After fetching payload from your server you can set it to 0 like below 从服务器获取有效负载后,可以将其设置为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. 我不确定用户从设置中禁用徽章时的行为。

If your application target is over iOS7, you can do only if application is alive in backgroud. 如果您的应用程序目标是通过iOS7进行的,则只有在backgroud中还存在应用程序时您才可以执行操作。

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. 为此,我还必须选中背景提取框。

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   
    }

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

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