前台服务可以调用未进行中的通知(对于api> 26)

时间:2019-05-22 13:00:06

标签: android android-notifications android-8.0-oreo foreground-service foregroundnotification

即使我正在为那些api使用通道,我也有一个前台服务,该服务似乎无法在api> 26(oreo)上发送未进行中的通知。 在下面的api上,通知可以正确显示。 下面是我发送通知的代码:

import pandas as pd

df = pd.read_excel("mydatafile.xlsx")
print("Column Headings:")

mylist = []
for i in df.index:
    if df['Names'][i].celltype == bold
        mylist.append(cell)

正确显示的唯一通知是正在进行的通知,该通知向用户显示该服务处于前台。我这样称呼它:

    private void notification(String content) {
    Random random = new Random();
    int id = random.nextInt(1000)+1;

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
        NotificationChannel chan = new NotificationChannel(STANDARD_NOTIFICATION_CHANNEL_ID, standardChannelName, NotificationManager.IMPORTANCE_NONE);
        chan.setLightColor(Color.BLUE);
        chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        assert manager != null;
        manager.createNotificationChannel(chan);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, STANDARD_NOTIFICATION_CHANNEL_ID);
        Notification notification = notificationBuilder.setOngoing(false)
                .setSmallIcon(android.R.drawable.btn_dropdown)
                .setContentTitle(content)
                .setPriority(NotificationManager.IMPORTANCE_DEFAULT)
                .setCategory(Notification.CATEGORY_SERVICE)
                .build();
        // TODO we need to see how to send not ongoing notifications in our case with API > 26
        manager.notify(id,notification);
    }
    else {
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, STANDARD_NOTIFICATION_CHANNEL_ID);
        Notification notification = notificationBuilder.setOngoing(false)
                .setSmallIcon(R.drawable.ic_launcher_fury)
                .setContentTitle(content)
                .build();
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(id,notification);
    }
}

您有一些线索吗?

1 个答案:

答案 0 :(得分:0)

我猜你的问题在这里:

NotificationChannel chan = new NotificationChannel(FOREGROUND_NOTIFICATION_CHANNEL_ID, foregroundChannelName, NotificationManager.IMPORTANCE_NONE);

根据NotificationManager.IMPORTANCE_NONE的JavaDoc

/**
 * A notification with no importance: does not show in the shade.
 */

因此,请尝试使用其他重要性级别,例如:NotificationManager.IMPORTANCE_DEFAULT

NotificationChannel chan = new NotificationChannel(FOREGROUND_NOTIFICATION_CHANNEL_ID, foregroundChannelName, NotificationManager.IMPORTANCE_DEFAULT);