簡體   English   中英

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

迅速

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any])

您可以在應用處於活動狀態時在上述功能中安排本地通知。 顯示通知。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM