通知未显示android

时间:2017-08-17 02:31:43

标签: android alarmmanager android-notifications

我有一个小问题,我希望它在特定时间之前的24小时内显示,并且会由用户选择,但我的通知由于某种原因没有显示< / p>

以下是我如何设置通知:

private void alarm(String pseudo, Date date){
    AlarmManager alarmMgr;
    PendingIntent alarmIntent;

    Context context = getApplicationContext();

    alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, AlarmReceiver.class);
    intent.putExtra("pseudo",pseudo);
    alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

// Set the alarm before 24hours before the selected date
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.setTimeInMillis(date.getTime() - 24 * 60 * 60 * 1000);
        intent.putExtra("DateVaccin",calendar.getTime());
        Toast.makeText(getApplicationContext(), "Prochaine Notification le "+ calendar.getTime(), Toast.LENGTH_LONG).show();
        getSharedPreferences(pseudo, MODE_PRIVATE).edit().putLong("DateVacc", calendar.getTimeInMillis()).commit();
        alarmMgr.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),alarmIntent);
    }

,这是我的alarmReceiver类:

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Toast.makeText(getApplicationContext(), "Notification déclanchée", Toast.LENGTH_SHORT).show();


        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(getApplicationContext())
                        .setSmallIcon(R.drawable.button_bb_selec)
                        .setContentTitle("Notification Vaccin")
                        .setContentText(intent.getExtras().getString("DateVaccin"));

        //TODO : get to another activity!!

       NotificationManager mNotificationManager =
                (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);

        int mNotificationId = 1;

        mNotificationManager.notify(mNotificationId, mBuilder.build());
    }
}

0 个答案:

没有答案