bestsource

파이어베이스:안드로이드 앱에서 기본 알림 채널을 설정하는 방법은?

bestsource 2023. 6. 8. 21:14
반응형

파이어베이스:안드로이드 앱에서 기본 알림 채널을 설정하는 방법은?

앱이 백그라운드에 있을 때 오는 알림 메시지의 기본 알림 채널을 설정하는 방법은 무엇입니까?기본적으로 이러한 메시지는 "기타" 채널을 사용합니다.

여기 공식 문서에서 볼 수 있듯이 다음 메타데이터 요소를 추가해야 합니다.AndroidManifest.xml애플리케이션 구성 요소 내:

<meta-data
    android:name="com.google.firebase.messaging.default_notification_channel_id"
    android:value="@string/default_notification_channel_id"/>

이 기본 채널은 알림 메시지에 지정된 채널이 없거나 제공된 채널이 앱에서 아직 생성되지 않은 경우에 사용됩니다.

다음을 사용하여 채널을 등록해야 합니다.NotificationManager CreateNotificationChannel.

이 예는 Xamarin에서 c#을 사용하지만 다른 곳에서도 광범위하게 적용할 수 있습니다.

private void ConfigureNotificationChannel()
{
    if (Build.VERSION.SdkInt < BuildVersionCodes.O)
    {
        // Notification channels are new in API 26 (and not a part of the
        // support library). There is no need to create a notification
        // channel on older versions of Android.
        return;
    }

    var channelId = Resources.GetString(Resource.String.default_notification_channel_id);
    var channelName = "Urgent";
    var importance = NotificationImportance.High;

    //This is the default channel and also appears in the manifest
    var chan = new NotificationChannel(channelId, channelName, importance);

    chan.EnableVibration(true);
    chan.LockscreenVisibility = NotificationVisibility.Public;

    var notificationManager = (NotificationManager)GetSystemService(NotificationService);
    notificationManager.CreateNotificationChannel(chan);

}

이 채널은 고유해야 합니다(예: com.mycompany.myapp.urgent).

그런 다음 AndroidManifest.xml응용 프로그램 태그 내부에 있는 문자열에 참조를 추가합니다.

<application android:label="MyApp" android:icon="@mipmap/icon">
    <meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="@string/default_notification_channel_id" />
</application>

마지막으로 string.xml의 문자열을 설정합니다.

<?xml version="1.0" encoding="UTF-8" ?>
<resources>
    <string name="default_notification_channel_id">com.mycompany.myapp.urgent</string>
</resources>

Cordova config.xml을 추가해야 합니다.

<widget 
... 
xmlns:android="http://schemas.android.com/apk/res/android" 
...>



 <platform name="android">
    <config-file target="AndroidManifest.xml" parent="application" mode="merge">
      <meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="@string/default_notification_channel_id"/>
    </config-file>
    <config-file target="res/values/strings.xml" parent="/*">
      <string name="default_notification_channel_id">urgent_alert</string>
    </config-file>
</platform>

언급URL : https://stackoverflow.com/questions/46047343/firebase-how-to-set-default-notification-channel-in-android-app

반응형