1
I’m having a problem in my app, it for some reason does not show the notification but the log. i runs normally!
Code:
package imm.pt.immsmart;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
public class ServicoPush extends Service{
public List<Worker> threads = new ArrayList<Worker>();
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate(){
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId){
Worker w = new Worker(startId);
w.start();
threads.add(w);
return(super.onStartCommand(intent, flags, startId));
}
class Worker extends Thread{
public int startId;
public boolean ativo = true;
public Worker(int startId){
this.startId = startId;
}
public void run(){
while (ativo){
try {
Thread.sleep(3000);
Log.i("teste", "ok");
//Notificação
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(ServicoPush.this);
builder.setTicker("Limite de Dados Moveis");
builder.setContentTitle("IMM SMART");
builder.setContentText("Estas quase a atingir o teu limite de dados!");
builder.setSmallIcon(R.drawable.ic_launcher_background);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background));
Notification n = builder.build();
n.vibrate = new long[]{150, 300, 150, 600};
nm.notify(R.drawable.ic_launcher_background, n);
try{
Uri som = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone toque = RingtoneManager.getRingtone(ServicoPush.this, som);
toque.play();
}catch (Exception e) {e.printStackTrace();}
ativo = false;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
@Override
public void onDestroy(){
super.onDestroy();
for(int i = 0, tam = threads.size(); i < tam; i++){
threads.get(i).ativo = false;
}
}
}