【问题标题】:Multiple daily repeating notifications at their specific time between in their intervals多个每日重复通知在其间隔之间的特定时间
【发布时间】:2020-06-07 11:44:01
【问题描述】:

对于我的应用,我应该向用户发送通知。

例如:

用户设置了 3 个通知 12:00 18:00 08:00

我想每天发送每个通知,直到我单独取消它们。即使应用程序关闭,也应该发送通知。

主要:

registerAlarm(12, 0, 1);
registerAlarm(18, 0, 2);
registerAlarm(8, 0, 3);

主要方法:

 private PendingIntent registerAlarm(int hour, int minute, int id){
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.MINUTE, minute);
    calendar.set(Calendar.SECOND, 0);

    Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
    intent.putExtra("NAME",id);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, id, intent,0);
    AlarmManager am = (AlarmManager)this.getSystemService(this.ALARM_SERVICE);

    am.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
    System.out.println("SELAMLAR OLSUN ID MAIN: " + id);
    return pendingIntent;
}

private void cancelAlarm(int RSQ) {

    Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RSQ, intent, 0);
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(pendingIntent);
}

报警接收器:

public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);


    Intent notificationIntent = new Intent("com.demo.FirebaseMessagingReceiveService");
    notificationIntent.setClass(context, Main2Activity.class);

    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    int id = Integer.valueOf(intent.getStringExtra("NAME"));
    PendingIntent pendingIntent = PendingIntent.getActivity(context, id, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);


    Notification.Builder builder = new Notification.Builder(context)
            .setSmallIcon(R.drawable.ic_android_black_24dp)
            .setContentTitle("ONLINE QUIZ APP")
            .setContentText("HEY TRY TO SOLVE TODAY")
            .setSound(alarmSound)
            .setAutoCancel(true)
            .setWhen(when)
            .setContentIntent(pendingIntent)
            .setVibrate(new long[] {1000, 1000, 1000, 1000, 1000});

    context.startService(notificationIntent);
    notificationManager.notify(id, builder.build());



}

}

【问题讨论】:

    标签: android-studio push-notification notifications android-notifications


    【解决方案1】:

    intent.putExtra("NAME",id); int id = Integer.valueOf(intent.getStringExtra("NAME"));

    不工作,忽略它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多