[英]When my app is in background, push notifications are handled only if I touch the top notification banner and not working while I click app icon
I've implemented application:didReceiveRemoteNotification:
to store data in my app when a push notification is received. 我已经实现了
application:didReceiveRemoteNotification:
在收到推送通知时将数据存储在我的应用程序中。
However when my app is in background and I receive a notification, the data is stored only if I touch the notification banner appearing on top: 但是,当我的应用程序处于后台并且收到通知时,仅当我触摸顶部显示的通知横幅时才会存储数据:
Instead, if I touch the app icon to reopen it, the content of the notification is not stored: 相反,如果我触摸应用程序图标以重新打开它,则不会存储通知的内容:
application:didReceiveRemoteNotification:
is invoked only when I push the notification banner on top. application:didReceiveRemoteNotification:
仅当我将通知横幅推到顶部时才会调用。
I've used applicationWillEnterForeground
and didFinishLaunchingWithOptions
methods and while clicking the app icon and debugging its entering applicationWillEnterForeground
and control goes nowhere. 我已经使用了
applicationWillEnterForeground
和didFinishLaunchingWithOptions
方法,同时单击应用程序图标并调试其输入的applicationWillEnterForeground
和控件无处可去。 Here is the code for didFinishLaunchingWithOptions
and applicationWillEnterForeground
and didReceiveRemoteNotification
. 这是
didFinishLaunchingWithOptions
和applicationWillEnterForeground
以及didReceiveRemoteNotification
的代码。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
self.isForeground = YES;
// Let the device know we want to receive push notifications
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
storage= [[NSMutableArray alloc]init];
if (launchOptions != nil) {
// launched from notification item click
NSDictionary *userInfo = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
if (userInfo != nil) [self HandleNotification:userInfo];
}
return YES;
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
isForeground = YES;
NSArray *subviews = [window subviews];
for (int i = 0; i < [subviews count]; i++) {
[[subviews objectAtIndex:i] removeFromSuperview];
}
//[self.window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[self HandleNotification:userInfo];
}
- (void)HandleNotification:(NSDictionary *)userInfo {
ApiWrapper *wrapper = [[ApiWrapper alloc] init];
NSString *dteStr = [[NSString alloc] init];
NSDate *nowdate = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
//[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"Europe/London"]];
[dateFormat setDateFormat:@"dd/MM/yyyy HH:mm:ss"];
dteStr = [dateFormat stringFromDate:nowdate];
[dateFormat release];
NSString *notifId = [userInfo objectForKey:@"NotificationId"];
NSData *test = self.strTest;
NSString *strToken = [NSString stringWithFormat:@"%@", test];
strToken = [strToken substringWithRange:NSMakeRange(1, [strToken length] - 2)];
[wrapper deviceResponse:notifId:dteStr:strToken];
NSLog(@".....user info%@", userInfo);
NSDictionary *pushInfo = [userInfo objectForKey:@"aps"];
NSString *alertstring = [pushInfo objectForKey:@"alert"];
NSLog(@"Alertstring: %@", alertstring);
[UIApplication sharedApplication].applicationIconBadgeNumber = [[[userInfo objectForKey:@"aps"] objectForKey: @"badgecount"] intValue];
MLNotifMessage *objNotif = [[MLNotifMessage alloc] init];
objNotif.notifText = alertstring;
NSDate *nowdate1 = [NSDate date];
NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
//[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"Europe/London"]];
[dateFormat1 setDateFormat:@"dd/MM/yyyy HH:mm:ss"];
objNotif.datenow = [dateFormat1 stringFromDate:nowdate1];
[dateFormat1 release];
NSLog(@"Date in delegate class is %@", objNotif.datenow);
[storage addObject:objNotif];
if (self.isForeground) {
NSArray *subviews = [window subviews];
for (int i = 0; i < [subviews count]; i++) {
[[subviews objectAtIndex:i] removeFromSuperview];
}
[self.window makeKeyAndVisible];
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
}
}
You can not get data for push notification if clicked on app icon on home screen. 如果点击主屏幕上的应用程序图标,则无法获取推送通知的数据。 But there is way around, you could send a small payload to your server whenever the app enters in foreground, then ask for the server to send push notification immediately.
但是有办法解决,只要应用程序进入前台,您就可以向服务器发送一个小的负载,然后请求服务器立即发送推送通知。
Also take a look at this : your question is possibly duplicate of it. 另外看看这个:你的问题可能是重复的。
Hope this helps you. 希望这对你有所帮助。
In general your app should not require the contents of push notifications for normal operation. 通常,您的应用程序不应要求正常操作的推送通知内容。 Apple doesn't even guarantee that push notifications will be delivered (it will drop all but the most recent if the device is unavailable).
Apple甚至不保证会发送推送通知(如果设备不可用,它将会丢弃最新的通知)。
Your app should always talk to the server to get the authoritative state of the user's data (or whatever it is you presenting). 您的应用应始终与服务器通信,以获取用户数据的权威状态(或您呈现的任何内容)。 If you do receive a push notification you can of course use that as a cue to update or display new information.
如果您确实收到推送通知,您当然可以将其用作更新或显示新信息的提示。 But even when the user taps your app icon normally (therefore no notification) you should contact the server to fetch or update everything you need.
但即使用户正常点击您的应用图标(因此没有通知),您也应该联系服务器以获取或更新您需要的所有内容。
Even though this is an older question, it ranks high in this topic and there's a solution for it as of iOS7. 即使这是一个较老的问题,它在这个主题中排名很高,并且从iOS7开始就有一个解决方案。
There's a method called application:didReceiveRemoteNotification:fetchCompletionHandler: which will get called even if your application is in the background. 有一个名为application的方法:didReceiveRemoteNotification:fetchCompletionHandler:即使你的应用程序在后台也会被调用。
The problem I was having is that it wasn't getting called. 我遇到的问题是它没有被调用。 I then found this article and realized I had to enable "Remote Notifications" inside of my project's Capabilities in order for this to work.
然后我发现了这篇文章并意识到我必须在我的项目功能中启用“远程通知”才能使其正常工作。
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.