【问题标题】:Notification in android system tray, does not workandroid系统托盘中的通知,不起作用
【发布时间】:2021-06-05 09:30:24
【问题描述】:

我已经设置了接收通知的代码,但它不起作用:

                String ns = Context.NOTIFICATION_SERVICE;
                NotificationManager notificationManager = (NotificationManager) getSystemService(ns);
                int icon = R.drawable.messages_received;
                CharSequence contentTitle = "TITLE";
                Intent notificationIntent = new Intent(this, MainActivity.class);
                PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

                Context context = getApplicationContext();
                NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

                Notification notification  = builder
                        .setContentIntent(contentIntent)
                        .setSmallIcon(icon)
                        .setWhen( System.currentTimeMillis())
                        .setContentTitle(contentTitle)
                        .setContentText(message.getMessage())
                        .build();

                notification.flags |= Notification.FLAG_AUTO_CANCEL;
                notification.defaults |= Notification.DEFAULT_SOUND;
                notification.defaults |= Notification.DEFAULT_VIBRATE;
                notification.defaults |= Notification.DEFAULT_LIGHTS;

                final int NOTIFY = 1;
                notificationManager.notify(NOTIFY, notification);

我在我的 MainActivity 上使用它,并在电话处于待机状态时尝试了它。 我该如何解决?它需要一些其他代码吗? 我遵循了这个指南:http://www.dre.vanderbilt.edu/~schmidt/android/android-4.0/out/target/common/docs/doc-comment-check/guide/topics/ui/notifiers/notifications.html

谢谢

【问题讨论】:

  • 您测试的操作系统版本是什么?
  • Android 10,但我也想要旧操作系统的解决方案,谢谢

标签: java android notifications


【解决方案1】:
     public static final String CHANNEL_ID = "com.example.test.services";
     public static final String CHANNEL_NAME = "UploadBackgroundService";
     public static final int FOREGROUND_ID = 1339;
    
    private void createAndShowForegroundNotification() {
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    
                NotificationChannel chan = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_NONE);
                chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
                manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                assert manager != null;
                manager.createNotificationChannel(chan);
    
                notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID);
                Notification notification = notificationBuilder
                        .setContentTitle("Image upload service")
                        .setContentText("Uploading Images...")
                        .setPriority(NotificationManager.IMPORTANCE_HIGH)
                        .setCategory(Notification.CATEGORY_SERVICE)
                        .setOngoing(true)
                        .setSmallIcon(R.drawable.logoo)
                        .setAutoCancel(false)
                        .build();
                manager.notify(FOREGROUND_ID, notification);
            } else {
                manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                notificationBuilder = new NotificationCompat.Builder(this,CHANNEL_ID )
                        .setDefaults(Notification.DEFAULT_ALL)
                        .setPriority(NotificationCompat.PRIORITY_HIGH)
                        .setContentTitle("Image upload service")
                        .setContentText("Uploading Images...")
                        .setOngoing(true)
                        .setSmallIcon(R.drawable.logoo)
                        .setAutoCancel(false);
                Notification notification = notificationBuilder.build();
                manager.notify(FOREGROUND_ID, notification);
            }
        }

try this code.replace the required parameters with your own.

【讨论】:

  • Krishan Madushanka,startForeground 代表什么?它缺少一些代码..
  • 我尝试了您的代码,但它也不起作用,我看到 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { .... 计算结果为 true,但没有通知出现,为什么?
  • 也尝试设置.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.logo)) .setBadgeIconType(R.mipmap.logo)
【解决方案2】:

我用这段代码解决了这个问题:

public static final String CHANNEL_ID = "messages-id";
public static final String CHANNEL_NAME = "messages";
public static final int FOREGROUND_ID = 1339;

private void notifyForeground(String message) {
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        NotificationChannel chan = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
        assert manager != null;
        manager.createNotificationChannel(chan);

        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID);
        Notification notification = notificationBuilder
                .setContentIntent(contentIntent)
                .setDefaults(Notification.DEFAULT_ALL)
                .setContentTitle("New message")
                .setContentText(message)
                .setPriority(NotificationManager.IMPORTANCE_HIGH)
                .setCategory(Notification.CATEGORY_MESSAGE)
                .setSmallIcon(R.drawable.messages_received)
                .setAutoCancel(true)
                .build();
        manager.notify(FOREGROUND_ID, notification);
    } else {
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,CHANNEL_ID )
                .setDefaults(Notification.DEFAULT_ALL)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setContentTitle("New message")
                .setContentText(message)
                .setSmallIcon(R.drawable.messages_received)
                .setAutoCancel(true);
        Notification notification = notificationBuilder.build();
        manager.notify(FOREGROUND_ID, notification);
    }
}

谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-28
    • 2017-02-03
    • 1970-01-01
    • 2018-12-12
    • 1970-01-01
    • 1970-01-01
    • 2019-12-05
    • 1970-01-01
    相关资源
    最近更新 更多