两个通知同时触发?

时间:2019-07-16 12:14:36

标签: java android push-notification

我试图在两个不同的时间发送两个通知,但是当我运行我的应用程序时,两个通知同时触发。

此外,triggerAtMillis:参数不能非常准确地工作,并且通知并非总是在正确的时间发送。

我对通知渠道不太熟悉,每个通知都在单独的渠道上。这些通知可以在同一频道上吗?可以将它们发送到同一接收者吗?

通知助手类:

public static class NotificationHelper extends ContextWrapper {

        public static final String channel1ID = "channel1ID";
        public static final String channel1Name = "USER1";



        public static final String channel2ID = "channel2ID";
        public static final String channel2Name = "USER2";
        private NotificationManager mManager;





        public NotificationHelper(Context base) {
            super(base);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                createChannels();
            }
        }

        @RequiresApi(api = Build.VERSION_CODES.O)
        private void createChannels() {

            NotificationChannel channel1 = new NotificationChannel(channel1ID, channel1Name, NotificationManager.IMPORTANCE_HIGH);
            channel1.enableLights(true);
            channel1.enableVibration(true);
            channel1.setLightColor(R.color.colorPrimary);
            channel1.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);

            NotificationChannel channel2 = new NotificationChannel(channel2ID, channel2Name, NotificationManager.IMPORTANCE_HIGH);
            channel2.enableLights(true);
            channel2.enableVibration(true);
            channel2.setLightColor(R.color.colorPrimary);
            channel2.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);

            getManager().createNotificationChannel(channel1);
            getManager().createNotificationChannel(channel2);


        }

        public NotificationManager getManager() {
            if (mManager == null) {
                mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            }
            return mManager;
        }

        public NotificationCompat.Builder getChannel1Notification() {
            return new NotificationCompat.Builder(getApplicationContext(), channel1ID)
                    .setContentTitle("Dressing")
                    .setContentText("Please scan the dressing on your: "+(et_DressingPos.getText().toString().trim()))
                    .setSmallIcon(R.drawable.ic_cnoticiation_scan);


        }

        public NotificationCompat.Builder getChannel2Notification() {
            return new NotificationCompat.Builder(getApplicationContext(), channel2ID)
                    .setContentTitle("Dressing")
                    .setContentText("Please change the dressing on your: "+(et_DressingPos.getText().toString().trim()))
                    .setSmallIcon(R.drawable.icon_notifications24dp);

        }


    }

警报接收器类:


public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        AddNewDressing.NotificationHelper notificationHelper = new AddNewDressing.NotificationHelper(context);
        NotificationCompat.Builder nb = notificationHelper.getChannel1Notification();
        notificationHelper.getManager().notify(1, nb.build());

        NotificationCompat.Builder nb2 = notificationHelper.getChannel2Notification();
        notificationHelper.getManager().notify(2, nb2.build());

    }
}

启动警报的方法:


private void startAlarmScan() {
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent, 0);


        alarmManager.setExact(AlarmManager.RTC_WAKEUP, 5000, pendingIntent);
    }
    private void startAlarmChange() {
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this, AlarmReceiver.class);
        PendingIntent pendingIntent2 = PendingIntent.getBroadcast(this, 1, intent, 0);


        alarmManager.setExact(AlarmManager.RTC_WAKEUP, 10000, pendingIntent2);
    }

0 个答案:

没有答案
相关问题