iOS 如果应用程序在前台,则应用程序不会收到通知
iOS App Not Receiving Notifications if App is in foreground
我的应用和推送通知有问题。我正在使用 Parse 服务器和 back4app.com 来处理推送通知。如果应用程序在后台运行 运行,或者甚至未处于活动状态,通知就会正常到达,但如果正在使用该应用程序,则不会弹出任何消息。我在这里错过了什么?该应用程序还允许本地通知,以及某些事情的提醒,所以我不确定这是否会导致它出现问题。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
window.rootViewController = navController;
[window makeKeyAndVisible];
[UNUserNotificationCenter currentNotificationCenter].delegate = self;
[Parse initializeWithConfiguration:[ParseClientConfiguration configurationWithBlock:^(id<ParseMutableClientConfiguration> configuration) {
configuration.applicationId = @"APPID";
configuration.clientKey = @"CLIENTKEY";
configuration.server = @"https://parseapi.back4app.com";
}]];
UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
UIUserNotificationTypeBadge |
UIUserNotificationTypeSound);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
NSDictionary *userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
if (userInfo) {
[self handleRemoteNotificationWithPayload:userInfo];
}
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
// Play a sound.
completionHandler(UNNotificationPresentationOptionSound);
NSLog( @"Here handle push notification in foreground" );
//For notification Banner - when app in foreground
completionHandler(UNNotificationPresentationOptionAlert);
// Print Notification info
NSLog(@"Userinfo %@",notification.request.content.userInfo);
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)(void))completionHandler {
NSLog(@"RECEIVED HIS WORD %@", response.notification.request.content.categoryIdentifier);
if ([response.notification.request.content.categoryIdentifier isEqualToString:@"OPEN_HIS_WORD"]) {
NSLog(@"RECEIVED HIS WORD");
HisWord *hiSword= [[HisWord alloc] initWithNibName:@"HisWord" bundle:[NSBundle mainBundle]];
[navController pushViewController:hiSword animated:YES];
}
// Else handle actions for other notification types. . .
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// Store the deviceToken in the current installation and save it to Parse.
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation setDeviceTokenFromData:deviceToken];
[currentInstallation saveInBackground];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[PFPush handlePush:userInfo];
UINavigationController *nav = (UINavigationController *) self.navController;
DirectoryViewController *dvController8 = [[DirectoryViewController alloc] initWithNibName:@"DirectoryViewController" bundle:[NSBundle mainBundle]];
[nav pushViewController:dvController8 animated:YES];
if (application.applicationState == UIApplicationStateInactive) {
[self handleRemoteNotificationWithPayload:userInfo];
}
}
-(void)handleRemoteNotificationWithPayload:(NSDictionary *)payload {
UINavigationController *nav = (UINavigationController *) self.navController;
DirectoryViewController *dvController8 = [[DirectoryViewController alloc] initWithNibName:@"DirectoryViewController" bundle:[NSBundle mainBundle]];
[nav pushViewController:dvController8 animated:YES];
}
您调用了 userNotificationCenter:willPresentNotification:withCompletionHandler:
的 completionHandler
两次,第二次被忽略。相反,您需要像这样传递两个选项:
completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
我的应用和推送通知有问题。我正在使用 Parse 服务器和 back4app.com 来处理推送通知。如果应用程序在后台运行 运行,或者甚至未处于活动状态,通知就会正常到达,但如果正在使用该应用程序,则不会弹出任何消息。我在这里错过了什么?该应用程序还允许本地通知,以及某些事情的提醒,所以我不确定这是否会导致它出现问题。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
window.rootViewController = navController;
[window makeKeyAndVisible];
[UNUserNotificationCenter currentNotificationCenter].delegate = self;
[Parse initializeWithConfiguration:[ParseClientConfiguration configurationWithBlock:^(id<ParseMutableClientConfiguration> configuration) {
configuration.applicationId = @"APPID";
configuration.clientKey = @"CLIENTKEY";
configuration.server = @"https://parseapi.back4app.com";
}]];
UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
UIUserNotificationTypeBadge |
UIUserNotificationTypeSound);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
NSDictionary *userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
if (userInfo) {
[self handleRemoteNotificationWithPayload:userInfo];
}
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
// Play a sound.
completionHandler(UNNotificationPresentationOptionSound);
NSLog( @"Here handle push notification in foreground" );
//For notification Banner - when app in foreground
completionHandler(UNNotificationPresentationOptionAlert);
// Print Notification info
NSLog(@"Userinfo %@",notification.request.content.userInfo);
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)(void))completionHandler {
NSLog(@"RECEIVED HIS WORD %@", response.notification.request.content.categoryIdentifier);
if ([response.notification.request.content.categoryIdentifier isEqualToString:@"OPEN_HIS_WORD"]) {
NSLog(@"RECEIVED HIS WORD");
HisWord *hiSword= [[HisWord alloc] initWithNibName:@"HisWord" bundle:[NSBundle mainBundle]];
[navController pushViewController:hiSword animated:YES];
}
// Else handle actions for other notification types. . .
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// Store the deviceToken in the current installation and save it to Parse.
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation setDeviceTokenFromData:deviceToken];
[currentInstallation saveInBackground];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[PFPush handlePush:userInfo];
UINavigationController *nav = (UINavigationController *) self.navController;
DirectoryViewController *dvController8 = [[DirectoryViewController alloc] initWithNibName:@"DirectoryViewController" bundle:[NSBundle mainBundle]];
[nav pushViewController:dvController8 animated:YES];
if (application.applicationState == UIApplicationStateInactive) {
[self handleRemoteNotificationWithPayload:userInfo];
}
}
-(void)handleRemoteNotificationWithPayload:(NSDictionary *)payload {
UINavigationController *nav = (UINavigationController *) self.navController;
DirectoryViewController *dvController8 = [[DirectoryViewController alloc] initWithNibName:@"DirectoryViewController" bundle:[NSBundle mainBundle]];
[nav pushViewController:dvController8 animated:YES];
}
您调用了 userNotificationCenter:willPresentNotification:withCompletionHandler:
的 completionHandler
两次,第二次被忽略。相反,您需要像这样传递两个选项:
completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);