iOS 应用在后台而非前台显示通知 iphone 4s & os 版本 9.3
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
Swift
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any])
当应用程序处于活动状态时,您可以在上述功能中安排本地通知。显示通知。
应用程序在非 运行 状态、后台状态时接收远程通知,并使用本地拦截概念在前台状态下获取通知(获取远程通知时触发本地通知)。但是当应用程序处于前台并且 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
Swift
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any])
当应用程序处于活动状态时,您可以在上述功能中安排本地通知。显示通知。