在指定时间触发的本地通知

时间:2017-10-17 07:05:37

标签: android android-notifications

我有一个会议对象,其中包含会议的结束时间,它有现在的会议/未来会议,我想在会议时间结束时向用户发送通知,我从json获得结束时间并将其保存到领域数据库,但问题是,当我的服务运行时,我在当前时间收到一个通知(意味着我打开应用程序),但它的结束时间是不同的。这是我给了我最好的:

在服务中:onStartCommannd

 realmResults= realm.where(MeetingsModel.class).findAll();
    Intent intent = new Intent(this, LocalBroadCastReceiver.class);

    if(realmResults!=null && realmResults.size()>0){
        for(int i=0;i<realmResults.size();i++){
            // here i am getting endTime of meeting
            String timeStampEnd=realmResults.get(i).getTimestampEnd();
            try {
                calendar.setTime(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").parse(timeStampEnd));
                long tempEndDate=calendar.getTimeInMillis();
                // setting my endTime
                calendar.setTimeInMillis(tempEndDate);
                    // unique id for every meeting
                    intent.putExtra("OBJECT_ID", realmResults.get(i).getId());


            } catch (ParseException e) {
                e.printStackTrace();
            }

        }
    }
    MeetingsModel specificPerson = realm.where(MeetingsModel.class)
            .equalTo("meetingId", intent.getIntExtra("OBJECT_ID",-1))
            .findFirst();
    Log.e("getId---",""+specificPerson.getId() + "----"+calendar.getTimeInMillis());
    AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(getBaseContext().ALARM_SERVICE);

    // pending intent with unique id
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), specificPerson.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);


    // here i am getting endTime of meeting which should trigger when meeting end
    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}

BroadcastReceiver:

// receiving the intent from the service
        MeetingsModel specificPerson = realm.where(MeetingsModel.class)
                .equalTo("meetingId", intent.getIntExtra("OBJECT_ID",-1))
                .findFirst();

        // Here i  am getting all the id of the meeting
        Log.e("specificPerson-",""+specificPerson.getId());

        // Prepare notification title and text
        String title = "Flowace App";
        String text = "Meeting END Time";

        // Prepare notification intent
        // MainActivity is the class that will be opened when user clicks on notification
        Intent intentAction = new Intent(context, MainActivity.class);

        // Notification should get created with same pending intent that sent from the service
        PendingIntent pendingNotificationIntent = PendingIntent.getActivity(context, specificPerson.getId(), intentAction, PendingIntent.FLAG_UPDATE_CURRENT);

        // Send notification (I have a static method in NotificationHelper)
        NotificationHelper.createAndSendNotification(context, title, text, pendingNotificationIntent,specificPerson.getId());

0 个答案:

没有答案