简体   繁体   English

Firebase推送通知警报声音未在iOS 10的后台模式下播放

[英]Firebase push notification alert sound is not playing in background mode in iOS 10

I am implementing Apple push notification in my native iOS app. 我正在本机iOS应用程序中实现Apple推送通知。 Push notification is implemented via FireBase. 推送通知是通过FireBase实现的。 It's working perfect in iOS 9 & earlier. 在iOS 9及更早版本中,它运行完美。

I am facing one issue in iOS 10. Push notification is working fine for in iOS 10 with other state but When app is in background, sound of my push notification is not playing. 我在iOS 10中遇到一个问题。推送通知在其他状态下的iOS 10中可以正常工作,但是当应用程序在后台运行时,我的推送通知的声音无法播放。 For other state of app , it's all ok. 对于其他状态的应用程序,一切正常。 Only issue with background mode. 仅背景模式有问题。

Below is my code for Implement push notification. 以下是我的实施推送通知的代码。

Insdie 英迪

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

Below is Registration code: 以下是注册代码:

////////// FireBase////////////
    // Register for remote notifications
    if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {
        // iOS 7.1 or earlier. Disable the deprecation warnings.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        UIRemoteNotificationType allNotificationTypes =
        (UIRemoteNotificationTypeSound |
         UIRemoteNotificationTypeAlert |
         UIRemoteNotificationTypeBadge);
        [application registerForRemoteNotificationTypes:allNotificationTypes];
#pragma clang diagnostic pop
    } else {
        // iOS 8 or later
        // [START register_for_notifications]
        if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
            UIUserNotificationType allNotificationTypes =
            (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
            UIUserNotificationSettings *settings =
            [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
            [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        } else {
            // iOS 10 or later

#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
            UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
            center.delegate = self;
            [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
                if( !error ){
                    [[UIApplication sharedApplication] registerForRemoteNotifications];
                }
            }];

            [[FIRMessaging messaging] setRemoteMessageDelegate:self];
#endif
        }

        [[UIApplication sharedApplication] registerForRemoteNotifications];
        // [END register_for_notifications]
    }


    [FIRApp configure];
    // [END configure_firebase]
    // Add observer for InstanceID token refresh callback.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tokenRefreshNotification:)
                                                 name:kFIRInstanceIDTokenRefreshNotification object:nil];

Below are the delegate(s) for manage received notification. 以下是用于管理收到的通知的代表。

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

    NSLog(@"Message ID: %@", userInfo[@"gcm.message_id"]);
    NSLog(@"%@", userInfo);

    [self manageAppAfterReceiveNotific: userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
}

#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{

    NSDictionary *userInfo = response.notification.request.content.userInfo;
    [self manageAppAfterReceiveNotific: userInfo];
}
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{

    completionHandler(UNNotificationPresentationOptionAlert);
}

- (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage {

    NSLog(@"%@", [remoteMessage appData]);

    [self manageAppAfterReceiveNotific: [remoteMessage appData]];
}
#endif

Below is my PayLoad : 以下是我的PayLoad:

{
    aps =     {
        alert =         {
            body = "any text";
            title = "New Notification";
        };
        badge = 1;
        "content-available" = 1;
        sound = default;
    };
    extra = "{\"childrenId\":\"48\",\"timestamp\":\"1479724388\"}";
    "gcm.message_id" = "Message ID";
    noteType = "CHECK_OUT";
}

根据Firebase文件https://firebase.google.com/docs/cloud-messaging/http-server-ref ,您应将“声音”字段放入通知消息中,请参阅表2a。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM