사용자가 RAM에서 앱을 제거해도 알림 (알림에 넣기)을 인 텐트의 추가 정보로 가져 오는 경우에도 알림을 보내야합니다.Android : 앱이 닫힌 상태에서 백그라운드에서 알림 수신을 푸시하십시오.
현재 앱이 백그라운드에서 열리거나 열리면 작동하지만 최근 앱 (RAM에서 삭제됨)에서 앱을 닫으면 추가 기능을 사용할 수 없습니다.
이것은 백그라운드 서비스를 만들고 내가 Extra로 전송해야하는 ID를 넣는 활동입니다.
AddEvent.java
// Gets the ID after it was generated by the database
int ID = newEvent.getID();
if (newEvent.hasNotification()) {
// Creates the intent that starts the background service
Intent serviceIntent = new Intent(this, NotificationService.class);
// Puts the ID and the Notification Time as Extras and starts the service
serviceIntent.putExtra(EXTRA_NOTIFICATION_ID,ID);
serviceIntent.putExtra(EXTRA_NOTIFICATION_TIME,notificationDate.getTimeInMillis());
startService(serviceIntent);
}
이 활동은 내가 확장 한 개념 IntentService를 시작합니다.
NotificationService.java
public class NotificationService extends IntentService {
public NotificationService() {
super("Notification Service");
}
@Override
protected void onHandleIntent(Intent workIntent) {
// Create the intent that is going to push the notification, giving it the previous bundle
Intent notificationIntent = new Intent(this, NotificationPusher.class);
long notificationTime = workIntent.getLongExtra(ActivityAddEvent.EXTRA_NOTIFICATION_TIME,-1);
int ID = workIntent.getIntExtra(ActivityAddEvent.EXTRA_NOTIFICATION_ID,-1);
Log.d("ID Service",""+ID);
notificationIntent.putExtra(ActivityAddEvent.EXTRA_NOTIFICATION_ID,ID);
PendingIntent pusher = PendingIntent.getBroadcast(this, UniqueID.getUniqueID(),
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//TODO : Can't get the Extras in Pusher
// Sets the alarm for the designed date and time
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,notificationTime,pusher);
}
public static class UniqueID {
private final static AtomicInteger uniqueID = new AtomicInteger(0);
public static int getUniqueID() {
return uniqueID.incrementAndGet();
}
}
}
그것은 notitication 시간을 가져 와서 원하는 시간에 알림을 밀어하기 위해 알람 관리자로 설정합니다.
이 정확히이고 ID가 TIME (long) 인 경우 ID를 새 인 텐트에 넣습니다.이 ID는 알림을 보내는 알림 푸셔입니다.
NotificationPusher.java
public class NotificationPusher extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent workIntent) {
// Gets the event from the database using the ID received in the intent
int ID = workIntent.getIntExtra(ActivityAddEvent.EXTRA_NOTIFICATION_ID, -1);
Log.d("ID Pusher", "" + ID);
EventManager eventManager = EventManager.getInstance(context);
Event event = null;
try {
event = eventManager.getEvent(ID);
} catch (SQLException e) {
// TODO: Add error for id not found
e.printStackTrace();
}
if (event != null) {
String notificationTitle;
String notificationText;
// Sets the notification
if (event.hasSubject()) {
notificationTitle = event.getSubject() + "'s " + event.getType().toString();
notificationText = context.getString(R.string.event_notification_default_text);
} else {
notificationTitle = event.getType().toString();
notificationText = event.getTitle();
}
// Create the intent that is gonna be triggered when the notification is clicked and add to the stack
Intent notificationIntent = new Intent(context, ActivitySingleEvent.class);
notificationIntent.putExtra(ActivityAddEvent.EXTRA_NOTIFICATION_ID, ID);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, NotificationService.UniqueID.getUniqueID(),
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// Gets the default sound for notifications
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// Create the notification with a title,icon,text,sound and vibration
NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_alarm_white_24dp)
.setContentTitle(notificationTitle)
.setContentText(notificationText)
.setContentIntent(pendingIntent)
// Notification auto cancel itself when clicked
.setAutoCancel(true)
.setSound(uri)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
.setLights(Color.BLUE, 3000, 3000);
// Build the notification and issue it
Notification notification = nBuilder.build();
NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nManager.notify(ID, notification);
}
else {
Toast.makeText(context,"null event",Toast.LENGTH_SHORT);
}
}
}
가 엑스트라에서 ID를 얻기 위해 시도 알림 푸셔의 상단에하지만 매번 응용 프로그램이 램에서 제거되고, 그것은 기본을 얻는다 값 (-1). 이 ID를 전달하는 방법을 모르겠습니다.
그래서 나는 정상적인 서비스로 내 IntentService를 변경해야합니까? 이미 시도했지만 서비스가 아닌 경우 엑스트라를 추가 할 수 있습니까? 편집 : 영구적 인 시스템 알림이 필요없는 다른 방법이 있습니까? –