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

时间:2016-11-23 06:49:43

标签: ios firebase background push-notification ios10

我正在我的原生iOS应用中实施Apple推送通知。 推送通知是通过FireBase实现的。 它在iOS 9和iOS中完美运行早。

我在iOS 10中遇到一个问题。推送通知在iOS 10中处于其他状态正常但当应用处于后台时,我的推送通知的声音没有播放。 对于其他应用程序状态,一切都好。 仅适用于后台模式。

以下是我的实施推送通知代码。

Insdie

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

以下是注册码:

////////// 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];

以下是管理收到通知的代表。

- (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

以下是我的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";
}

1 个答案:

答案 0 :(得分:0)

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