Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 8或更高版本中未显示通知_Android_Android Notifications_Android 8.0 Oreo - Fatal编程技术网

Android 8或更高版本中未显示通知

Android 8或更高版本中未显示通知,android,android-notifications,android-8.0-oreo,Android,Android Notifications,Android 8.0 Oreo,在注明此文章为副本之前,请查看完整的描述。我到处找,似乎找不到解决问题的办法 问题 在我的应用程序中,通知不会出现在Android 8或更高版本上。我似乎根本无法得到任何通知来参加 到目前为止我尝试了什么? 我看了一篇又一篇的文章,像是一篇教程。 所有这些都是关于设置smallIcon、contentTitle、contentText和通知频道的。我做了所有这些,但没有任何明显的成功 我的代码 在NotificationHelper类中,我有以下方法: public Notificati

在注明此文章为副本之前,请查看完整的描述。我到处找,似乎找不到解决问题的办法

问题 在我的应用程序中,通知不会出现在Android 8或更高版本上。我似乎根本无法得到任何通知来参加

到目前为止我尝试了什么? 我看了一篇又一篇的文章,像是一篇教程。
所有这些都是关于设置smallIcon、contentTitle、contentText和通知频道的。我做了所有这些,但没有任何明显的成功

我的代码

在NotificationHelper类中,我有以下方法:

    public NotificationHelper(Context base) {
        super(base);
        createChannels();
    }

    public void createChannels() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // Android 8 or higher? Set notification channel
            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ONE_ID,
                    CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_HIGH);
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.setShowBadge(true);
            notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            getManager().createNotificationChannel(notificationChannel);
        }
    }

    private NotificationManager getManager() {
        if (notifyManager == null) {
            notifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        }
        return notifyManager;
    }

    public Notification getNotification1(Context context) {
        NotificationCompat.Builder  builder = new NotificationCompat.Builder(context, CHANNEL_ONE_ID)
                .setContentTitle("New Message")
                .setContentText("You've received new messages.")
                .setSmallIcon(R.drawable.ic_notification_alert); // alert icon.

        return builder.build();
    }

    public void notify(int id, Notification notification) {
        Log.i("NotificationHelper", "Notifying with notification: " + notification.toString());
        getManager().notify(id, notification);
    }

然后在我的MainActivity中,我在onButtonClick事件中有以下方法(显然是在MainActivity的onCreate()方法中实例化notificationHelper对象之后):

有人有什么想法吗

编辑:结果表明,在模拟器上运行时,代码工作正常。这可能是指定的电池优化设置的问题


getNotification1()
中初始化通道ID的方式似乎值得怀疑。 我会写这封信:

public Notification getNotification1(Context context) {

    NotificationCompat.Builder  builder = new NotificationCompat.Builder(context, CHANNEL_ONE_ID)
            .setContentTitle("New Message")
            .setContentText("You've received new messages.")
            .setSmallIcon(R.drawable.ic_notification_alert); // alert icon.

    return builder.build();
}
编辑: 可以尝试将自定义布局与RemoteView一起使用吗

public Notification getNotification1() {
    String CHANNEL_ID = "my_channel_01";// The id of the channel.

    createChannels(CHANNEL_ID,"name");

    NotificationCompat.Builder  builder = new NotificationCompat.Builder(ctx, CHANNEL_ID)
            .setContentTitle("New Message")
            .setContentText("You've received new messages.")
            .setSmallIcon(R.drawable.ic_launcher_foreground); // alert icon.

    RemoteViews contentView = new RemoteViews(R.layout.notification_custom_layout);
    contentView.setTextViewText(R.id.notification_title, "App name");
    contentView.setTextViewText(R.id.notification_text, "Your msg");
    mBuilder.setContent(contentView);

    return builder.build();
}

我怀疑这个问题与频道的创建方式有关。将“创建频道”更改为“在下面”

public void createChannels(String channelId, String channelName) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // Android 8 or higher? Set notification channel
            NotificationChannel notificationChannel = new NotificationChannel(channelId,
                    channelName, NotificationManager.IMPORTANCE_HIGH);
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.setShowBadge(true);
            notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            getManager().createNotificationChannel(notificationChannel);
        }
    }
然后删除构造函数中的
createChannels
,并将其放入
getNotification1

public Notification getNotification1(Context context) {
        String CHANNEL_ID = "my_channel_01";// The id of the channel.

        createChannels(CHANNEL_ID,CHANNEL_NAME)

        NotificationCompat.Builder  builder = new NotificationCompat.Builder(context, CHANNEL_ID)
                .setContentTitle("New Message")
                .setContentText("You've received new messages.")
                .setSmallIcon(R.drawable.ic_notification_alert); // alert icon.

        return builder.build();
    }
编辑 我已经安装了一个测试应用程序,在运行安卓9的模拟器上进行测试,通知显示的很好。我的助手类看起来像这样

我用这种方法来测试它

NotificationHelper helper = NotificationHelper(this);
helloWorldTextview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                helper.notify(10,helper.getNotification1())
            }
        });

NotificationHelper扩展了什么?为什么要在
super
构造函数调用中传递
Context
,然后将其传递到
getNotification1()
方法中?为什么您要使用
CHANNEL\u ONE\u ID
创建
NotificationChannel
,而使用本地
CHANNEL\u ID
创建
通知?这些值相同吗?如果是这样的话,你为什么不到处使用这个字段呢?您的logcat中是否有任何相关错误或消息?它正在扩展ContextWrapper,这也是NotificationHelper的构造函数中给出上下文的原因。频道id是一个很好的点,我改变了它,但仍然没有给我结果。我没有收到任何相关的错误消息不…你如何创建此通知?它是在应用程序内部创建的还是通过外部API创建的(例如Firebase云消息)?感谢您的回复。我将其更改为同时使用CHANNEL\u ONE\u ID,但仍然没有成功。您是否扩展了FirebaseMessagingService?是否输入onMessageReceived()?是和是。只有通知没有显示。。我现在用一个简单的按钮来测试我是否能够发送通知。我在运行Android 9的模拟器上运行它,它工作得很好。嗯,你说得对,当我在模拟器上运行我的代码时,它确实工作。奇怪,但很高兴知道。我将开始研究为什么会发生这种情况;)。好的,如果你需要帮助,你可以发布一个新问题
public class NotificationHelper {

    public Context ctx;
    public NotificationManager notifyManager;

    public NotificationHelper(Context base) {
        ctx  = base;
    }

    public void createChannels(String channelId, String channelName) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // Android 8 or higher? Set notification channel
            NotificationChannel notificationChannel = new NotificationChannel(channelId,
                    channelName, NotificationManager.IMPORTANCE_HIGH);
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.setShowBadge(true);
            notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            getManager().createNotificationChannel(notificationChannel);
        }
    }

    private NotificationManager getManager() {
        if (notifyManager == null) {
            notifyManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
        }
        return notifyManager;
    }

    public void notify(int id, Notification notification) {
        Log.i("NotificationHelper", "Notifying with notification: " + notification.toString());
        getManager().notify(id, notification);
    }

    public Notification getNotification1() {
        String CHANNEL_ID = "my_channel_01";// The id of the channel.

        createChannels(CHANNEL_ID,"name");

        NotificationCompat.Builder  builder = new NotificationCompat.Builder(ctx, CHANNEL_ID)
                .setContentTitle("New Message")
                .setContentText("You've received new messages.")
                .setSmallIcon(R.drawable.ic_launcher_foreground); // alert icon.

        return builder.build();
    }
}
NotificationHelper helper = NotificationHelper(this);
helloWorldTextview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                helper.notify(10,helper.getNotification1())
            }
        });