我正在尝试在通知面板中显示通知

时间:2019-03-23 13:18:18

标签: android

这是我正在尝试使用的代码。我在android studio中没有任何错误,但是单击按钮时什么也没有发生。我现在还不是100%在android studio中,所以请不要忽略最小的错误。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_notifications );


        Button btncreate = findViewById ( 
 R.id.btnCreateNotification );
        btncreate.setOnClickListener ( new 
View.OnClickListener () {
            @Override
            public void onClick(View view) {
                createNotification();
            }
        } );

    public void createNotification(){


         Intent intent = new Intent (this, 
NotificationRecieverActivity.class);
         PendingIntent pendingIntent = 
 PendingIntent.getActivities (this,(int) 
 System.currentTimeMillis (), new Intent[]{intent}, 0 );

         Notification notification = new Notification.Builder 
 (this   )
            .setContentTitle("Title Notification")
            .setContentText("this is the title notification")
            .setSmallIcon(R.drawable.notificationbell)
            .setContentIntent(pendingIntent)
            .build();

        NotificationManager notificationManager = 
  (NotificationManager) getSystemService ( 
  NOTIFICATION_SERVICE );
        notification.flags = 
   Notification.FLAG_AUTO_CANCEL;

         notificationManager.notify(0, notification);


   }


 }

1 个答案:

答案 0 :(得分:1)

如果您在oreo版本以下使用上面的代码,它将可以正常工作,但是从oreo版本开始,通知已更改,请查看答案...

在您的活动中......

createChannel()

 NotificationCompat.Builder builder=new NotificationCompat.Builder(this, 
 NOTIFICATION_CHANNEL_ID);

    builder
            .setShowWhen(false)
            .setSmallIcon(android.R.drawable.ic_media_play)
            .setLargeIcon(description.getIconBitmap())
            .setColor(this.getResources().getColor(R.color.colorPrimaryDark))
            .setContentTitle(description.getTitle())
            .setContentText(description.getSubtitle())
            .setContentIntent(ContentPI)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .addAction(android.R.drawable.ic_media_previous,"",backPI)
            .addAction(index,"",PPIntent)
            .addAction(android.R.drawable.ic_media_next,"",nextPI)
            .addAction(android.R.drawable.ic_menu_close_clear_cancel,"",StopPi)
            .setColorized(true)

    startForeground(id, builder.build());

创建频道的方法...

public void createChannel()
{
     NotificationChannel channel;
    if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O)
    {
         channel=new NotificationChannel(NOTIFICATION_CHANNEL_ID,
                "Channel one" , NotificationManager.IMPORTANCE_LOW);
        channel.setLightColor(Color.BLUE);
        channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
        NotificationManager manager = (NotificationManager) 
                getSystemService(Context.NOTIFICATION_SERVICE);
        assert manager != null;
        manager.createNotificationChannel(channel);

    }