[英]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
我已經實現了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;
}
}
如果點擊主屏幕上的應用程序圖標,則無法獲取推送通知的數據。 但是有辦法解決,只要應用程序進入前台,您就可以向服務器發送一個小的負載,然后請求服務器立即發送推送通知。
希望這對你有所幫助。
通常,您的應用程序不應要求正常操作的推送通知內容。 Apple甚至不保證會發送推送通知(如果設備不可用,它將會丟棄最新的通知)。
您的應用應始終與服務器通信,以獲取用戶數據的權威狀態(或您呈現的任何內容)。 如果您確實收到推送通知,您當然可以將其用作更新或顯示新信息的提示。 但即使用戶正常點擊您的應用圖標(因此沒有通知),您也應該聯系服務器以獲取或更新您需要的所有內容。
即使這是一個較老的問題,它在這個主題中排名很高,並且從iOS7開始就有一個解決方案。
有一個名為application的方法:didReceiveRemoteNotification:fetchCompletionHandler:即使你的應用程序在后台也會被調用。
我遇到的問題是它沒有被調用。 然后我發現了這篇文章並意識到我必須在我的項目功能中啟用“遠程通知”才能使其正常工作。
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.