禁用存储的提醒警报管理器类型

时间:2018-10-04 11:16:39

标签: android android-recyclerview notifications

我正在尝试启用某些提醒功能,这些物品位于回收站视图中。

启用提醒起作用,并且它应按应有的方式显示。我的问题是,如果用户要取消提醒,则不会取消。我不确定为什么或我做错了什么。谁能帮我吗?

适配器:

 public void onBindViewHolder(@NonNull AssNotiAdapter.MyViewHolder holder, @SuppressLint("RecyclerView") final int position) {
    DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance();
    symbols.setDecimalSeparator('.');
    DecimalFormat format = new DecimalFormat();
    format.setDecimalFormatSymbols(symbols);
    format.setMaximumFractionDigits(2);
    final FAssessment model = assList.get(position);
    holder.textView_title.setText(model.getDesc());
    holder.textView_date.setText(model.getDate());
    holder.textView_time.setText(model.getTime());
    holder.textView_total.setText("Total: "+Double.toString(model.getTotal()));
    if (selected_position == position) {
        holder.itemView.setBackgroundColor(Color.LTGRAY);
    } else {
        holder.itemView.setBackgroundColor(Color.WHITE);
    }
    holder.itemView.setOnClickListener(v -> {

        notifyItemChanged(selected_position);
        selected_position = position;
        notifyItemChanged(selected_position);
    });
    holder.switchDay.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked)
                setReminder(mContext,ReminderReceiver.class,5,model.getDesc());
            else
                cancelReminder(mContext,ReminderReceiver.class,5,model.getDesc());
        }
    });
}

private void setReminder(Context context, Class<?> cls, int sec, String s)
{
    Intent intent = new Intent(context, cls);
    intent.putExtra("TIME",sec);
    intent.putExtra("EDIT",s);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, sec, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    am.set( AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (sec * 1000), pendingIntent );
}
private void cancelReminder(Context context, Class<?> cls, int sec, String s)
{
    Intent intent = new Intent(context, cls);
    intent.putExtra("TIME",sec);
    intent.putExtra("EDIT",s);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
            sec, intent, PendingIntent.FLAG_ONE_SHOT);
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    if(pendingIntent != null) {
        //assert am != null;
        am.cancel(pendingIntent);
    }
}

和ReminderReciever

public class ReminderReceiver extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {
    int Request_Code = intent.getExtras().getInt("TIME",0);
    String r = intent.getExtras().getString("EDIT","");
    showNotification(context, MainActivity.class,
            "You have an Assessment coming up! ", r,Request_Code);
}

public void showNotification(Context context, Class<?> cls, String title, String content,int RequestCode)
{
    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    Intent notificationIntent = new Intent(context, cls);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(cls);
    stackBuilder.addNextIntent(notificationIntent);

    PendingIntent pendingIntent = stackBuilder.getPendingIntent(
            RequestCode,PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationManager notificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = context.getString(R.string.app_name);
        String description = context.getString(R.string.app_name);
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel channel = new NotificationChannel("my_channel_01", name, importance);
        channel.setDescription(description);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        assert notificationManager != null;
        notificationManager.createNotificationChannel(channel);
    }

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context,"my_channel_01");
    Notification notification = builder.setContentTitle(title)
            .setContentText(content).setAutoCancel(true)
            .setSound(alarmSound).setSmallIcon(R.drawable.ic_launcher_background)
            .setContentIntent(pendingIntent).build();


    notificationManager.notify(RequestCode,notification);
}
}

屏幕:

Setting Notification

这是我获得代码的地方

How to send notification without being online

提前谢谢

0 个答案:

没有答案