使用alarmmanager和Broadcast接收器发送通知

时间:2017-07-04 05:45:18

标签: android

实际上我已经为我的应用程序实现了通知。每当我点击一个按钮时,它就会设置通知和呼叫广播接收器的时间。但是当发送3个通知的请求时,如下午3点,下午4点,下午5点发送请求发送下午5点通知,不是下午3点和下午4点,因为下午5点一个是最后触发的。我认为这与通知ID有关。我正在传递id为0.我应该做什么。请帮助。

我的广播接收器

public class NotificationMessage extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent arg1) {
        Log.d("Tag","dd");
        showNotification(context);
    }

    private void showNotification(Context context) {
        Log.d("Tag", "visible");

        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                new Intent(context, MainActivity.class), 0);


        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(R.drawable.logo)
                        .setContentTitle("HURRY UP!!")
                        .setContentText("You have a session in 15 minutes.");
        mBuilder.setContentIntent(contentIntent);
        mBuilder.setDefaults(Notification.DEFAULT_SOUND);
        mBuilder.setAutoCancel(true);
        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        int notiid = preferences.getInt("Id", -1 );

        int notification_id = notiid;
        Log.d("ID1", String.valueOf(notification_id));

        mNotificationManager.notify(notification_id, mBuilder.build());
        Log.d("Tag","fuck u");
    }
}

这是我打电话的地方:

holder.accept.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                Call<AcceptedDealResponse> call = service.acceptedDeal(acceptDealRequest);
                call.enqueue(new Callback<AcceptedDealResponse>() {
                    @Override
                    public void onResponse(Call<AcceptedDealResponse> call, Response<AcceptedDealResponse> response) {
                        AcceptedDealResponse response1 = response.body();
                        if(response.body().getMessage().equals("Successful !! Deal Booked !!")){
                            Log.d(",,", "" + " deal boooked");


                            Log.d("jkhdjksahda  ","" + newDealResponsess.getResponses().get(position).getTimeFrom() );


                            DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
                            try {
                                date1 = formatter.parse(String.valueOf(newDealResponsess.getResponses().get(position).getTimeFrom()));

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

                            cal = Calendar.getInstance();
                            cal.setTime(date1);

                            Log.d("Tag", String.valueOf(cal.get(Calendar.DATE)));


                            calendar = Calendar.getInstance();

                            calendar.set(Calendar.YEAR, cal.get(Calendar.YEAR));
                            calendar.set(Calendar.MONTH, cal.get(Calendar.MONTH));
                            calendar.set(Calendar.DATE, cal.get(Calendar.DATE));
                            calendar.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY));
                            calendar.set(Calendar.MINUTE, cal.get(Calendar.MINUTE));
                            calendar.set(Calendar.SECOND, cal.get(Calendar.SECOND));

                            Log.d("hdfbsdhbfs :", "" + calendar.get(Calendar.HOUR_OF_DAY));
                            Log.d("hdfbsdhbfs :", "" + calendar.get(Calendar.MINUTE));
                            Log.d("hdfbsdhbfs :", "" + calendar.get(Calendar.SECOND));
                            Log.d("hdfbsdhbfs :", "" + calendar.get(Calendar.DATE));
                            Log.d("hdfbsdhbfs :", "" + calendar.get(Calendar.MONTH));
                            Log.d("hdfbsdhbfs :", "" + calendar.get(Calendar.YEAR));

                            Intent notificationmassage = new Intent(v.getContext(),NotificationMessage.class);

                            Log.d("Tag","cool");

                            PendingIntent pi = PendingIntent.getBroadcast(v.getContext(), 0,notificationmassage, PendingIntent.FLAG_UPDATE_CURRENT);


                            Log.d("Tag","cooled");
                            AlarmManager am = (AlarmManager) v.getContext().getSystemService(ALARM_SERVICE);
                            Log.d("Tag","cooooled");

                            int id = (int) calendar.getTimeInMillis();

                            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(v.getContext());
                            SharedPreferences.Editor editor = preferences.edit();
                            editor.putInt("Id", id);
                            Log.d("ID", String.valueOf(id));
                            editor.apply();


                            am.set(AlarmManager.RTC_WAKEUP, (calendar.getTimeInMillis() - 900000), pi);

1 个答案:

答案 0 :(得分:0)

我想问题出在你的PendingIntent REQUEST_CODE。在使用AlarmManager设置REQUEST_CODE时,您可能已使用PendingInetent对所有alarm使用相同的alarm

  

尝试为不同的REQUEST_CODE使用不同的PendingIntent   不同的Alarm

使用以下代码使用Alarm设置AlarmManager

    int notiId = UNIQUE_ID;

    // Intent
    Intent mIntent = new Intent(getApplicationContext(), NotificationMessage.class);
    mIntent.putExtra("NOTIFICATION_ID", notiId);

    // Pending broadcast intent
    PendingIntent mPI = PendingIntent.getBroadcast(getApplicationContext(), 
                        notiId, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    // Alarm manager
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, TARGET_TIME_MILLISECONDS, mPI);

希望这会起作用〜