Ich versuche eine Notification aus einem Background Task heraus aufzurufen. Einen Toast kann ich anzeigen lassen aber es erscheint keine Notification, beim ersten mal. Wieso passiet das?
Backgound Task etc. nach: https://guides.codepath.com/an…anager-for-periodic-tasks
public class OfflineUpdate extends IntentService{
public OfflineUpdate() {
super("OfflineUpdater");
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
NotificationUtils.sendNotification(this,"Update", "Hello hello!");
Toast.makeText(this,"UpdateGlobal",Toast.LENGTH_SHORT).show();
ListManager.update(this);
}
}
//Notification Utils
public class NotificationUtils {
public static void sendNotification(Context c, String title, String message) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(c)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(message);
Intent resultIntent = new Intent(c, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(c);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) c.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
}
Alles anzeigen