[英]iOS app shows notifications when in background but not foreground iphone 4s & os version 9.3
应用程序在不处于运行状态,后台状态时正在接收远程通知,并使用本地拦截概念在前台状态中接收通知(在获取远程通知时触发本地通知)。 但是,当应用程序处于前台并且iOS版本为9.3时,它不会收到远程通知。
我的代码在这里:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(@"Called");
NSLog(@"Response - > %@",userInfo);
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
localNotification.alertBody = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = 0;
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
NSLog( @"Handle push from foreground" );
// custom code to handle push while app is in the foreground
NSLog(@"%@", notification.request.content.userInfo);
completionHandler(UNNotificationPresentationOptionAlert);
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler
{
NSLog( @"Handle push from background or closed" );
// if you set a member variable in didReceiveRemoteNotification, you will know if this is from closed or background
NSLog(@"%@", response.notification.request.content.userInfo);
NSInteger countNoti = [[[response.notification.request.content.userInfo valueForKey:@"aps"] valueForKey:@"badge"] integerValue];
if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive ) {
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
localNotification.alertBody = [[response.notification.request.content.userInfo valueForKey:@"aps"] valueForKey:@"alert"];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = countNoti;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
}
didReceiveRemoteNotification
调用,但未显示通知。 谢谢。
我认为您尚未在AppDelegate文件中实现iOS9 PushNotification方法
Objective-C的
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
迅速
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any])
您可以在应用处于活动状态时在上述功能中安排本地通知。 显示通知。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.