Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.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 通知startForeground()第二次不工作_Android_Android Notifications_Notificationmanager - Fatal编程技术网

Android 通知startForeground()第二次不工作

Android 通知startForeground()第二次不工作,android,android-notifications,notificationmanager,Android,Android Notifications,Notificationmanager,我正在使用服务类创建通知。它在android O下运行良好,但对于更高版本,通知会第一次显示,如果我第二次尝试显示它,则不会显示。下面是我的代码: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { Log.e("inside here", "inside "); NotificationManager manager = (NotificationManager) service.getSystemS

我正在使用服务类创建通知。它在android O下运行良好,但对于更高版本,通知会第一次显示,如果我第二次尝试显示它,则不会显示。下面是我的代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        Log.e("inside here", "inside ");
        NotificationManager manager = (NotificationManager) service.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationChannel channel = new NotificationChannel(PRIMARY_CHANNEL, PRIMARY_CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
        channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        manager.createNotificationChannel(channel);
    }

    NotificationCompat.Builder builder = new NotificationCompat.Builder(service, PRIMARY_CHANNEL)
            .setAutoCancel(false)
            .setContentTitle(strLiveBroadcast)
            .setContentText(strAppName)
            .setLargeIcon(largeIcon)
            .setContentIntent(pendingIntent)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setSmallIcon(android.R.drawable.stat_sys_headset)
            .addAction(icon, "pause", action)
            .addAction(R.drawable.ic_stop_white, "stop", stopAction)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setWhen(System.currentTimeMillis())
            .setStyle(new android.support.v4.media.app.NotificationCompat.MediaStyle()
                    .setMediaSession(service.getMediaSession().getSessionToken())
                    .setShowActionsInCompactView(0, 1)
                    .setShowCancelButton(true)
                    .setCancelButtonIntent(stopAction));

    service.startForeground(NOTIFICATION_ID, builder.build());
这在较低版本中运行完全正常。

startForeground()
如果将
NOTIFICATION\u ID
参数设置为0,或者每次调用时都需要添加随机ID逻辑,则通知不起作用

startForeground(0, notification); // Doesn't work...

startForeground(1, notification); // Works!!!

我希望这能奏效

请发布此方法代码:
onMessageReceived
它可以帮助您。即使是在更高版本中,它也会第一次起作用,但第二次不起作用。@Ali我不清楚你想说什么。
NOTIFICATION\u ID
在我的情况下是555好的,你可以通过跟随这位朋友来实现这一点,这就是你问题的确切解决方案,请检查@bhaskarI是否清楚地提到它第一次起作用,但第二次不起作用。此外,我还添加了更高版本的条件,并在我的menifest文件中添加了权限