Android - 打开活动后重复通知

时间:2017-06-01 11:37:13

标签: android notifications alarmmanager

我正在尝试构建将在特定时间每天发送通知的应用程序。我正在使用 .setOngoing(true) .setAutoCancel(true) 因为我想粘贴通知,除非用户通过点击通知打开应用程序。但是当应用程序打开时,通知会消失并在几秒钟后再次显示..如何在打开应用程序时停止显示通知但仅在特定时间显示?

MainActivity代码为:

$('#parentElement').find('.col-md-1').removeClass('col-md-1');

AlarmReceiver类是:

    public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 16);
        calendar.set(Calendar.MINUTE, 6);
        calendar.set(Calendar.SECOND, 0);
        Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE);
        am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

    }

}

}

1 个答案:

答案 0 :(得分:1)

在你的主要活动中输入以下代码: -

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 1);
calendar.set(Calendar.MINUTE, 49);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM,Calendar.PM);

if(calendar.getTimeInMillis()>System.currentTimeMillis()){

     Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
     pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
     AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE);
     am.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000, pendingIntent); //Repeat every 24 h


 }
else{

    Toast.makeText(getApplicationContext(),"Always set value with greater than current time",Toast.LENGTH_SHORT).show();
}

并更改接收器上的代码如下所示: -

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

        intent = new Intent(context, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra("STARTED_BY_RECEIVER", true);
        context.startActivity(intent);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pendingIntent)
                .setContentText("notification text")
                .setContentTitle("notification title")
                .setAutoCancel(true)
                .setOngoing(true);
        notificationManager.notify(100, builder.build());

试试这个!!