繁体   English   中英

无法在android的设备通知部分收到通知

[英]Unable to received notification in device notification section in android

这是我接收通知的代码

public class FirebaseMessageReceiver
        extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        sendNotification(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody());

    }

    private void sendNotification(String messageTitle,String messageBody) {
        Intent intent = new Intent(this, NotificationActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,0 /* request code */, intent,PendingIntent.FLAG_UPDATE_CURRENT);

        long[] pattern = {500,500,500,500,500};

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setContentTitle(messageTitle)
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setVibrate(pattern)
                .setLights(Color.BLUE,1,1)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
}

我正在尝试从 firebase 控制台接收通知我能够在触发通知时得到回电我也成功执行了标题和 msg 代码但我无法在设备的通知部分看到通知任何人都可以帮我做什么我做错了。

对于运行 Android O 或更高版本的设备,您需要先创建一个通知通道才能接收如下通知:

private void createNotificationChannel() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    CHANNEL_ID,
                    "Channel 1",
                    NotificationManager.IMPORTANCE_HIGH
            );

            channel.setDescription("This is Channel 1");

            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(channel);
        }
}

此外,您需要在 NotificationCompact.Builder 构造函数中传递这个 CHANNEL_ID 常量,如下所示:

NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this, CHANNEL_ID)

你使用通知渠道吗? 如果您使用的是 Adroid 8 或更高版本,则预期行为:

从 Android 8.0(API 级别 26)开始,所有通知都必须分配给一个频道,否则它不会出现。

https://developer.android.com/guide/topics/ui/notifiers/notifications#ManageChannels

在这里您可以找到如何创建通知通道的详细教程: https ://developer.android.com/training/notify-user/channels

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM