未从Android服务显示通知

问题描述 投票:-1回答:1

我搜索了许多与此主题相关的其他问题,但发现回答不令人满意,也没有一个对我有用。我想显示一个连续的通知,该通知只能由应用终止。但是我写的代码在几天前有效,但现在不行。

private void GenNotification(String title, String body)
    {
        try
        {
            Log.i(Config.TAGWorker, "Generating Notification . . .");
            Intent myIntent = new Intent(this, MainActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(
                    this,
                    0,
                    myIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);

            Notification notification = new NotificationCompat.Builder(this)
                    .setContentTitle(title)
                    .setContentText(body)
                    .setChannelId("myID")
                    .setTicker("Notification!")
                    .setWhen(System.currentTimeMillis())
                    .setContentIntent(pendingIntent)
                    .setDefaults(Notification.DEFAULT_SOUND)
                    .setAutoCancel(false)
                    .setSmallIcon(R.drawable.floppy)
                    .setOngoing(true)
                    .build();
            NotificationManager mNotificationManager = (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
            mNotificationManager.notify(1, notification);
        }
        catch (Exception e)
        {
            Log.e(Config.TAGWorker, e.getMessage());
        }
    }

Logcat中没有记录关于这些的例外。该代码在服务的onCreate中调用。该服务正确启动,我可以在Log cat中看到,也没有异常,但未显示通知。我的操作系统是适用于诺基亚(PI)的Android ONE]

java android service notifications
1个回答
0
投票

您是否检查了您的字符串(标题和正文)是否为空,如果它为空,则不会显示通知如果您的android 7.0以上版本,还请检查您每次启动服务时是否调用通知通道当您想起相同的ID为1时,清除通知。

© www.soinside.com 2019 - 2024. All rights reserved.