当收到推送通知时,我已经实现了application:didReceiveRemoteNotification:
在我的应用程序中存储数据。
然而,当我的应用程序在后台,我收到通知时,只有当我触摸出现在顶部的通知横幅时,数据才会存储:
相反,如果我触摸应用程序图标重新打开它,通知的内容不会被存储:
application:didReceiveRemoteNotification:
只在我将通知横幅推到顶部时被调用。
我使用了applicationWillEnterForeground
和didFinishLaunchingWithOptions
方法,同时单击应用程序图标并调试其进入applicationWillEnterForeground
和控制无处可去。以下是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;
}
}
如果点击主屏幕上的应用程序图标,则无法获得推送通知的数据。但是有一种方法,你可以在应用程序进入前台时向服务器发送一个小的有效载荷,然后要求服务器立即发送推送通知。
再看看这个:你的问题可能和它重复了。
一般来说,你的应用程序不应该需要推送通知的内容来正常运行。苹果甚至不能保证推送通知会被发送(如果设备不可用,它会删除除最新通知外的所有通知)。
你的应用程序应该始终与服务器通信,以获得用户数据的权威状态(或你呈现的任何数据)。如果你确实收到了推送通知,你当然可以将其作为更新或显示新信息的提示。但即使用户正常点击你的应用图标(因此没有通知),你也应该联系服务器来获取或更新你需要的一切。
尽管这是一个较老的问题,但它在这个主题中排名很高,并且在iOS7中有一个解决方案。
有一个方法叫做application:didReceiveRemoteNotification:fetchCompletionHandler:即使你的应用程序在后台也会被调用。
我遇到的问题是它没有被调用。然后我找到了这篇文章,并意识到我必须在我的项目的功能中启用"远程通知"才能使其工作。