[英]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 我有一个场景,其中应用程序将获得推送通知,并需要在我的应用程序的主屏幕中显示消息,因为我将消息数组保存到我的应用程序委托的用户默认值,一切正常,但在以下条件下它不起作用
if app is in killed state and a notification came and user triggeres the app through the app icon (not from push notification ) 如果应用程序处于被杀死状态并且发出通知并且用户通过应用程序图标触发应用程序(而不是来自推送通知)
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). *许多应用程序采用的可能解决方案是检查远程服务器是否有未读通知(例如,检查未设置的读取日期字段)。
if app is in killed state and a notification came and user triggeres the app through the app icon (not from push notification ) 如果应用程序处于被杀死状态并且发出通知并且用户通过应用程序图标触发应用程序(而不是来自推送通知)
if app is in background and notification came and user enters to app through app icon (not from push message ) in this case also 如果应用程序在后台并且通知来了,用户也通过应用程序图标(不是来自推送消息)进入应用程序
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.