我的接收器没有显示通知-为什么?

时间:2018-11-16 12:00:42

标签: java android

//我曾打算进行oncreate

UnaryOperator<String>

//在这里我要设置闹钟

UnaryOperator<Object>

//这是我的接收者类

 Intent alarmIntent = new Intent(getContext(), ServiceReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(getContext(),
                0, alarmIntent, 0);

///我在清单中提到了接收器类

 public void setALARM(String time, String strDate){

            AlarmManager manager = (AlarmManager) getContext()
                    .getSystemService(Context.ALARM_SERVICE);

            String strHour=formateDateFromstring("HH:mm","HH",time);
            String strMin=formateDateFromstring("HH:mm","mm",time);
            String strdate=formateDateFromstring("yyyy-MM-dd","dd",strDate);
            String strMonth=formateDateFromstring("yyyy-MM-dd","MM",strDate);
            String strYear=formateDateFromstring("yyyy-MM-dd","yyyy",strDate);

            int hour=Integer.parseInt(strHour);
            int min=Integer.parseInt(strMin);
            int date=Integer.parseInt(strdate);
            int month=Integer.parseInt(strMonth)-1;
            int year=Integer.parseInt(strYear);

            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.DAY_OF_MONTH,date);  //1-31
            cal.set(Calendar.MONTH,month);  //first month is 0!!! January is zero!!!
            cal.set(Calendar.YEAR,year);//year...

            cal.set(Calendar.HOUR_OF_DAY,hour);  //HOUR
            cal.set(Calendar.MINUTE,min);//MIN
            cal.set(Calendar.SECOND,0);


            manager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                    pendingIntent);
        }

我想设置一个确切时间的警报。我在调试模式下检查了代码是否在给定时间到了onReceive内进行调试。到了给定时间就开始进行调试了。问题是通知不可见。

2 个答案:

答案 0 :(得分:0)

不确定是否是您的特定问题,但是API级别26.1.0中不推荐使用NotificationCompat.Builder(Context context)。您应该改用NotificationCompat.Builder(Context, String)。所有发布的通知都必须为26台以上的设备指定一个NotificationChannel ID,以便您可以检入Notification and Notification.Builder documentation

答案 1 :(得分:0)

尝试类似的方法,未经测试。

   String channelID  = "Notification_Channel_ID";
   String channelDesr = "Notification_channel_description";
   buildNotificationChannel(channelID,channelDesr);



    public void buildNotificationChannel(String channelID, String description) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager manager = (NotificationManager) 
            context.getSystemService(Context.NOTIFICATION_SERVICE);
           if (manager.getNotificationChannel(channelID) == null) {
                NotificationChannel channel = new NotificationChannel(channelID, description,
            NotificationManager.IMPORTANCE_LOW);
            channel.setDescription(description);
            manager.createNotificationChannel(channel);
        }
    }
}

   NotificationCompat.Builder builder = new 
             NotificationCompat.Builder(context,channelID)
            .setSmallIcon(R.drawable.logob_icon_prestigesalon)
            .setContentTitle("ABC")
            .setContentText("time to go")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT);
           Toast.makeText(context,"service",Toast.LENGTH_LONG).show();
           NotificationManager notificationmanager = (NotificationManager) context
              .getSystemService(Context.NOTIFICATION_SERVICE);
            notificationmanager.notify(0, builder.build());