简体   繁体   English

通过单击应用程序图标直接启动应用程序时,无法获取通知数据

[英]Push notification data not getting when app launched directly by clicking app icon

I have a scenario in which app will get push notification and need to show that messages in home screen of my app, for that i saved the message array into user defaults from my app delegate and everything works well, but in following conditions it's not working 我有一个场景,其中应用程序将获得推送通知,并需要在我的应用程序的主屏幕中显示消息,因为我将消息数组保存到我的应用程序委托的用户默认值,一切正常,但在以下条件下它不起作用

  1. if app is in killed state and a notification came and user triggeres the app through the app icon (not from push notification ) 如果应用程序处于被杀死状态并且发出通知并且用户通过应用程序图标触发应用程序(而不是来自推送通知)

  2. if app is in background and notification came and user enters to app through app icon (not from push message ) in this case also 如果应用程序在后台并且通知来了,用户也通过应用程序图标(不是来自推送消息)进入应用程序

Then i searched for solutions and came to know about silent push notifications (for background mode) and nothing else so i need to know how to handle all scenarios by push notifications and my appdelegete is 然后我搜索了解决方案,并了解了静音推送通知(用于后台模式),没有别的,所以我需要知道如何通过推送通知处理所有场景,我的appdelegete是

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];


    if (remoteNotif) {

        [self handlePushMessage:remoteNotif];
    }
    return YES;
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{

    [self handlePushMessage:userInfo];

}

-(void)handlePushMessage :(NSDictionary*)userInfo{

   //method to handle push message
}

Thanks in advance 提前致谢

In scenario 1. NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey]; 在场景1. NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey]; here remoteNotif return nil as you enter into the app through triggering app icon. 当你通过触发应用程序图标进入应用程序时, remoteNotif返回nil

In scenario 2. You can get push notification info through the following method 在方案2中。您可以通过以下方法获取推送通知信息

-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
      if (userInfo) {

          [self handlePushMessage:userInfo];
      }
}

This is a common issue: if the user does not open your app by means of the displayed notification, there is no* way of getting the related information. 这是一个常见问题:如果用户未通过显示的通知打开您的应用程序,则无法获取相关信息。

* A possible solution employed by many apps is to check with a remote server for unread notifications (eg check for an unset read-date field). *许多应用程序采用的可能解决方案是检查远程服务器是否有未读通知(例如,检查未设置的读取日期字段)。

  1. if app is in killed state and a notification came and user triggeres the app through the app icon (not from push notification ) 如果应用程序处于被杀死状态并且发出通知并且用户通过应用程序图标触发应用程序(而不是来自推送通知)

    • If the app is in killed state then the push notification payload can only be handed over to the app when the user taps the push notification itself. 如果应用程序处于被杀死状态,那么当用户点击推送通知本身时,推送通知有效负载只能被移交给应用程序。 If the user launches the app from app icon then the notification payload will not be passed to the application 如果用户从应用程序图标启动应用程序,则通知有效内容将不会传递给应用程序
  2. if app is in background and notification came and user enters to app through app icon (not from push message ) in this case also 如果应用程序在后台并且通知来了,用户也通过应用程序图标(不是来自推送消息)进入应用程序

    • if you are targeting iOS7 and up then you need to enable background mode for remote notifications. 如果您的目标是iOS7及更高版本,那么您需要为远程通知启用后台模式。 Please check below link in order to get notification payload even when app is in background 请检查以下链接,以便即使应用程序处于后台也能获得通知有效负载

    didReceiveRemoteNotification not working in the background didReceiveRemoteNotification无法在后台运行

    The above mentioned app delegate method gets called when the app is in foreground,Background and suspended state. 当app处于前台,后台和挂起状态时,会调用上面提到的app delegate方法。

There is no way to get the notification payload when app is killed and when the app icon is directly clicked instead of push notification in notification center. 当应用程序被终止时以及直接点击应用程序图标而不是通知中心中的推送通知时,无法获取通知有效负载。

Enable "Remote Notifications" under background modes in capabilities in target settings. 在目标设置的功能中,在后台模式下启用“远程通知”。 This will fetch the notification data even when the app is in the background. 即使应用程序在后台,这也会获取通知数据。 Also, make sure to implement: 另外,请确保实施:

-(void)application:(UIApplication *)application 
didReceiveRemoteNotification:(NSDictionary *)userInfo 
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler;

in your app delegate. 在你的应用程序委托中。

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

相关问题 检测应用程序是否通过单击应用程序图标或推送通知启动(当应用程序被用户强制终止时) - Detect if Application is launched by clicking app icon or by push notification (when app is force killed by user) 通过推送通知启动时调试应用程序 - Debugging App When Launched by Push Notification 单击应用程序图标时,推送通知应用程序内徽章图标未更新 - Push notification in-app badge icon not updating when clicking on app icon 推送通知仅在使用 Xcode 启动应用程序时有效 - Push notification works only when app launched with Xcode iOS推送通知-单击应用程序图标而不是通知时如何获取通知数据 - iOS Push Notification - How to get the notification data when you click on the app icon instead of notification 如果直接从后台而不是从通知味精启动应用程序,如何知道推送消息信息? - How to know a push message info if app is launched from background directly rather than from notification msg? 应用未启动时如何调试推送通知? - How to debug push notification while app is not launched? 检测应用程序是否从推送通知启动/打开 - Detect if the app was launched/opened from a push notification 推送通知-应用徽章图标 - Push Notification - App Badge Icon 当用户直接点击应用程序时如何获取推送通知消息? - How to get push notification message when the user click the App directly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM