[英]Notification is not working when i close my app
I am creating an app in which user can schedule appointment on specific day. 我正在创建一个应用程序,用户可以在其中安排特定日期的约会。 for testing, when i am checking it by setting alarm for 5 or 10 mins further then it work properly. 对于测试,当我通过设置警报5或10分钟进一步检查它时,它将正常工作。 But if i set alarm for hours or days further or i close my app then it is not giving notification.. following is a method i for creating notification 但是,如果我将闹钟设置了数小时或数天,或者我关闭了我的应用程序,则它没有发出通知。以下是我创建通知的方法
public void setAlarm() {
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, ihour);
c.set(Calendar.MINUTE, iminute);
c.set(Calendar.DAY_OF_MONTH, idate);
c.set(Calendar.MONTH, imonth);
c.set(Calendar.YEAR, iyear);
SharedPreferences sharedPreferences = getSharedPreferences(variable.APPOINTMENT_NO, MODE_PRIVATE);
SharedPreferences.Editor editor = getSharedPreferences(variable.APPOINTMENT_NO, MODE_PRIVATE).edit();
request_id = sharedPreferences.getInt(variable.APPOINTMENT_NO, 0);
if (request_id == 0) {
request_id = 1;
editor.putInt(variable.APPOINTMENT_NO, request_id).commit();
} else {
request_id++;
editor.putInt(variable.APPOINTMENT_NO, request_id).commit();
}
Intent intent1 = new Intent(Doctor_Appointment_Set.this, AppointmentSetReciever_inner.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
Doctor_Appointment_Set.this, request_id, intent1,
0);
AlarmManager am = (AlarmManager) Doctor_Appointment_Set.this
.getSystemService(Doctor_Appointment_Set.this.ALARM_SERVICE);
am.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
Log.d(getClass().getSimpleName(), request_id + "");
}
and this is my broadcast receiver class. 这是我的广播接收器课程。 i created it as a inner class 我将其创建为内部类
public static class AppointmentSetReciever_inner extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Random randomGenerator = new Random();
int index = randomGenerator.nextInt(3);
Log.d(getClass().getSimpleName(), request_id + "");
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, Show_Appointment_Notification.class);
notificationIntent.putExtra("NotificationMessage", dr_name + " | " + st_time + " | " + st_date);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, request_id,
notificationIntent, 0);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
context)
.setContentTitle("Appointment")
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.male))
.setSmallIcon(R.mipmap.logo)
.setContentText("Today You Have an Appointment with Dr. " + dr_name)
//.setStyle(new NotificationCompat.BigTextStyle().bigText(tip.get(index)))
.setSound(alarmSound)
.setAutoCancel(true).setWhen(when)
.setContentIntent(pendingIntent)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
.setPriority(Notification.PRIORITY_MAX);
notificationManager.notify(request_id, mNotifyBuilder.build());
}
}
You should use a receiver to create an alarm independently whether or not the application is running, for this you have to create it as follows: 无论应用程序是否正在运行,都应使用接收器独立创建警报,为此,您必须按以下方式创建警报:
In the manifest: 在清单中:
<receiver android:name=".OnBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
The code: 编码:
public class OnBootReceiver extends BroadcastReceiver {
private static final String TAG = OnBootReceiver.class.getSimpleName();
private static final int PERIOD = 1000 * 60 * 10; // 10 minutes
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
setAlarm(context);
}
}
public static void setAlarm(Context context) {
AlarmManager mgr =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
boolean alarmUp = (PendingIntent.getBroadcast(context, 0,
new Intent(context, Doctor_Appointment_Set.class),PendingIntent.FLAG_NO_CREATE) != null);
if (!alarmUp){
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime()+60000,
PERIOD,
getPendingIntent(context));
}else{
Log.i(TAG, "OnBootReceiver : Alarm is already active");
}
}
public static void cancelAlarm(Context ctxt) {
AlarmManager mgr=(AlarmManager)ctxt.getSystemService(Context.ALARM_SERVICE);
mgr.cancel(getPendingIntent(ctxt));
}
private static PendingIntent getPendingIntent(Context ctxt) {
Intent i=new Intent(ctxt, OnAlarmWakefulReceiver.class);
return(PendingIntent.getBroadcast(ctxt, 0, i, PendingIntent.FLAG_UPDATE_CURRENT));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.