alarm and notification not triggered at specified time

感情迁移 提交于 2020-01-11 07:57:21

问题


I am trying to trigger a notification and an alarm at a specified time. I have put log information to console to see if the correct time is being set and it is fine. However, still the alarm is not being triggered. Please help.

/Code for Creating Notification and Alarm/

btn_add_task.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                db.addTask(new DataModel(input_task_title.getText().toString(),input_task_desc.getText().toString(),
                        checkBox.isChecked(),txtDate.getText().toString(),txtTime.getText().toString(),isComplete));

                Calendar calendar = Calendar.getInstance();
                //Set notification for the set date and time
                calendar.set(Calendar.MONTH, month);
                calendar.set(Calendar.YEAR, year);
                calendar.set(Calendar.DAY_OF_MONTH, day);
                calendar.set(Calendar.HOUR_OF_DAY, hour);
                calendar.set(Calendar.MINUTE, minute);
                calendar.set(Calendar.SECOND, 0);
                if(mHour>12) {
                    calendar.set(Calendar.AM_PM, Calendar.PM);
                } else {
                    calendar.set(Calendar.AM_PM, Calendar.AM);
                }

                Log.i("Alarm at: ",calendar.get(Calendar.DAY_OF_MONTH)+"-"+
                        calendar.get(Calendar.MONTH)+"-"+
                        calendar.get(Calendar.YEAR)+" at "+
                        calendar.get(Calendar.HOUR_OF_DAY)+":"+
                        calendar.get(Calendar.MINUTE));

                Intent notifyMessage = new Intent(getApplicationContext(),NotificationMessage.class);
                PendingIntent pi = PendingIntent.getService(getApplicationContext(), 0, notifyMessage, PendingIntent.FLAG_UPDATE_CURRENT);
                AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
                am.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pi);

                Toast.makeText(getApplicationContext(),R.string.new_task_added,Toast.LENGTH_LONG).show();
                startActivity(new Intent(AddTask.this,MainActivity.class));
            }
        });

/Notification Message Class/

package com.apps.katz.doer;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

/**
 * Created by katz on 4/5/16.
 */
public class NotificationMessage extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        showNotification(context);
    }

    private void showNotification(Context context) {
        Log.i("notification","visible");
        PendingIntent contentIntent = PendingIntent.getActivity(context,0,
                new Intent(context,NotificationMessage.class),0);
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context).setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Doer Notification")
                .setContentText("This is a Doer Notification");
        mBuilder.setContentIntent(contentIntent);
        mBuilder.setDefaults(Notification.DEFAULT_SOUND);
        mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
        mBuilder.setDefaults(Notification.DEFAULT_LIGHTS);
        mBuilder.setAutoCancel(true);
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(1,mBuilder.build());
    }
}

Please help what I am doing wrong?


回答1:


Take a look at the documentation of the setRepeating method:

Note: as of API 19, all repeating alarms are inexact. If your application needs precise delivery times then it must use one-time exact alarms, rescheduling each time as described above. Legacy applications whose targetSdkVersion is earlier than API 19 will continue to have all of their alarms, including repeating alarms, treated as exact.

Your alarms will be inexact always using setRepeating, try using setExact and reprograming the alarm when it's triggered.

Also, try extending WakefulBroadcastReceiver instead of BroadcastReceiver in NotificationMessage in order to be able to wake the service when your app isn't running.




回答2:


Inside btn_add_task listener, set the alarm as below -

Intent notifyMessage = new Intent(getApplicationContext(),NotificationMessage.class);
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 0, notifyMessage, PendingIntent.FLAG_UPDATE_CURRENT);    
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pi);

Now, put a log inside onReceive of NotificationMessage class to be sure that the alarm is fired on time. Hope, now your program will run.



来源:https://stackoverflow.com/questions/37189161/alarm-and-notification-not-triggered-at-specified-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!