简体   繁体   English

如何在Android中获取多个通知

[英]How to get multiple notifications in Android

In one activity I have three DatePickerDialog where the user can put three date for a reminder purpose. 在一个活动中,我有三个DatePickerDialog,用户可以在其中放置三个日期以作提醒。 The app must notify each date and if the user enters two or three equal date they must be notified in sequence . 该应用程序必须通知每个日期,如果用户输入两个或三个相等的日期,则必须依次通知他们。 My problem is that only the last date entered is notified. 我的问题是只有最后输入的日期会得到通知。 I use a random number for the notification_id. 我对notification_id使用随机数。 How do I fix? 我该如何解决? Thanks 谢谢

Here the code: 这里的代码:

ScheduleClient.java ScheduleClient.java

public class ScheduleClient {

    private ScheduleService mBoundService;
    private Context mContext;
    private boolean mIsBound;

    public ScheduleClient(Context context) {
        mContext = context;
    }

    public void doBindService() {
        // Establish a connection with our service
        mContext.bindService(new Intent(mContext, ScheduleService.class), mConnection, Context.BIND_AUTO_CREATE);
        mIsBound = true;
    }

    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder  service) {
            // This is called when the connection with our service has been established,
            // giving us the service object we can use to interact with our service.
            mBoundService = ((ScheduleService.ServiceBinder) service).getService();
        }

        public void onServiceDisconnected(ComponentName className) {
            mBoundService = null;
        }
    };

    public void setAlarmForNotification(Calendar c){
        mBoundService.setAlarm(c);
    }

    public void doUnbindService() {
        if (mIsBound) {
            // Detach our existing connection.
            mContext.unbindService(mConnection);
            mIsBound = false;
        }
    }
}

ScheduleService.java ScheduleService.java

public class ScheduleService extends Service {


    public class ServiceBinder extends Binder {
        ScheduleService getService() {
            return ScheduleService.this;
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("ScheduleService", "Received start id " + startId + ": " + intent);

         stopped, so return sticky.
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }


    private final IBinder mBinder = new ServiceBinder();

    public void setAlarm(Calendar c) {
        // This starts a new thread to set the alarm
        // You want to push off your tasks onto a new thread to free up the UI to carry on responding
        new AlarmTask(this, c).run();
    }
}

AlarmTask.java AlarmTask.java

public class AlarmTask implements Runnable {

    private final Calendar date;
    private final AlarmManager am;
    private final Context context;

    public AlarmTask(Context context, Calendar date) {
        this.context = context;
        this.am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        this.date = date;
    }

    @Override
    public void run() {
        // Request to start are service when the alarm date is upon us
        // We don't start an activity as we just want to pop up a notification into the system bar not a full activity
        Intent intent = new Intent(context, NotifyService.class);
        intent.putExtra(NotifyService.INTENT_NOTIFY, true);
        PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);

        // Sets an alarm - note this alarm will be lost if the phone is turned off and on again
        am.set(AlarmManager.RTC, date.getTimeInMillis(), pendingIntent);
    }
}

NotifyService.java NotifyService.java

public class NotifyService extends Service {

    public class ServiceBinder extends Binder {
        NotifyService getService() {
            return NotifyService.this;
        }
    }

    Random random = new Random();
    int randomID = random.nextInt(9999 - 1000) + 1000;
    public static final String INTENT_NOTIFY = "com.try.myapp.INTENT_NOTIFY";
    private NotificationManager mNM;

    @Override
    public void onCreate() {
        Log.i("NotifyService", "onCreate()");
        mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("LocalService", "Received start id " + startId + ": " + intent);

        // If this service was started by out AlarmTask intent then we want to show our notification
        if(intent.getBooleanExtra(INTENT_NOTIFY, false))
            showNotification(randomID);

        // We don't care if this service is stopped as we have already delivered our notification
        return START_NOT_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    private final IBinder mBinder = new ServiceBinder();

    private void showNotification(int notificationID) {
        // This is the 'title' of the notification
        CharSequence title = "Notification!";
        // This is the icon to use on the notification
        int icon = R.drawable.ic_dialog_alert;
        // This is the scrolling text of the notification
        CharSequence text = "Sub text notification.";
        // What time to show on the notification
        long time = System.currentTimeMillis();

        Notification notification = new Notification(icon, text, time);

        // The PendingIntent to launch our activity if the user selects this notification
        PendingIntent contentIntent = PendingIntent.getActivity(this, notificationID, new Intent(this, SecondActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);

        // Set the info for the views that show in the notification panel.
        notification.setLatestEventInfo(this, title, text, contentIntent);

        // Clear the notification when it is pressed
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_LIGHTS;
        notification.defaults |= Notification.DEFAULT_VIBRATE;

        // Send the notification to the system.
        mNM.notify(notificationID, notification);

        // Stop the service when we are finished
        stopSelf();
    }
}

InsertDateActivity.java InsertDateActivity.java

public class InsertCarActivity extends AppCompatActivity {

....

toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            int id = item.getItemId();
            if (id == R.id.saveButton) {
                firstDate = fDate.getText().toString().trim();
                secondDate = sDate.getText().toString().trim();
                thirdDate = tDate.getText().toString().trim();

                String[] arrFirstDate = firstDate.split("-");

                int day = Integer.parseInt(arrFirstDate[0]);
                int month = Integer.parseInt(arrFirstDate[1]);
                month -= 1;
                int year = Integer.parseInt(arrFirstDate[2]);

                Calendar c = Calendar.getInstance();
                c.set(year, month, day);
                c.set(Calendar.HOUR_OF_DAY, 0);
                c.set(Calendar.MINUTE, 0);
                c.set(Calendar.SECOND, 0);

                scheduleClient.setAlarmForNotification(c);

                String[] arrSecondDate = secondDate.split("-");

                int day1 = Integer.parseInt(arrSecondDate[0]);
                int month1 = Integer.parseInt(arrSecondDate[1]);
                month1 -= 1;
                int year1 = Integer.parseInt(arrSecondDate[2]);

                Calendar c1 = Calendar.getInstance();
                c1.set(year1, month1, day1);
                c1.set(Calendar.HOUR_OF_DAY, 0);
                c1.set(Calendar.MINUTE, 0);
                c1.set(Calendar.SECOND, 0);

                scheduleClient.setAlarmForNotification(c1);

                String[] arrThirdDate = thirdDate.split("-");

                int day2 = Integer.parseInt(arrThirdDate[0]);
                int month2 = Integer.parseInt(arrThirdDate[1]);
                month2 -= 1;
                int year2 = Integer.parseInt(arrThirdDate[2]);

                Calendar c2 = Calendar.getInstance();
                c2.set(year2, month2, day2);
                c2.set(Calendar.HOUR_OF_DAY, 0);
                c2.set(Calendar.MINUTE, 0);
                c2.set(Calendar.SECOND, 0);

                scheduleClient.setAlarmForNotification(c2);

                return true;
                }
            return false;

         }

....

}

When you create your PendingIntent here: 在此处创建PendingIntent

PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);

you probably want to pass a different requestCode (the first 0 you're passing) every time or the PendingIntent will be the same as the last one you created and the following: 您可能希望每次都传递一个不同的requestCode (您传递的第一个0 ),否则PendingIntent将与您创建的最后一个相同,并执行以下操作:

am.set(AlarmManager.RTC, date.getTimeInMillis(), pendingIntent);

will set a new alarm for the same PendingIntent as before, so only the last alarm you set will actually ever trigger. 会为之前相同的 PendingIntent设置一个警报,因此只有您设置的最后一个警报才会触发。

From the official docs 来自官方文档

If you truly need multiple distinct PendingIntent objects active at the same time (such as to use as two notifications that are both shown at the same time), then you will need to ensure there is something that is different about them to associate them with different PendingIntents. 如果确实需要同时激活多个不同的PendingIntent对象(例如,用作同时显示的两个通知),则需要确保它们之间存在一些不同之处,以将它们与不同的对象关联PendingIntents。 This may be any of the Intent attributes considered by Intent.filterEquals, or different request code integers supplied to getActivity(Context, int, Intent, int), getActivities(Context, int, Intent[], int), getBroadcast(Context, int, Intent, int), or getService(Context, int, Intent, int). 这可以是Intent.filterEquals考虑的任何Intent属性,或者是提供给getActivity(Context,int,Intent,int),getActivities(Context,int,Intent [],int),getBroadcast(Context,int)的不同请求代码整数。 ,Intent,int或getService(Context,int,Intent,int)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM