Не выводится Notification

Рейтинг: 0Ответов: 1Опубликовано: 07.12.2014

Есть код, который должен выводиться в статус баре через сервис, но он не выводится. Посмотрите профи глазом, почему, а то запарился уже...

public class MyServiceNotificationLessons extends Service{

    @Override
    public IBinder onBind(Intent intent){
        return null;
    }
    void someTask()
    {}

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
    someTask();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
    }

    @Override
    public void onCreate(){
        final int NOTIFY_ID=1;
        long when = System.currentTimeMillis();

        Calendar calendar = Calendar.getInstance();
        CharSequence date = calendar.get(Calendar.DAY_OF_MONTH)+"."+calendar.get(Calendar.MONTH)+"."+calendar.get(Calendar.YEAR);
        if((date.equals("5.12.2014"))||(date.equals("6.12.2014"))||(date.equals("7.12.2014"))){

            NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

            int icon = R.drawable.ic_launcher;
            CharSequence contentTitle = "Уважаемый пользователь! ";
            CharSequence contentText = "Будь здоров)))....";

            @SuppressWarnings("deprecation")
            Notification ntf = new Notification(icon,contentTitle,when);
            Context context = getApplicationContext();

            Intent notificationIntent = new Intent(this,MainActivity.class);
            PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0);

            mNotificationManager.notify(NOTIFY_ID,ntf);
        }
    }

    }

Ответы

▲ 2Принят

Проверьте, есть ли у вас в манифесте объявление вашего сервиса.

Обновление 1:

Попробуйте заменить вот эти ваши строки кода

NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

 @SuppressWarnings("deprecation")
 Notification ntf = new Notification(icon,contentTitle,when);

mNotificationManager.notify(NOTIFY_ID,ntf);

на вот эти:

        Notification notification = new NotificationCompat.Builder(this)
        .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), icon))
        .setContentText("Начинаю загрузку")
        .setContentTitle("Загрузка статей")
        .setAutoCancel(true)
        .setContentIntent(contentIntent).build();

        this.startForeground(10, notification);

Да, и попробуйте перенести код из onCreate в onStartComand