iOS应用程序在后台显示通知,但不显示前台iphone 4s& os版本9.3

时间:2018-04-06 07:18:26

标签: ios objective-c iphone apple-push-notifications

应用程序在未运行状态,后台状态时接收远程通知,并且还使用本地拦截概念获取前景状态通知(获取远程通知时触发本地通知)。但是当应用程序处于前台且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,但未显示通知。 感谢。

1 个答案:

答案 0 :(得分:1)

我认为您尚未在AppDelegate文件中实现iOS9 PushNotification方法

<强>目标C

string query = "SELECT * FROM users WHERE username = @username";

ServerModel.Database.CheckConnection(); // Get the main connection
MySqlCommand cmd = new MySqlCommand(query, ServerModel.Database);
cmd.Parameters.AddWithValue("@username", username);

UserStatus userStatus;
using (MySqlDataReader dataReader = cmd.ExecuteReader())
{
    if (dataReader.Read())
    {
        ...
        dataReader.Close();
        return userStatus;
    }
}

<强>夫特

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

当app处于活动状态时,您可以在上述功能中安排本地通知。显示通知。