te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>Can't get correct Icon or Intent when sending notification through Firebase Cloud Messaging android - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Can't get correct Icon or Intent when sending notification through Firebase Cloud Messaging android - Stack Overflow

programmeradmin3浏览0评论

I'm using Firebase Cloud Messaging to send notifications to users about updates in my Android app. Notifications work fine when the app is in the foreground, but when the app is in the background or closed, I experience two issues:

The notification icon is just a blank circle instead of the specified drawable (R.drawable.notification_icon). Clicking the notification opens the app instead of the intended URL (which should open a webpage). I think this might be related to the context, but I’ve already tried using applicationContext, and the issue still happens.

My Implementation:

class MyFirebaseMessagingService : FirebaseMessagingService() {
    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        super.onMessageReceived(remoteMessage)

        // Get the notification data
        val notification = remoteMessage.notification

        val notificationIntent = Intent(Intent.ACTION_VIEW, Uri.parse(";))
        if (notification != null) {
            notification.title?.let { title ->
                notification.body?.let { body ->
                    sendNotification(
                        this, title, body, "updates", "Updates", notificationIntent
                    )
                }
            }
        }
    }

    override fun onNewToken(token: String) {
        super.onNewToken(token)
        Log.d("FCM", "New token: $token")
    }
}

fun sendNotification(context: Context, title: String, message: String, channelID: String, channelName: String, intent: Intent) {
    val notificationId = 1

    // Create a notification channel (only needed for Android 8.0+)
    val channel = NotificationChannel(
        channelID,
        channelName,
        NotificationManager.IMPORTANCE_DEFAULT
    ).apply {
        description = channelName
    }

    val pendingIntent = PendingIntent.getActivity(
        context,
        0,
        intent,
        PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
    )

    val notificationManager: NotificationManager =
        context.getSystemService(NotificationManager::class.java)
    notificationManager.createNotificationChannel(channel)

    // Build the notification
    val notification = NotificationCompat.Builder(context, channelID)
        .setSmallIcon(R.drawable.notification_icon) // This is showing as a blank circle
        .setContentTitle(title)
        .setContentText(message)
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        .setAutoCancel(true)
        .setContentIntent(pendingIntent)
        .build()

    with(NotificationManagerCompat.from(context)) {
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
            return
        }
        notify(notificationId, notification)
    }
}

I'm using Firebase Cloud Messaging to send notifications to users about updates in my Android app. Notifications work fine when the app is in the foreground, but when the app is in the background or closed, I experience two issues:

The notification icon is just a blank circle instead of the specified drawable (R.drawable.notification_icon). Clicking the notification opens the app instead of the intended URL (which should open a webpage). I think this might be related to the context, but I’ve already tried using applicationContext, and the issue still happens.

My Implementation:

class MyFirebaseMessagingService : FirebaseMessagingService() {
    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        super.onMessageReceived(remoteMessage)

        // Get the notification data
        val notification = remoteMessage.notification

        val notificationIntent = Intent(Intent.ACTION_VIEW, Uri.parse("https://github/geeclensy/escape"))
        if (notification != null) {
            notification.title?.let { title ->
                notification.body?.let { body ->
                    sendNotification(
                        this, title, body, "updates", "Updates", notificationIntent
                    )
                }
            }
        }
    }

    override fun onNewToken(token: String) {
        super.onNewToken(token)
        Log.d("FCM", "New token: $token")
    }
}

fun sendNotification(context: Context, title: String, message: String, channelID: String, channelName: String, intent: Intent) {
    val notificationId = 1

    // Create a notification channel (only needed for Android 8.0+)
    val channel = NotificationChannel(
        channelID,
        channelName,
        NotificationManager.IMPORTANCE_DEFAULT
    ).apply {
        description = channelName
    }

    val pendingIntent = PendingIntent.getActivity(
        context,
        0,
        intent,
        PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
    )

    val notificationManager: NotificationManager =
        context.getSystemService(NotificationManager::class.java)
    notificationManager.createNotificationChannel(channel)

    // Build the notification
    val notification = NotificationCompat.Builder(context, channelID)
        .setSmallIcon(R.drawable.notification_icon) // This is showing as a blank circle
        .setContentTitle(title)
        .setContentText(message)
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        .setAutoCancel(true)
        .setContentIntent(pendingIntent)
        .build()

    with(NotificationManagerCompat.from(context)) {
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
            return
        }
        notify(notificationId, notification)
    }
}
Share Improve this question asked Feb 17 at 11:41 Gee ClensyGee Clensy 135 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

The problem is likely that you're sending a "notification message" instead of a "data message". Firebase's docs say that notification messages will only call your callback if your app is running, but otherwise will be displayed by the FCM SDK itself. Whereas data messages always go via your callback:

https://firebase.google/docs/cloud-messaging/concept-options

You'll need to change how you're sending the notification from the Firebase console to make it a data message, and change your app code to get its data from remoteMessage.data instead of removeMessage.notification

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论