首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在app中设置android fcm默认频道?

如何在app中设置android fcm默认频道?
EN

Stack Overflow用户
提问于 2021-08-15 05:10:56
回答 1查看 170关注 0票数 2

一旦我看到这个错误,我搜索了足够的谷歌,意识到这是一个明显的元数据问题,所以我在修复它并做了大量工作后提出了这个问题。

例如,清单default_notification_channel -> default_notification_channel_id

我的原木猫

代码语言:javascript
运行
复制
2021-08-15 14:03:05.326 26315-27157/com.project W/FirebaseMessaging: Notification Channel requested (fcm_default_channel) has not been created by the app. Manifest configuration, or default, value will be used.
2021-08-15 14:03:05.327 26315-27157/com.project W/FirebaseMessaging: Notification Channel set in AndroidManifest.xml has not been created by the app. Default value will be used.

我的清单

代码语言:javascript
运行
复制
 <service
            android:name=".ui.fcm.MyFirebaseMessagingService"
            android:stopWithTask="false"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>

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

和我的fcm服务类

代码语言:javascript
运行
复制
class MyFirebaseMessagingService : FirebaseMessagingService() {

    override fun onNewToken(token: String) {
        Timber.d("Refreshed token: $token")

        //TODO : Token save 

    }

    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        
        Timber.d("MyFirebaseMessagingService onMessageReceived 성공!!!!")
        
        if (remoteMessage.notification != null) {
            Timber.d("MyFirebaseMessagingService noti.body = ${remoteMessage.notification?.body}")
            sendNotification(remoteMessage)
        }

        if (remoteMessage.data.isEmpty()) {
            return
        }

        super.onMessageReceived(remoteMessage)
    }

    private fun sendNotification(remoteMessage: RemoteMessage) {
        val uniId: Int = (System.currentTimeMillis() / 7).toInt()
        
        val intent = Intent(this, MainActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        val pendingIntent =
            PendingIntent.getActivity(this, uniId, intent, PendingIntent.FLAG_ONE_SHOT)

        val channelId = getString(R.string.default_notification_channel_id)

        val soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)

        val notificationBuilder = NotificationCompat.Builder(this, channelId)
            .setSmallIcon(R.mipmap.ic_launcher) 
            .setContentTitle(remoteMessage.data["body"].toString())
            .setContentText(remoteMessage.data["title"].toString())
            .setAutoCancel(true)
            .setSound(soundUri) 
            .setContentIntent(pendingIntent) 

        val notificationManager =
            getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel =
                NotificationChannel(channelId, "Notice", NotificationManager.IMPORTANCE_DEFAULT)

            notificationManager.createNotificationChannel(channel)
        }
        
        notificationManager.notify(uniId, notificationBuilder.build())
    }
}

我之所以问这个问题,是因为在我将数据保存到房间时,fcm不能正常工作。推送消息来了,但是数据没有记录下来,我不确定是否有正确的数据来了,因为这样的错误正在出来。

我还没能解决这个问题,你还能帮我吗?

EN

回答 1

Stack Overflow用户

发布于 2021-08-15 07:09:29

在服务标签中再添加一个意图过滤器。喜欢,

代码语言:javascript
运行
复制
<service
        android:name=".firebase.MessagingService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

添加这个完全相同的意图过滤器标记。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68788894

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档