简体   繁体   English

Flutter:如何在 firebase_messaging package 中定义通知通道?

[英]Flutter: How to define notification channel in firebase_messaging package?

I am sending this json to cloud messaging (with proper headers) and want to get notification on my device, but it does not appear on my phone.我正在将此 json 发送到云消息(带有正确的标头)并希望在我的设备上收到通知,但它没有出现在我的手机上。 I think that maybe i am missing channel id initialization for Android?我想也许我缺少 Android 的频道 ID 初始化?

*Additional info *附加信息

  • If i send "notification" payload with title or body it is receiving, but i dont want to use "notification" payload.如果我发送带有标题或正文的“通知”有效负载,但我不想使用“通知”有效负载。 I want to have "data" payload only我只想拥有“数据”有效负载
{
"to" : "my device token",
"mutable_content" : true,
"content_available": true,
    "data" : {
        "content": {
            "id": 100,
            "channelKey": "basic_channel",
            "title": "Huston!\nThe eagle has landed!",
            "body": "A small step for a man, but a giant leap to Flutter's community!",
            "notificationLayout": "BigPicture",
            "largeIcon": "https://media.fstatic.com/kdNpUx4VBicwDuRBnhBrNmVsaKU=/full-fit-in/290x478/media/artists/avatar/2013/08/neil-i-armstrong_a39978.jpeg",
            "bigPicture": "https://www.dw.com/image/49519617_303.jpg",
            "showWhen": true,
            "autoCancel": true,
            "privacy": "Private"
        },
        "actionButtons": [
            {
                "key": "REPLY",
                "label": "Reply",
                "autoCancel": true,
                "buttonType":  "InputField"
            },
            {
                "key": "ARCHIVE",
                "label": "Archive",
                "autoCancel": true
            }
        ]
    }
}

In order to define a notification channel you have to go to your AndroidManifest.xml and within the application component add this meta:为了定义通知通道,您必须将 go 到您的AndroidManifest.xml并在应用程序组件中添加此元:

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

then in your strings.xml file put the channel id like this:然后在您的 strings.xml 文件中输入频道 ID,如下所示:

<string name='default_notification_channel_id'>Channel ID</string>

After doing this, you can define your channel id in the FCM Console.完成此操作后,您可以在 FCM 控制台中定义您的频道 ID。

As simple as this.就这么简单。 Just put your android channel name here below and it works with a charm:只需在下面输入您的 android 频道名称,它就会发挥作用:

"notification": {
            "body": "Test notification",
            "title": "Test Test Test",
            "click_action": "FLUTTER_NOTIFICATION_CLICK",
           
            "sound": "your_custom_sound" 
            "android_channel_id": "channel_id_youcreated",
},

          'to':
              "",
        },

Sound file name is not necessary in the above given notification payload if you assigned that sound to your notification channel thru MainActivity.kt or java file.如果您通过 MainActivity.kt 或 java 文件将该声音分配给您的通知通道,则上述通知有效负载中不需要声音文件名。 However, it is necessary for older Android versions as they will use the sound file directly.但是,对于较旧的 Android 版本是必需的,因为它们将直接使用声音文件。 Put your file inside res/raw folder.将文件放在 res/raw 文件夹中。

暂无
暂无

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

相关问题 Flutter 错误:错误:无法解析 'package:firebase_messaging/firebase_messaging.dart' 中的 package 'firebase_messaging' - Flutter Error: Error: Could not resolve the package 'firebase_messaging' in 'package:firebase_messaging/firebase_messaging.dart' Flutter with firebase_messaging - 如何在权限对话框中自定义消息? - Flutter with firebase_messaging - how to custom message on permission dialog? FIREBASE_MESSAGING:当应用程序处于后台或已终止时,onBackgroundMessage 不处理通知 - FIREBASE_MESSAGING: onBackgroundMessage not handling notification when app is on Background or Terminated Flutter 推送通知应用程序后台:firebase_messaging - Flutter push notifications app background : firebase_messaging firebase_messaging/未知:Flutter IOS 发生未知错误 - firebase_messaging/unknown: An unknown error has occured on Flutter IOS flutter “firebase_messaging”和“localstorage”版本解决失败 - flutter "firebase_messaging" and "localstorage" version solving failed MissingPluginException(未在通道 firebase_messaging 上找到方法 Messaging#requestPermission 的实现 - MissingPluginException(No implementation found for method Messaging#requestPermission on channel firebase_messaging 如何通过firebase云消息发送定时通知到flutter app - how to send scheduled notification to flutter app through firebase cloud messaging 有没有办法在 flutter 中使用操作按钮实现推送通知? 使用 firebase_messaging - Is there any way to implement push notifications with action buttons in flutter? Using firebase_messaging Flutter Firebase 消息无法点击通知 - Flutter Firebase Messaging cannot click on notification
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM