Ich muss Benachrichtigungen aus dem Hintergrund senden, auch wenn der Benutzer die App aus dem RAM entfernt, Informationen (in die Benachrichtigung einfügen) als Extras aus den Intents.Android: Drücken Sie dynamische Benachrichtigungen aus dem Hintergrund, während App geschlossen ist
Derzeit funktioniert es, wenn die App im Hintergrund geöffnet oder geöffnet wird, aber es kann keine Extras erhalten, während die App von den letzten Apps geschlossen wird (aus dem RAM entfernt).
Dies ist die Aktivität, die den Hintergrunddienst erstellt und die ID, die ich als Extra übertragen muss, einfügt.
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);
}
Diese Aktivität beginnt eine IntentService, die ich verlängert.
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();
}
}
}
Es wird die notitication Zeit und stellt es auf den Alarm Manager, um die Benachrichtigung zu dem gewünschten Zeitpunkt zu drücken.
Es wird korrekt die ID und die Zeit (so lang) dann setzt die ID in der neuen Absicht, die die Benachrichtigung Pusher ist, die die Benachrichtigung ausgeben.
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);
}
}
}
An der Spitze des Notification Pusher versucht, die ID von Extras zu bekommen, aber jedes Mal die Anwendung von ram entfernt, wird es den Standard Wert (-1). Ich weiß nicht, wie ich diese ID weitergeben soll.
Also muss ich meine IntentService in einen normalen Dienst ändern? Ich habe es bereits ausprobiert, aber wie kann ich Extras hinzufügen, wenn das nicht beabsichtigt ist? Edit: Gibt es eine andere Methode, die keine permanente Systembenachrichtigung benötigt? –