>  기사  >  백엔드 개발  >  iOS의 백그라운드 모드에서 FCM(Firebase Cloud Messaging) 알림이 유지되지 않는 이유는 무엇입니까?

iOS의 백그라운드 모드에서 FCM(Firebase Cloud Messaging) 알림이 유지되지 않는 이유는 무엇입니까?

Susan Sarandon
Susan Sarandon원래의
2024-10-20 15:22:02926검색

Why Are Firebase Cloud Messaging (FCM) Notifications Not Retained in Background Mode on iOS?

Firebase 클라우드 메시징(FCM): iOS에서 앱이 백그라운드 모드일 때 알림이 수신되지 않음

FCM을 사용하면 개발자가 다음으로 푸시 알림을 보낼 수 있습니다. iOS 및 Android 기기. 이 문제는 앱이 백그라운드에 있을 때 알림이 수신되지 않을 때 발생합니다.

가능한 원인:

  • 알림 전송에 잘못된 PHP 코드가 있습니다.
  • iOS 애플리케이션에서 백그라운드 모드 지원이 부족합니다.

해결책:

PHP 코드:

  • PHP 페이로드에 다음 필드를 추가하세요.

    • 'content_available' => true(iOS 앱이 백그라운드에 있을 때 트리거)
    • 'priority' => '높음'
    • '알림' => $data($data를 원하는 알림 데이터로 대체)
  • $data를 맞춤 값과 함께 '메시지' 및 '본문' 키를 모두 포함하도록 설정하세요.

iOS 애플리케이션:

  1. Xcode 프로젝트에 .gcm 파일이 포함되어 있는지 확인하세요.
  2. AppDelegate가 GCMReceiverDelegate를 준수하는지 확인하세요.
  3. applicationDidBecomeActive 및 applicationDidEnterBackground 메서드를 재정의하여 각각 FCM에 (재)연결하고 연결을 끊습니다.
  4. UIRemoteNotificationType을 사용하여 알림을 등록합니다.
  5. 수신된 알림을 처리하는 application:didReceiveRemoteNotification: 메서드를 구현합니다. 앱이 백그라운드에 있을 때.
  6. 필요한 경우 application:didReceiveRemoteNotification:fetchCompletionHandler: 메소드를 구현하여 백그라운드에서 수신된 알림을 추가로 처리합니다.

업데이트된 PHP 코드 :

<code class="php"><?php

$data = array(
    'message' => 'Hello World!',
    'body' => 'Hello World!'
);

$post = array(
    'registration_ids' => $ids,
    'data' => $data,
    'content_available' => true,
    'priority' => 'high',
    'notification' => $data
);

// ... Remaining code</code>

업데이트된 iOS 애플리케이션:

<code class="objc">// AppDelegate.h
@interface AppDelegate : UIResponder <UIApplicationDelegate, GCMReceiverDelegate>

// AppDelegate.m
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // ...
    // Register for remote notifications
    if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {
        UIRemoteNotificationType allNotificationTypes =
        (UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge);
        [application registerForRemoteNotificationTypes:allNotificationTypes];
    } else {
        UIUserNotificationType allNotificationTypes =
        (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
        UIUserNotificationSettings *settings =
        [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    }
    // ...
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    [[GCMService sharedInstance] disconnect];
    _connectedToGCM = NO;
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    [[GCMService sharedInstance] connectWithHandler:^(NSError *error) {
        // ...
    }];
}

// ...</code>

참고:

  • Apple 개발자 포털에서 앱에 대한 푸시 알림을 활성화했습니다.
  • FCM 프로젝트 설정에서 푸시 알림 인증서를 추가하세요.

위 내용은 iOS의 백그라운드 모드에서 FCM(Firebase Cloud Messaging) 알림이 유지되지 않는 이유는 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.