关闭应用程序后,我的通知未运行。在此问题中,我使用了intentservice
,但我不知道如何将intentservice
中的代码转换为广播或在应用程序关闭时可以使我的服务继续运行的任何方法。
这是我的服务
public class ReminderAlarmService extends IntentService {
private static final String TAG = ReminderAlarmService.class.getSimpleName();
private static final int NOTIFICATION_ID = 100;
private static final String NOTIFICATION_CHANNEL_ID = "my_notification_channel";
public static PendingIntent getReminderPendingIntent(Context context, Uri uri, int code) {
Intent action = new Intent(context, ReminderAlarmService.class);
action.setData(uri);
return PendingIntent.getService(context, code, action, PendingIntent.FLAG_UPDATE_CURRENT);
}
public ReminderAlarmService() {
super(TAG);
}
@Override
protected void onHandleIntent(Intent intent) {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Uri uri = intent.getData();
//Display a notification to view the task details
Intent action = new Intent(this, Reminder_Activity.class);
action.setData(uri);
PendingIntent operation = TaskStackBuilder.create(this)
.addNextIntentWithParentStack(action)
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
//Grab the task description
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
String title = "";
String keberapa = "";
try {
if (cursor != null && cursor.moveToFirst()) {
title = AlarmReminderContract.getColumnString(cursor, AlarmReminderContract.AlarmReminderEntry.KEY_TITLE);
keberapa = AlarmReminderContract.getColumnString(cursor, AlarmReminderContract.AlarmReminderEntry.KEY_KEBERAPA);
}
} finally {
if (cursor != null) {
cursor.close();
}
}
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Pengingat Minum Obat", NotificationManager.IMPORTANCE_DEFAULT);
// Configure the notification channel.
notificationChannel.setDescription("Jangan lupa untuk minum "+title+" di hari ini ya! Karena kesembuhanmu sudah di depan mata.");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setVibrate(new long[]{0, 100, 100, 100, 100, 100})
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(operation)
.setContentTitle("Pengingat Minum Obat")
.setStyle(new NotificationCompat.BigTextStyle().bigText("Jangan lupa untuk minum "+title+" di hari ini ya! Karena kesembuhanmu sudah di depan mata."));
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
}