FCM 通知未显示

发布于 2025-01-16 21:13:08 字数 2849 浏览 3 评论 0原文

所以我的通知系统无缘无故无法工作,我尝试了很多 stackoverflow 解决方案,但没有一个真正起作用。我思考了好几个小时,所以请帮忙!

这是 MessagingService.java 类:

public final class MessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        User user = new User(
                remoteMessage.getData().get(Constants.KEY_NAME),
                "",
                "",
                remoteMessage.getData().get(Constants.KEY_FCM_TOKEN),
                remoteMessage.getData().get(Constants.KEY_USER_ID)
        );

        String channelId = "chat_message";

        Intent intent = new Intent(this, ChatActivity.class);

        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.putExtra(Constants.KEY_USER, user);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId);

        builder.setSmallIcon(R.drawable.ic_notification);
        builder.setContentTitle(user.getName());
        builder.setContentText(remoteMessage.getData().get(Constants.KEY_MESSAGE));
        builder.setStyle(new NotificationCompat.BigTextStyle()
                .bigText(remoteMessage.getData().get(Constants.KEY_MESSAGE)));
        builder.setPriority(NotificationCompat.PRIORITY_HIGH);
        builder.setContentIntent(pendingIntent);
        builder.setAutoCancel(true);
        builder.addAction(R.drawable.ic_notification, "eee", pendingIntent);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            CharSequence channelName = "Chat Message";
            String channelDescription = "Used for message notifications";

            NotificationChannel channel = new NotificationChannel("", "", NotificationManager.IMPORTANCE_DEFAULT);
            channel.setDescription(channelDescription);

            NotificationManager notificationChannel = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationChannel.createNotificationChannel(channel);
        }

        int id = ThreadLocalRandom.current().nextInt(100);

        NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
        notificationManagerCompat.notify(id, builder.build());
    }
}

这是我的 AndroidManifest.xml:

        <service
            android:name=".firebase.MessagingService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

so my notification system isn't working for no reason, I've tried many stackoverflow solutions but none of them really worked. I have been thinking for many hours, so please help!

here is MessagingService.java class:

public final class MessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        User user = new User(
                remoteMessage.getData().get(Constants.KEY_NAME),
                "",
                "",
                remoteMessage.getData().get(Constants.KEY_FCM_TOKEN),
                remoteMessage.getData().get(Constants.KEY_USER_ID)
        );

        String channelId = "chat_message";

        Intent intent = new Intent(this, ChatActivity.class);

        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.putExtra(Constants.KEY_USER, user);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId);

        builder.setSmallIcon(R.drawable.ic_notification);
        builder.setContentTitle(user.getName());
        builder.setContentText(remoteMessage.getData().get(Constants.KEY_MESSAGE));
        builder.setStyle(new NotificationCompat.BigTextStyle()
                .bigText(remoteMessage.getData().get(Constants.KEY_MESSAGE)));
        builder.setPriority(NotificationCompat.PRIORITY_HIGH);
        builder.setContentIntent(pendingIntent);
        builder.setAutoCancel(true);
        builder.addAction(R.drawable.ic_notification, "eee", pendingIntent);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            CharSequence channelName = "Chat Message";
            String channelDescription = "Used for message notifications";

            NotificationChannel channel = new NotificationChannel("", "", NotificationManager.IMPORTANCE_DEFAULT);
            channel.setDescription(channelDescription);

            NotificationManager notificationChannel = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationChannel.createNotificationChannel(channel);
        }

        int id = ThreadLocalRandom.current().nextInt(100);

        NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
        notificationManagerCompat.notify(id, builder.build());
    }
}

and here's my AndroidManifest.xml:

        <service
            android:name=".firebase.MessagingService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文